2016-12-05 70 views
0

当我尝试从python中的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 
======================================================================== 
+0

你能发布你的代码吗? – bhazero025

+1

它看起来像'open()'没有这样的关键字:https://docs.python.org/2/library/functions.html#open但是,只有在你发布你的代码后, 。 – wanderlust

+0

就此而言:[csv](https://docs.python.org/2/library/csv.html)位于标准库中。 – dhke

回答

2

newline是一个有效的关键字参数open()in Python 3,但不in Python 2,这似乎是你使用的是什么。

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

当然,您可以使用csv module来代替,具体取决于您的用例(原始问题中不明确)。

+1

...或使用['io.open()'](https://docs.python.org/2/library/io.html#io.open)。 – dhke

+0

@dhke谢谢你的提示!我会将其添加到答案中。 – elethan