輸入: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模塊自動處理引用字段內的新行? –