【发布时间】:2013-11-27 22:02:38
【问题描述】:
我在 csv.reader 中尝试了很多选项,但它不起作用。我是 python 新手,几乎尝试了每个参数,我的 csv 文件中的单个凌乱消息看起来像这样
"Hey Hi
how are you all,I stuck into this problem,i have tried with such parameter but exceeding the existing number of records,in short file is not getting read properly.
\"I have tried with
datareader=csv.reader(csvfile,quotechar='"',lineterminator='\n\n\n\r\r',quoting=csv.QUOTE_ALL)
Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode? \"......... hence the problem continue.
"
正如预期的那样,由于 \" 和 \n 在消息中获得更多记录或记录被打破,我尝试了不同的行终止符,你可以看到在消息中但没有成功,这是我现在的代码..
with open("D:/Python/mssg5.csv", "r") as csvfile:
datareader = csv.reader(csvfile,quotechar='"' ,lineterminator='\n',quoting=csv.QUOTE_ALL)
count = 0
#csv_out = open('D:/Python/mycsv.csv', 'wb')
#mywriter = csv.writer(csv_out)
for row in datareader:
count = count + 1
print "COUNT is :%d" % count
任何帮助,谢谢。
【问题讨论】:
-
很难复制您的问题,您能否提供一个 csv 文件并给我们一些有关文件编码等的详细信息?顺便说一句,您是否只是尝试使用通用换行模式(“rU”而不是“r”)?