[英]Python/CSV: opening CSV file in function as DictReader is not working (int object)
I want to open a csv-file in a python function as DictReader and then make it into a list. 我想在Python函数中以DictReader打开一个csv文件,然后将其放入列表中。 Doing this in the ipython shell works fine. 在ipython shell中执行此操作即可。 However, if I try to call a function that should do exactly the same, I get: TypeError: 'int' object is not iterable
但是,如果尝试调用应该完全相同的函数,则会得到: TypeError: 'int' object is not iterable
def get_all(pathtofile):
mctable = csv.DictReader(open(pathtofile), delimiter='\t')
for x in mctable:
print x
shell: 贝壳:
In [77]: get_all('amaecht.csv')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-77-a467058ba260> in <module>()
----> 1 get_all('amaecht.csv')
/getmc.py in get_all(mclist)
3 mctable=csv.DictReader(open(pathtofile), delimiter='\t')
4 for x in mctable:
----> 5 print x
TypeError: 'int' object is not iterable
Try something like this it worked for me when hope it will be helpful to you aswell 如果希望对您有所帮助,请尝试对我有用的类似方法
file = open('/Desktop/d.csv', "r")
csv_file =csv.reader(file.read().splitlines())
header = ['title','price' ]
for row in csv_file:
params = dict(zip(header, row))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.