I am trying to make a csv file using the lists
['2005-04-18', '2005-04-19', '2005-04-20']
['1', '1', '1']
['0.7683', '0.766664', '0.764696']
['1.380162', '1.373308', '1.358544']
with the dates in the first column and the corresponding values across the rows
i already have
with open((os.path.join('data','hist', 'rates-'+date1+' to ' +date2+ '.csv')), 'wt') as my_csv:
csv_writer = csv.writer(my_csv, lineterminator='\n')
csv_writer.writerow(["Date",curr1, curr2, curr3])
which gives me the file with the headers. How can I finish this?
You use a loop to process the elements of the arrays that you have.
a = ['2005-04-18', '2005-04-19', '2005-04-20']
b = ['1', '1', '1']
c = ['0.7683', '0.766664', '0.764696']
d = ['1.380162', '1.373308', '1.358544']
You can use zip
to create tuples from the lists and write those:
for row in zip(a, b, c, d):
csv_writer.writerow(row)
Or you can just youse string formatting and print it into the file yourself (without using csv_writer):
for i in range(len(a)):
csv_writer.writerow('{}, {}, {}, {}'
.format(a[i], b[i], c[i], d[i]))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.