Posts

Showing posts with the label Python

Download zip file using Python

https://www.zerotosingularity.com/blog/downloading-datasets-introducting-pdl/ import requests import zipfile import os # download file resp = requests.get(f"{url}{file}", allow_redirects=True, stream=True) filename = f"{download_dir}{file}" zfile = open(filename, 'wb') zfile.write(resp.content) zfile.close() zipf = zipfile.ZipFile(filename, 'r') zipf.extractall(download_dir) zipf.close() os.remove(filename) YEAR=2015 for MONTH=`seq -w 1 12`; do PARAMS="UserTableName... RESPONSE=$(curl -X POST --data "$PARAMS" http://www.transtats.bts.gov/DownLoad_Table.asp?Table_ID=236&Has_Group=3&Is_ Zipped=0) echo "Received $RESPONSE" ZIPFILE=$(echo $RESPONSE | tr '\"' '\n' | grep zip) echo $ZIPFILE curl -o $YEAR$MONTH.zip $ZIPFILE done Is this useful? https://stackoverflow.com/questions/51665087/python-download-zip-file-from-restapi

Saving data to MySQL with Python

import mysql.connector from time import sleep import random import sys ## C:\ProgramData\MySQL\MySQL Server 8.0\my.ini ## https://dev.mysql.com/doc/refman/8.0/en/caching-sha2-pluggable-authentication.html#caching-sha2-pluggable-authentication-installation try:     username = "yourusername"; password="yourpassword";     database="yourdatabase";host="localhost";     cnx = mysql.connector.connect(user=username, password=password,                                   host=host,database=database)     cursor = cnx.cursor()     print("Successfully connected to database!")          update = True     while update:         try:             value = random.randint(0,1024)              sensor_value = value       ...

Install gspread with Anaconda

Use this command conda install -c conda-forge gspread  Read about it at https://anaconda.org/conda-forge/gspread More links about gspread below https://pypi.org/project/gspread/ https://github.com/burnash/gspread https://gspread.readthedocs.io/en/latest/

Upgrade Python on Anaconda

https://conda.io/docs/user-guide/tasks/manage-python.html#updating-or-upgrading-python https://www.anaconda.com/download/ Upgrade to the latest version on the same branch conda update python Upgrade to a specific version conda install python = 3 .6

Steps to create and activate Python virtual environment

Some rough steps below pip install virtualenv virtualenv --version virtualenv my_project cd my_project/Scripts activate https://virtualenv.pypa.io/en/latest/reference/ virtualenv -p C:\Python27\python.exe mypython27 cd mypython27/Scripts activate deactivate

A simple Python program that reads a textfile and creates 2-level folder structure based on the contents of the textfile

import os, sys # If directory (path) doesn’t exist, create it def createPath(path):    if not os.path.isdir(path):       os.mkdir(path) f = open(r"F:\Folders.txt") folder = "F:\\Documents\\" ## Read the first line line = f.readline() ## If the file is not empty keep reading line one at a time till the file is empty i=1 while line:     #print(line)     for char in '\n?.!/;:':        line = line.replace(char,'')     position = line.find("*")     if position == 0:       #print("line is {}" .format(line[1:]))       folder2 = folder + str(i) + "." + line[1:]       print(folder2)       createPath(folder2)       j=1       i=i+1     else:       if len(line.strip()) != 0:         folder3 = folder2 + "\\" + str(j) + "." + line       ...