1 回答
TA贡献1872条经验 获得超3个赞
您可以预处理到适当的 TSV,然后从那里读取它。用于itertools.groupby查找“\N”结尾。如果此文件存在其他问题,例如内部选项卡未转义,则一切都将失败。
import itertools
import re
separator_re = re.compile(r"\s*\\N\s*$", re.MULTILINE)
with open('other.csv') as infp:
with open('other-conv.csv', 'w') as outfp:
for hassep, subiter in itertools.groupby(infp, separator_re.search):
if hassep:
outfp.writelines("{}\n".format(separator_re.sub("",line))
for line in subiter)
else:
for line in subiter:
if line.endswith("\\\n"):
line = line[:-2] + " "
else:
line = line.strip()
outfp.write(line)
添加回答
举报