类型Error参数1需要是迭代器

时间:2018-02-20 22:04:39

标签: python

我正在尝试将数据从我的filteredData.csv复制到我已经完成的average2016.csv,但是当我正在尝试分割我正在获得的日期时

File 
"U:/CISP253/Final/test", line 9, in <module>
    readCSV = csv.reader(csvfile, delimiter=',')
TypeError: argument 1 must be an iterator
>>> 

作为我的错误。

import shutil
import csv
import collections
with open("filteredData.csv", "wb") as csvfile:
    csvfile.close()

with open("average2016.csv", "w") as csvfile2:
    shutil.copyfile('filteredData.csv', 'average2016.csv')
    readCSV = csv.reader(csvfile2, delimiter=',')
    result = collections.defaultdict(list)
    for row in readCSV:
       year = row[1].split("-")[0]
       result[year].append(row)

    csvfile2.close()

我对readCSV = csv.reader(csvfile2,delimiter =',')进行了更改,这是必要的,就像上面提到的那样将csvfile转换为csvfile2。 这也是分割日期的最好方法吗?

谢谢

1 个答案:

答案 0 :(得分:0)

您忘了更改为csvfile2

with open("average2016.csv", "w") as csvfile2:
    shutil.copyfile('filteredData.csv', 'average2016.csv')
    readCSV = csv.reader(csvfile2, delimiter=',')