Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python将日期列添加到csv_Python_Csv - Fatal编程技术网

使用python将日期列添加到csv

使用python将日期列添加到csv,python,csv,Python,Csv,我有以下将数据输出写入csv文件的代码。我想使用创建文件名时使用的dayed.strftime变量在每行末尾添加一个日期变量。例如: 谢谢 我的当前输出如下所示: 圆柱 一, 二, 我想添加以下列: 日期 2016年2月5日 2016年2月5日 。 . 代码:: filepath = 'C:\\test\\' filename = yesterday.strftime('%Y-%m-%d') + '_' + 'test.csv' f = open( filename, 'wt') wr

我有以下将数据输出写入csv文件的代码。我想使用创建文件名时使用的dayed.strftime变量在每行末尾添加一个日期变量。例如:

谢谢

我的当前输出如下所示:

圆柱

一,

二,

我想添加以下列:

日期

2016年2月5日

2016年2月5日

。 .

代码::

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()

一般来说:

结果从哪里来?您可能需要查看这些结果,您可以将
if results.get('rows',[]):
替换为结果中的行。get('rows'):
仅替换为
if results.get('rows'):
,用于结果中的行['rows]:;第一种用法除了布尔测试外不使用默认值(因此默认值
None
是可以的),第二种用法已经知道密钥存在,因此根本不需要使用
。get
。首先感谢您的发布。我运行了代码,但它在文件开头添加了某种索引字段。有没有办法让我省掉它或者给它起个名字?目前只是空白。
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')