将来自不同CSV的列合并为单个文件

时间:2018-03-17 13:37:58

标签: python csv merge

基本上我有2个csv文件如下:

File 1:      File 2:          Current output:
   Num          Num2          Num
    1            1             1
    2            2             2
    3            3             3
    4            4             4
                                  Num2
                                   1
                                   2
                                   3
                                   4

我想将它们合并到一个csv文件中,如下所示:

Expected File 3:
    Num Num2
    1   1
    2   2
    3   3
    4   4

但是,当我合并文件时,它从文件1数据的底部开始。如何使它们从第2列第1行开始,而不是从下面开始。

inputs = ["asd.csv", "b.csv"]  # etc

# First determine the field names from the top line of each input file
# Comment 1 below
fieldnames = []
for filename in inputs:
  with open(filename, "r", newline="") as f_in:
    reader = csv.reader(f_in)
    headers = next(reader)
    for h in headers:
      if h not in fieldnames:
        fieldnames.append(h)

# Then copy the data
with open("out.csv", "w", newline="") as f_out:   # Comment 2 below
  writer = csv.DictWriter(f_out, fieldnames=fieldnames)
  for filename in inputs:
    with open(filename, "r", newline="") as f_in:
      reader = csv.DictReader(f_in)  # Uses the field names in this file
      for line in reader:
        # Comment 3 below
        writer.writerow(line)

4 个答案:

答案 0 :(得分:0)

您可以使用Track

zip

输出:

import csv
inputs = ["asd.csv", "b.csv"]
new_data = [filter(None, a+b) for a, b in zip(*[list(csv.reader(open(i))) for i in inputs])]
with open('filename.csv', 'w') as f:
  write = csv.writer(f)
  write.writerows(new_data)

答案 1 :(得分:0)

使用pandas,

import pandas as pd
inputs = ["asd.csv", "b.csv"]
df1=pd.read_csv(inputs[0])
df2=pd.read_csv(inputs[1])
df3["Num1 Num2"]= df1["Num1"]+" "+df2["Num2"] 
df3.to_csv("your_output_path")

答案 2 :(得分:0)

使用pandas的解决方案:

    model1 = "Pro Cloud "
    model2 = "Pro Cloud2"


    [model1 : settings]
    sscs_protocol = "http"
    sscs_server = "localhost"
    sscs_port = "804"
    sscs_db_alias = "MY_SERVER"
    auth_code = ""
    login_prompt = "false"
    default_diagram = ""
    show_discuss = "false"

答案 3 :(得分:0)

以下方法应该运作良好:

from itertools import zip_longest    
import csv

data = []    

for csv_filename in ['asd.csv', 'b.csv']:
    with open(csv_filename, 'r', newline='') as f_input:
        data.append([row[0] for row in csv.reader(f_input)])

with open('out.csv', 'w', newline='') as f_output:        
    csv.writer(f_output).writerows(zip_longest(*data))

它首先从您的文件列表中读取单个列到单个data列表中,然后使用zip_longest将它们组合在一起以创建输出CSV文件。

通过使用zip_longest,它可以处理您的文件列表恰好包含不同行数的情况。

相关问题