输入:csv文件。 采样数据如何使用python将多行csv文件放入单行中
id,name,gnd,address,second_name.
"100","xx","m","plot y zzzz city","zx
yddfe"
输出:csv文件。
id,name,gnd,address,second_name.
"100","xx","m","plot y zzzz city","zxyddfe"
在源文件最后一列数据被分成两行,我想成单行来加载数据。
我尝试:
import csv
with open("out.csv","w") as fout:
with open("sample.csv")as f:
firsttline = ""
for line in f:
if not line.startswith('"'):continue
else:
line = line.rstrip('\n')
firstline = firstline + '\n'+ line
print >> fout, firstline
with open ("out.csv") as fin:
with open("out1.csv","w") as fout1:
for line in fin:
if not line.isspace():
fout1.write(line)
使用此代码,我没有得到正确的记录计数后。一些列的数据丢失了。请帮助我。
通过执行“继续”,您将错过部分数据。在这个例子中是'yddfe'。做'firstline = firstline + line.rstrip('\ n')'而不用'\ n'之间。 – SBylemans
为什么不使用csv模块自动处理引用字段内的新行? –