繁体   English   中英

Python / CSV:由于DictReader,无法在函数中打开CSV文件(int对象)

[英]Python/CSV: opening CSV file in function as DictReader is not working (int object)

我想在Python函数中以DictReader打开一个csv文件,然后将其放入列表中。 在ipython shell中执行此操作即可。 但是,如果尝试调用应该完全相同的函数,则会得到: TypeError: 'int' object is not iterable

def get_all(pathtofile):
    mctable = csv.DictReader(open(pathtofile), delimiter='\t') 
    for x in mctable:
        print x

贝壳:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM