【问题标题】:CSV new-line character and qoute inside quoted fieldCSV 换行符和引用字段内的 qoute
【发布时间】: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”)?

标签: python csv


【解决方案1】:

在 csv 文件中可以尝试的几件事:

  1. 将凌乱的字符串放入双引号“”“字符串”“”
  2. 在您凌乱的字段中的每一行的末尾使用 continue char \

【讨论】:

    猜你喜欢
    • 2018-11-17
    • 2020-05-21
    • 1970-01-01
    • 2013-06-23
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    相关资源
    最近更新 更多