Add a date column to csv using python -
i have following code writes data output csv file. want add date variable @ end of each row using yesterday.strftime variable using in creating filename. example:
thanks!
my current output like:
columna
1
2
and want add 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:] h in results.get('columnheaders')] writer.writerow(header) print(''.join('%30s' % h h in header)) # write data table. if results.get('rows', []): row in results.get('rows'): writer.writerow(row) print(''.join('%30s' % r r in row)) else: print ('no rows found') f.close()
in [26]: import pandas pd in [27]: import datetime in [28]: = pd.read_csv('a.csv') in [29]: out[29]: columna 0 1 1 2 in [30]: a['date'] = [datetime.date.today()]*len(a) in [31]: 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
Comments
Post a Comment