Python/CSV:在函数中打开CSV文件,因为DictReader不工作(int对象)
我想用python函数作为DictReader打开一个csv文件,然后将其放入列表中。 在ipython外壳中执行此操作效果良好。 但是,如果我尝试调用一个应该完全相同的函数,我会得到: TypeError:“int”对象不可编辑 外壳:Python/CSV:在函数中打开CSV文件,因为DictReader不工作(int对象),python,csv,Python,Csv,我想用python函数作为DictReader打开一个csv文件,然后将其放入列表中。 在ipython外壳中执行此操作效果良好。 但是,如果我尝试调用一个应该完全相同的函数,我会得到: TypeError:“int”对象不可编辑 外壳: In [77]: get_all('amaecht.csv') --------------------------------------------------------------------------- TypeError
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
试试这样的东西我在 希望它也能对你有所帮助
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))
很可能,amaecht.csv是否与调用它的模块位于同一目录中?另外,“int”对象不可iterable基本上就是它的意思:您正在为mctable返回一个整数。请打印mctable的内容好吗?您确定您向我们显示的代码就是给出错误消息的代码吗?
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))