在python

时间:2016-12-05 08:25:10

标签: python-2.7 csv

我一直收到一个"无效的关键字"我尝试从python中的csv文件读取时出错。有什么想法可以解决这个问题吗?

C:\Python27\python.exe C:/Users/User_Name/PycharmProjects/untitled/car.py

Traceback (most recent call last): File 
"C:/Users/User_Name/PycharmProjects/untitled/car.py", line 122, in <module> d = handle_refugee_data.DataTable(csvformat="generic", data_directory="car2014", start_date="2013-12-01") File 
"C:\Users\User_Name\PycharmProjects\untitled\handle_refugee_data.py", line 78, in __init__ with open("%s/%s" % (data_directory, data_layout), newline='') as csvfile: 
TypeError: 'newline' is an invalid keyword argument for this function 

Process finished with exit code 1
========================================================================

1 个答案:

答案 0 :(得分:2)

newlineopen() in Python 3的有效关键字参数,但不是in Python 2,它似乎就是您所使用的。

如果可能,一种解决方案是使用Python 3执行脚本。或者,正如评论中 @dhke 所指出的那样,您可以改为使用io.open(),它接受​​newline关键字参数。

当然,您可以使用csv module代替,具体取决于您的使用案例(原始问题并不清楚)。