Reputation: 43
I have the following code which writes a data output to a csv file. I want to add a date variable at the end of each row using the yesterday.strftime variable that i am using in creating the filename. For example:
Thanks!
my current output is like:
columnA
1
2
and I want to add the following column:
Date
2/5/2016
2/5/2016
. . .
CODE::
filepath = 'C:\\test\\'
filename = yesterday.strftime('%Y-%m-%d') + '_' + 'test.csv'
f = open( filename, 'wt')
writer = csv.writer(f, lineterminator='\n')
header = [h['name'][3:] for h in results.get('columnHeaders')]
writer.writerow(header)
print(''.join('%30s' % h for h in header))
# Write data table.
if results.get('rows', []):
for row in results.get('rows'):
writer.writerow(row)
print(''.join('%30s' % r for r in row))
else:
print ('No Rows Found')
f.close()
Upvotes: 2
Views: 2687
Reputation: 6758
In [26]: import pandas as pd
In [27]: import datetime
In [28]: a = pd.read_csv('a.csv')
In [29]: a
Out[29]:
columnA
0 1
1 2
In [30]: a['Date'] = [datetime.date.today()]*len(a)
In [31]: a
Out[31]:
columnA Date
0 1 2016-02-05
1 2 2016-02-05
In [32]: a.to_csv('adate.csv')
Generally: https://www.airpair.com/python/posts/top-mistakes-python-big-data-analytics
Upvotes: 1