I am trying to retrieve files from the internet, save them as dataframe (without saving the files on computer), do some calculation, save resulting dataframes in the required format as csv files, zip them and send them back to the orginal source(see below my code). This works perfectly fine but as I have to send hundred of files and was wondering if there is a way of saving the dataframe to the csv file and then zipping it on the go (i.e. without saving it on computer)? Below is my code;
file_names=['A','B','C']
hdll_url = 'http://XYZ'
saving_folder_url = 'C:\Users\Desktop\Testing_Folder'
general = []
for i in file_names:
url = ('http://')
data1 = requests.get(url)
Z1 = zipfile.ZipFile(StringIO.StringIO(data1.content))
x=pd.read_csv(Z1.open('%s.tsv'%i), sep = '\t', names=["Datetime","Power"])
x['Datetime'] = pd.to_datetime(x['Datetime'])
x = x.set_index("Datetime")
x = x.resample('H', how= 'sum')
general.append(x)
ABC = pd.DataFrame(general[0]['Power'] + general[1]['Power'] + general[3]['Power'] * 11.363)
ABC.to_csv('%s\ABC.tsv'%saving_folder_url,
sep='\t',header=False,date_format='%Y-%m-%dT%H:%M:%S.%f')
ABC_z1 = zipfile.ZipFile('%s\ABC.zip'%saving_folder_url,mode='w')
ABC_z1.write("%s\ABC.tsv"%saving_folder_url)
ABC_z1.close()
ABC_files1 = {'file': open("%s\ABC.zip"%saving_folder_url,'rb')}
ABC_r1 = requests.put(hdll_url, files=ABC_files1, headers = {'content-type':'application/zip',
'Content-Disposition': 'attachment;filename=ABC.zip'
} )
XYZ = pd.DataFrame(general[0]['Power'] + general[1]['Power']*100 )
XYZ.to_csv('%s\XYZ.tsv'%saving_folder_url,
sep='\t',header=False,date_format='%Y-%m-%dT%H:%M:%S.%f')
XYZ_z1 = zipfile.ZipFile('%s\XYZ.zip'%saving_folder_url,mode='w')
XYZ_z1.write("%s\XYZ.tsv"%saving_folder_url)
XYZ_z1.close()
XYZ_files1 = {'file': open("%s\XYZ.zip"%saving_folder_url,'rb')}
XYZ_r1 = requests.put(hdll_url, files=XYZ_files1, headers = {'content-type':'application/zip',
'Content-Disposition': 'attachment;filename=XYZ.zip'
} )
P.S: I am only allowed to send files in .zip format.