如何使用python将多行csv文件带入单行

时间:2017-09-26 07:15:12

标签: 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)

使用此代码后,我没有获得正确的记录计数。缺少一些列的数据。请帮助我。

1 个答案:

答案 0 :(得分:0)

这样可以解决问题:

import pandas as pd
df = pd.DataFrame.from_csv('data.csv')

输出看起来像

    name gnd           address  second_name.
id                                          
100   xx   m  plot y zzzz city  zx\r\n yddfe

有关pandas的更多信息。 使用pandas读取和写入.csv文件非常容易。我总是更喜欢它们而不是csv库。