在 python 中将多个 CSV 行合并为 1

Concat multiple CSV rows into 1 in python

我正在尝试联系 CSV 行。我尝试将 CSV 行转换为 pandas 列表,但由于某些文件为空,它会附加 'nan' 值。 另外,我尝试使用 zip 但它会连接列值。

    with open(i) as f:
        lines = f.readlines()
        res = ""
        for i, j in zip(lines[0].strip().split(','), lines[1].strip().split(',')):
            res += "{} {},".format(i, j)
            print(res.rstrip(','))
        for line in lines[2:]:
            print(line)

我有如下数据,

输入数据:- Input CSV Data

预期输出:- Output CSV Data

行数超过3行,这里只给出示例。 建议一种无需创建新文件即可完成上述任务的方法。请指出任何特定的功能或示例代码。

只要文件中没有发生任何奇怪的事情,像这样的东西应该可以工作:

with open(i) as f:
  result = []
  for line in f:
    result += line.strip().split(',')
  print(result)

这假定您的第一行包含正确数量的列。它将读取整个文件,忽略空数据(",")并累积足够的数据点来填充一行,然后切换到下一行:

写入测试文件:

with open ("f.txt","w")as f:
    f.write("""Circle,Year,1,2,3,4,5,6,7,8,9,10,11,12
abc,2018,,,,,,,,,,,,
2.2,8.0,6.5,9,88,,,,,,,,,,
55,66,77,88,,,,,,,,,,
5,3.2,7

def,2017,,,,,,,,,,,,
2.2,8.0,6.5,9,88,,,,,,,,,,
55,66,77,88,,,,,,,,,,
5,3.2,7

""")

处理测试文件:

data = [] # all data
temp = [] # data storage until enough found , then put into data 

with open("f.txt","r") as r:
    # get header and its lenght
    title = r.readline().rstrip().split(",")
    lenTitel = len(title)
    data.append(title)

    # process all remaining lines of the file
    for l in r:
        t = l.rstrip().split(",") # read one lines data
        temp.extend( (x for x in t if x) ) # this eliminates all empty ,, pieces even in between 
        # if enough data accumulated, put as sublist into data, keep rest
        if len (temp) > lenTitel:
            data.append( temp[:lenTitel] )
            temp = temp [lenTitel:]
    if temp:
        data.append(temp)

print(data)

输出:

[['Circle', 'Year', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'], 
 ['abc', '2018', '2.2', '8.0', '6.5', '9', '88', '55', '66', '77', '88', '5', '3.2', '7'], 
 ['def', '2017', '2.2', '8.0', '6.5', '9', '88', '55', '66', '77', '88', '5', '3.2', '7']]

备注:

  • 您的文件不能有前导换行符,否则标题的大小不正确。
  • 中间的换行不会造成伤害
  • 你不能有 "empty" 个细胞 - 它们会被淘汰