迭代器应返回字符串,而不是csv.reader列出错误

时间:2018-05-10 11:51:52

标签: python string python-3.x python-2.7 csv

此代码显示标题中的错误..帮我plz.I写了下面的代码写入csv文件读取另一个文件的输入,但这显示错误。当我将“r”更改为“rb “然后它显示”迭代器应该返回字节“错误。

错误 - >回溯(最近一次调用最后一次):   文件“C:/Users/abhirav.sati/Downloads/salesdata.py”,第34行,in     csv_reader(csvReader,路径)   csv_reader中的文件“C:/Users/abhirav.sati/Downloads/salesdata.py”,第11行     对于读取行: _csv.Error:迭代器应该返回字符串,而不是列表(你是否以文本模式打开文件?)

import csv
def csv_reader(fileobj,path):
    read=csv.reader(fileobj,delimiter=',')
    with open(path, "wt") as csv_file:
        write=csv.writer(csv_file, delimiter=',')
        i=1
        for row in read:
            if(i==1):
                write.writerow(",".join(row))
                i=2
                continue
            if(row[3]=="Trade"):
                continue
            else:
                if(row[6]==NULL):
                    r=[row[0],row[0],"A,",row[8],row[9],row[0]]
                    #r=row[0]+row[0]+"A,"+row[8]+row[9]+row[0]
                    write.writerow(r)
                else:
                    r=[row[0],row[0],"B,",row[6],row[7],row[0]]
                    #r=row[0]+row[0]+"A,"+row[8]+row[9]+row[0]
                    write.writerow(r)
if __name__ == "__main__":
    path="sales.csv"
    csv_path = "FlowEdge-TRTH-Time_Sales.csv"
    f_obj  = open(csv_path, "r")
    data=csv.reader((line.replace('\0','') for line in f_obj), delimiter=",")
    csv_reader(data,path)

0 个答案:

没有答案