【发布时间】:2014-01-23 09:58:39
【问题描述】:
我正在使用 unicodecsv 在 Python 中循环遍历 csv 文件的行。 我的 CSV 文件中的所有字符串都在引号之间,但 csv 阅读器仍将换行符视为行分隔符。
这是我的代码:
with open(path, mode='rU') as f:
reader = unicodecsv.reader(f, delimiter=b',', quoting=csv.QUOTE_MINIMAL, quotechar=b'"', lineterminator="\n")
for count, row in enumerate(reader):
if count < row_offset:
continue
record = {}
for col, mapper in enumerate(mappers):
...
...
这是一个 csv 行的示例:
"test","this line will
continue on the next line","another column",
由于某种原因,读者会将其解读为 2 行而不是 1 行。
编辑
新的示例 CSV 行:
628,2012-07-27 01:59:32,000445,MARC,525,"HE547 ","1",2012-07-27,,,,,,,,"This is an example, this is a test line.
new line but it is in the same csv line, followed by some enters!
",
【问题讨论】:
-
这是 Python 3 还是 Python 2?您在这里使用
b''字符串文字。 -
你能至少告诉我们这两行返回的是什么吗?随意 futz 内容,但保留初始空格和引号字符。
-
@MartijnPieters:解析器返回一个越界异常,因为换行时没有足够的列。这只发生在该列中有输入的行。所有其他行均已正确处理。
标签: python csv unicode newline