Python 如何将csv文件中的列附加到一个文件中?

Python 如何将csv文件中的列附加到一个文件中?,python,python-3.x,csv,scripting,Python,Python 3.x,Csv,Scripting,我正在用Python编写一个脚本。我有一堆csv文件,每个文件包含一列。这些文件可能是这样的: data = [] for f in filenames: # filled when creating files, you can use os.walk to fill yours with open(f) as r: data.append([x.strip() for x in r]) # data is a list of columns, we ne

我正在用Python编写一个脚本。我有一堆csv文件,每个文件包含一列。这些文件可能是这样的:

data = []
for f in filenames:       # filled when creating files, you can use os.walk to fill yours
    with open(f) as r:
        data.append([x.strip() for x in r])

# data is a list of columns, we need a list of list of columns, so we transpose the data:
transpose = zip(*data)

# write the joined file
import csv
with open("joined.txt","w", newline="") as j:
    w = csv.writer(j)
    w.writerows(transpose)
[['Header0', 'text_0_0', 'text_0_1', 'text_0_2', 'text_0_3', 'text_0_4'], # one files data
 ['Header1', 'text_1_0', 'text_1_1', 'text_1_2', 'text_1_3', 'text_1_4'], 
 ['Header2', 'text_2_0', 'text_2_1', 'text_2_2', 'text_2_3', 'text_2_4'], 
 ['Header3', 'text_3_0', 'text_3_1', 'text_3_2', 'text_3_3', 'text_3_4'], 
 ['Header4', 'text_4_0', 'text_4_1', 'text_4_2', 'text_4_3', 'text_4_4'], 
 ['Header5', 'text_5_0', 'text_5_1', 'text_5_2', 'text_5_3', 'text_5_4'], 
 ['Header6', 'text_6_0', 'text_6_1', 'text_6_2', 'text_6_3', 'text_6_4'], 
 ['Header7', 'text_7_0', 'text_7_1', 'text_7_2', 'text_7_3', 'text_7_4'], 
 ['Header8', 'text_8_0', 'text_8_1', 'text_8_2', 'text_8_3', 'text_8_4'], 
 ['Header9', 'text_9_0', 'text_9_1', 'text_9_2', 'text_9_3', 'text_9_4']]
FirstFile.csv

First
a
b
c
data = []
for f in filenames:       # filled when creating files, you can use os.walk to fill yours
    with open(f) as r:
        data.append([x.strip() for x in r])

# data is a list of columns, we need a list of list of columns, so we transpose the data:
transpose = zip(*data)

# write the joined file
import csv
with open("joined.txt","w", newline="") as j:
    w = csv.writer(j)
    w.writerows(transpose)
SecondFile.csv

Second
a2
b2
c2
data = []
for f in filenames:       # filled when creating files, you can use os.walk to fill yours
    with open(f) as r:
        data.append([x.strip() for x in r])

# data is a list of columns, we need a list of list of columns, so we transpose the data:
transpose = zip(*data)

# write the joined file
import csv
with open("joined.txt","w", newline="") as j:
    w = csv.writer(j)
    w.writerows(transpose)
我希望创建一个结果文件(我们称之为result.csv),该文件如下所示:

First    Second
a        a2
b        b2
c        c2
data = []
for f in filenames:       # filled when creating files, you can use os.walk to fill yours
    with open(f) as r:
        data.append([x.strip() for x in r])

# data is a list of columns, we need a list of list of columns, so we transpose the data:
transpose = zip(*data)

# write the joined file
import csv
with open("joined.txt","w", newline="") as j:
    w = csv.writer(j)
    w.writerows(transpose)
[('Header0', 'Header1', 'Header2', 'Header3', 'Header4', 'Header5', 'Header6', 'Header7', 'Header8', 'Header9'), 
 ('text_0_0', 'text_1_0', 'text_2_0', 'text_3_0', 'text_4_0', 'text_5_0', 'text_6_0', 'text_7_0', 'text_8_0', 'text_9_0'), 
 ('text_0_1', 'text_1_1', 'text_2_1', 'text_3_1', 'text_4_1', 'text_5_1', 'text_6_1', 'text_7_1', 'text_8_1', 'text_9_1'), 
 ('text_0_2', 'text_1_2', 'text_2_2', 'text_3_2', 'text_4_2', 'text_5_2', 'text_6_2', 'text_7_2', 'text_8_2', 'text_9_2'), 
 ('text_0_3', 'text_1_3', 'text_2_3', 'text_3_3', 'text_4_3', 'text_5_3', 'text_6_3', 'text_7_3', 'text_8_3', 'text_9_3'), 
 ('text_0_4', 'text_1_4', 'text_2_4', 'text_3_4', 'text_4_4', 'text_5_4', 'text_6_4', 'text_7_4', 'text_8_4', 'text_9_4')]

如何在python中将所有csv追加到一个目录中,并追加所有列,以便生成一个类似于此的result.csv(当然,还有更多列)?

您可以尝试使用Pandas

import pandas as pd
result = pd.concat([ pd.read_csv(f) for f in filenames ],axis=1)
result.to_csv("result.csv",index=False)
data = []
for f in filenames:       # filled when creating files, you can use os.walk to fill yours
    with open(f) as r:
        data.append([x.strip() for x in r])

# data is a list of columns, we need a list of list of columns, so we transpose the data:
transpose = zip(*data)

# write the joined file
import csv
with open("joined.txt","w", newline="") as j:
    w = csv.writer(j)
    w.writerows(transpose)
  • 创建文件名列表(例如,
    文件名
  • 进口大熊猫
  • 在列表理解中使用concat函数

  • 你可以试着用熊猫

    import pandas as pd
    result = pd.concat([ pd.read_csv(f) for f in filenames ],axis=1)
    result.to_csv("result.csv",index=False)
    
    data = []
    for f in filenames:       # filled when creating files, you can use os.walk to fill yours
        with open(f) as r:
            data.append([x.strip() for x in r])
    
    # data is a list of columns, we need a list of list of columns, so we transpose the data:
    transpose = zip(*data)
    
    # write the joined file
    import csv
    with open("joined.txt","w", newline="") as j:
        w = csv.writer(j)
        w.writerows(transpose)
    
  • 创建文件名列表(例如,
    文件名
  • 进口大熊猫
  • 在列表理解中使用concat函数

  • 您可以使用csv模块:

    data = []
    for f in filenames:       # filled when creating files, you can use os.walk to fill yours
        with open(f) as r:
            data.append([x.strip() for x in r])
    
    # data is a list of columns, we need a list of list of columns, so we transpose the data:
    transpose = zip(*data)
    
    # write the joined file
    import csv
    with open("joined.txt","w", newline="") as j:
        w = csv.writer(j)
        w.writerows(transpose)
    
    创建10个文件:

    filenames = []
    for i in range(10):
        filenames.append(f"file_{i}.txt")
        with open(filenames[-1],"w") as f:
            f.write(f"Header{i}\n")
            for row in range(5):
                f.write(f"text_{i}_{row}\n")
    
    data = []
    for f in filenames:       # filled when creating files, you can use os.walk to fill yours
        with open(f) as r:
            data.append([x.strip() for x in r])
    
    # data is a list of columns, we need a list of list of columns, so we transpose the data:
    transpose = zip(*data)
    
    # write the joined file
    import csv
    with open("joined.txt","w", newline="") as j:
        w = csv.writer(j)
        w.writerows(transpose)
    
    读取所有文件:

    data = []
    for f in filenames:       # filled when creating files, you can use os.walk to fill yours
        with open(f) as r:
            data.append([x.strip() for x in r])
    
    # data is a list of columns, we need a list of list of columns, so we transpose the data:
    transpose = zip(*data)
    
    # write the joined file
    import csv
    with open("joined.txt","w", newline="") as j:
        w = csv.writer(j)
        w.writerows(transpose)
    
    检查是否正常:

    data = []
    for f in filenames:       # filled when creating files, you can use os.walk to fill yours
        with open(f) as r:
            data.append([x.strip() for x in r])
    
    # data is a list of columns, we need a list of list of columns, so we transpose the data:
    transpose = zip(*data)
    
    # write the joined file
    import csv
    with open("joined.txt","w", newline="") as j:
        w = csv.writer(j)
        w.writerows(transpose)
    
    with open("joined.txt") as j:
        print(j.read())
    
    输出:

    data = []
    for f in filenames:       # filled when creating files, you can use os.walk to fill yours
        with open(f) as r:
            data.append([x.strip() for x in r])
    
    # data is a list of columns, we need a list of list of columns, so we transpose the data:
    transpose = zip(*data)
    
    # write the joined file
    import csv
    with open("joined.txt","w", newline="") as j:
        w = csv.writer(j)
        w.writerows(transpose)
    
    Header0,Header1,Header2,Header3,Header4,Header5,Header6,Header7,Header8,Header9
    text_0_0,text_1_0,text_2_0,text_3_0,text_4_0,text_5_0,text_6_0,text_7_0,text_8_0,text_9_0
    text_0_1,text_1_1,text_2_1,text_3_1,text_4_1,text_5_1,text_6_1,text_7_1,text_8_1,text_9_1
    text_0_2,text_1_2,text_2_2,text_3_2,text_4_2,text_5_2,text_6_2,text_7_2,text_8_2,text_9_2
    text_0_3,text_1_3,text_2_3,text_3_3,text_4_3,text_5_3,text_6_3,text_7_3,text_8_3,text_9_3
    text_0_4,text_1_4,text_2_4,text_3_4,text_4_4,text_5_4,text_6_4,text_7_4,text_8_4,text_9_4
    

    数据
    如下所示:

    data = []
    for f in filenames:       # filled when creating files, you can use os.walk to fill yours
        with open(f) as r:
            data.append([x.strip() for x in r])
    
    # data is a list of columns, we need a list of list of columns, so we transpose the data:
    transpose = zip(*data)
    
    # write the joined file
    import csv
    with open("joined.txt","w", newline="") as j:
        w = csv.writer(j)
        w.writerows(transpose)
    
    [['Header0', 'text_0_0', 'text_0_1', 'text_0_2', 'text_0_3', 'text_0_4'], # one files data
     ['Header1', 'text_1_0', 'text_1_1', 'text_1_2', 'text_1_3', 'text_1_4'], 
     ['Header2', 'text_2_0', 'text_2_1', 'text_2_2', 'text_2_3', 'text_2_4'], 
     ['Header3', 'text_3_0', 'text_3_1', 'text_3_2', 'text_3_3', 'text_3_4'], 
     ['Header4', 'text_4_0', 'text_4_1', 'text_4_2', 'text_4_3', 'text_4_4'], 
     ['Header5', 'text_5_0', 'text_5_1', 'text_5_2', 'text_5_3', 'text_5_4'], 
     ['Header6', 'text_6_0', 'text_6_1', 'text_6_2', 'text_6_3', 'text_6_4'], 
     ['Header7', 'text_7_0', 'text_7_1', 'text_7_2', 'text_7_3', 'text_7_4'], 
     ['Header8', 'text_8_0', 'text_8_1', 'text_8_2', 'text_8_3', 'text_8_4'], 
     ['Header9', 'text_9_0', 'text_9_1', 'text_9_2', 'text_9_3', 'text_9_4']]
    
    它看起来像:

    First    Second
    a        a2
    b        b2
    c        c2
    
    data = []
    for f in filenames:       # filled when creating files, you can use os.walk to fill yours
        with open(f) as r:
            data.append([x.strip() for x in r])
    
    # data is a list of columns, we need a list of list of columns, so we transpose the data:
    transpose = zip(*data)
    
    # write the joined file
    import csv
    with open("joined.txt","w", newline="") as j:
        w = csv.writer(j)
        w.writerows(transpose)
    
    [('Header0', 'Header1', 'Header2', 'Header3', 'Header4', 'Header5', 'Header6', 'Header7', 'Header8', 'Header9'), 
     ('text_0_0', 'text_1_0', 'text_2_0', 'text_3_0', 'text_4_0', 'text_5_0', 'text_6_0', 'text_7_0', 'text_8_0', 'text_9_0'), 
     ('text_0_1', 'text_1_1', 'text_2_1', 'text_3_1', 'text_4_1', 'text_5_1', 'text_6_1', 'text_7_1', 'text_8_1', 'text_9_1'), 
     ('text_0_2', 'text_1_2', 'text_2_2', 'text_3_2', 'text_4_2', 'text_5_2', 'text_6_2', 'text_7_2', 'text_8_2', 'text_9_2'), 
     ('text_0_3', 'text_1_3', 'text_2_3', 'text_3_3', 'text_4_3', 'text_5_3', 'text_6_3', 'text_7_3', 'text_8_3', 'text_9_3'), 
     ('text_0_4', 'text_1_4', 'text_2_4', 'text_3_4', 'text_4_4', 'text_5_4', 'text_6_4', 'text_7_4', 'text_8_4', 'text_9_4')]
    

    您可以使用csv模块:

    data = []
    for f in filenames:       # filled when creating files, you can use os.walk to fill yours
        with open(f) as r:
            data.append([x.strip() for x in r])
    
    # data is a list of columns, we need a list of list of columns, so we transpose the data:
    transpose = zip(*data)
    
    # write the joined file
    import csv
    with open("joined.txt","w", newline="") as j:
        w = csv.writer(j)
        w.writerows(transpose)
    
    创建10个文件:

    filenames = []
    for i in range(10):
        filenames.append(f"file_{i}.txt")
        with open(filenames[-1],"w") as f:
            f.write(f"Header{i}\n")
            for row in range(5):
                f.write(f"text_{i}_{row}\n")
    
    data = []
    for f in filenames:       # filled when creating files, you can use os.walk to fill yours
        with open(f) as r:
            data.append([x.strip() for x in r])
    
    # data is a list of columns, we need a list of list of columns, so we transpose the data:
    transpose = zip(*data)
    
    # write the joined file
    import csv
    with open("joined.txt","w", newline="") as j:
        w = csv.writer(j)
        w.writerows(transpose)
    
    读取所有文件:

    data = []
    for f in filenames:       # filled when creating files, you can use os.walk to fill yours
        with open(f) as r:
            data.append([x.strip() for x in r])
    
    # data is a list of columns, we need a list of list of columns, so we transpose the data:
    transpose = zip(*data)
    
    # write the joined file
    import csv
    with open("joined.txt","w", newline="") as j:
        w = csv.writer(j)
        w.writerows(transpose)
    
    检查是否正常:

    data = []
    for f in filenames:       # filled when creating files, you can use os.walk to fill yours
        with open(f) as r:
            data.append([x.strip() for x in r])
    
    # data is a list of columns, we need a list of list of columns, so we transpose the data:
    transpose = zip(*data)
    
    # write the joined file
    import csv
    with open("joined.txt","w", newline="") as j:
        w = csv.writer(j)
        w.writerows(transpose)
    
    with open("joined.txt") as j:
        print(j.read())
    
    输出:

    data = []
    for f in filenames:       # filled when creating files, you can use os.walk to fill yours
        with open(f) as r:
            data.append([x.strip() for x in r])
    
    # data is a list of columns, we need a list of list of columns, so we transpose the data:
    transpose = zip(*data)
    
    # write the joined file
    import csv
    with open("joined.txt","w", newline="") as j:
        w = csv.writer(j)
        w.writerows(transpose)
    
    Header0,Header1,Header2,Header3,Header4,Header5,Header6,Header7,Header8,Header9
    text_0_0,text_1_0,text_2_0,text_3_0,text_4_0,text_5_0,text_6_0,text_7_0,text_8_0,text_9_0
    text_0_1,text_1_1,text_2_1,text_3_1,text_4_1,text_5_1,text_6_1,text_7_1,text_8_1,text_9_1
    text_0_2,text_1_2,text_2_2,text_3_2,text_4_2,text_5_2,text_6_2,text_7_2,text_8_2,text_9_2
    text_0_3,text_1_3,text_2_3,text_3_3,text_4_3,text_5_3,text_6_3,text_7_3,text_8_3,text_9_3
    text_0_4,text_1_4,text_2_4,text_3_4,text_4_4,text_5_4,text_6_4,text_7_4,text_8_4,text_9_4
    

    数据
    如下所示:

    data = []
    for f in filenames:       # filled when creating files, you can use os.walk to fill yours
        with open(f) as r:
            data.append([x.strip() for x in r])
    
    # data is a list of columns, we need a list of list of columns, so we transpose the data:
    transpose = zip(*data)
    
    # write the joined file
    import csv
    with open("joined.txt","w", newline="") as j:
        w = csv.writer(j)
        w.writerows(transpose)
    
    [['Header0', 'text_0_0', 'text_0_1', 'text_0_2', 'text_0_3', 'text_0_4'], # one files data
     ['Header1', 'text_1_0', 'text_1_1', 'text_1_2', 'text_1_3', 'text_1_4'], 
     ['Header2', 'text_2_0', 'text_2_1', 'text_2_2', 'text_2_3', 'text_2_4'], 
     ['Header3', 'text_3_0', 'text_3_1', 'text_3_2', 'text_3_3', 'text_3_4'], 
     ['Header4', 'text_4_0', 'text_4_1', 'text_4_2', 'text_4_3', 'text_4_4'], 
     ['Header5', 'text_5_0', 'text_5_1', 'text_5_2', 'text_5_3', 'text_5_4'], 
     ['Header6', 'text_6_0', 'text_6_1', 'text_6_2', 'text_6_3', 'text_6_4'], 
     ['Header7', 'text_7_0', 'text_7_1', 'text_7_2', 'text_7_3', 'text_7_4'], 
     ['Header8', 'text_8_0', 'text_8_1', 'text_8_2', 'text_8_3', 'text_8_4'], 
     ['Header9', 'text_9_0', 'text_9_1', 'text_9_2', 'text_9_3', 'text_9_4']]
    
    它看起来像:

    First    Second
    a        a2
    b        b2
    c        c2
    
    data = []
    for f in filenames:       # filled when creating files, you can use os.walk to fill yours
        with open(f) as r:
            data.append([x.strip() for x in r])
    
    # data is a list of columns, we need a list of list of columns, so we transpose the data:
    transpose = zip(*data)
    
    # write the joined file
    import csv
    with open("joined.txt","w", newline="") as j:
        w = csv.writer(j)
        w.writerows(transpose)
    
    [('Header0', 'Header1', 'Header2', 'Header3', 'Header4', 'Header5', 'Header6', 'Header7', 'Header8', 'Header9'), 
     ('text_0_0', 'text_1_0', 'text_2_0', 'text_3_0', 'text_4_0', 'text_5_0', 'text_6_0', 'text_7_0', 'text_8_0', 'text_9_0'), 
     ('text_0_1', 'text_1_1', 'text_2_1', 'text_3_1', 'text_4_1', 'text_5_1', 'text_6_1', 'text_7_1', 'text_8_1', 'text_9_1'), 
     ('text_0_2', 'text_1_2', 'text_2_2', 'text_3_2', 'text_4_2', 'text_5_2', 'text_6_2', 'text_7_2', 'text_8_2', 'text_9_2'), 
     ('text_0_3', 'text_1_3', 'text_2_3', 'text_3_3', 'text_4_3', 'text_5_3', 'text_6_3', 'text_7_3', 'text_8_3', 'text_9_3'), 
     ('text_0_4', 'text_1_4', 'text_2_4', 'text_3_4', 'text_4_4', 'text_5_4', 'text_6_4', 'text_7_4', 'text_8_4', 'text_9_4')]
    

    我确信有更多的python方法,但这会起作用(只要所有文件的行数相同)

    data = []
    for f in filenames:       # filled when creating files, you can use os.walk to fill yours
        with open(f) as r:
            data.append([x.strip() for x in r])
    
    # data is a list of columns, we need a list of list of columns, so we transpose the data:
    transpose = zip(*data)
    
    # write the joined file
    import csv
    with open("joined.txt","w", newline="") as j:
        w = csv.writer(j)
        w.writerows(transpose)
    

    我确信有更多的python方法,但这会起作用(只要所有文件的行数相同)

    data = []
    for f in filenames:       # filled when creating files, you can use os.walk to fill yours
        with open(f) as r:
            data.append([x.strip() for x in r])
    
    # data is a list of columns, we need a list of list of columns, so we transpose the data:
    transpose = zip(*data)
    
    # write the joined file
    import csv
    with open("joined.txt","w", newline="") as j:
        w = csv.writer(j)
        w.writerows(transpose)
    

    如果您正在寻找纯python解决方案,最好是
    csv.DictReader
    csv.DictWriter
    ,这样您就可以更好地控制数据的格式化方式。此外,所有内容都是动态“生成”的,因此对于非常大的文件,它的内存效率会更高

    data = []
    for f in filenames:       # filled when creating files, you can use os.walk to fill yours
        with open(f) as r:
            data.append([x.strip() for x in r])
    
    # data is a list of columns, we need a list of list of columns, so we transpose the data:
    transpose = zip(*data)
    
    # write the joined file
    import csv
    with open("joined.txt","w", newline="") as j:
        w = csv.writer(j)
        w.writerows(transpose)
    
    import csv
    
    with open('csv1.csv') as csv1, open('csv2.csv') as csv2:
        r1 = csv.DictReader(csv1)
        r2 = csv.DictReader(csv2)
        with open('csv3.csv', 'w') as csv3:
            writer = csv.DictWriter(csv3, 
                fieldnames=["First", "Second"],
                lineterminator='\n'
            )
            writer.writeheader()
            writer.writerows({**x, **y} for x, y in zip(r1, r2))
    

    如果您正在寻找纯python解决方案,最好是
    csv.DictReader
    csv.DictWriter
    ,这样您就可以更好地控制数据的格式化方式。此外,所有内容都是动态“生成”的,因此对于非常大的文件,它的内存效率会更高

    data = []
    for f in filenames:       # filled when creating files, you can use os.walk to fill yours
        with open(f) as r:
            data.append([x.strip() for x in r])
    
    # data is a list of columns, we need a list of list of columns, so we transpose the data:
    transpose = zip(*data)
    
    # write the joined file
    import csv
    with open("joined.txt","w", newline="") as j:
        w = csv.writer(j)
        w.writerows(transpose)
    
    import csv
    
    with open('csv1.csv') as csv1, open('csv2.csv') as csv2:
        r1 = csv.DictReader(csv1)
        r2 = csv.DictReader(csv2)
        with open('csv3.csv', 'w') as csv3:
            writer = csv.DictWriter(csv3, 
                fieldnames=["First", "Second"],
                lineterminator='\n'
            )
            writer.writeheader()
            writer.writerows({**x, **y} for x, y in zip(r1, r2))
    

    您以前使用过熊猫吗?您尝试过任何东西吗?如果您没有大小问题(例如每个文件有一百万行或每列有非常大的值)。。。只需加载所有文件并将其写入新文件。查看模块
    csv
    ,以便于编写。或者使用pandas。@mad_u当可以使用
    csv
    模块和列表单独完成时,没有理由为此默认返回pandasfast@roganjosh你是怪物吗?为什么你不爱熊猫。他们很可爱!您以前使用过熊猫吗?您尝试过任何东西吗?如果您没有大小问题(例如每个文件有一百万行或每列有非常大的值)。。。只需加载所有文件并将其写入新文件。查看模块
    csv
    ,以便于编写。或者使用pandas。@mad_u当可以使用
    csv
    模块和列表单独完成时,没有理由为此默认返回pandasfast@roganjosh你是怪物吗?为什么你不爱熊猫。他们很可爱!我用csv做这个,转置(transpose=zip(*data))似乎不起作用。@yalpsideman它在我发布的代码中起作用。zip()生成一个只能使用一次的生成器—如果您先打印它,然后在您要写入文件时将其清空。使用
    transpose=list(zip(*data))
    从转置的值创建一个列表,这样您就可以多次使用它们进行打印和文件写入。我正在使用csv进行此操作,而转置(transpose=zip(*data))似乎不起作用。@yalpsideman它在我发布的代码中起作用。zip()生成一个只能使用一次的生成器—如果您先打印它,然后在您要写入文件时将其清空。使用
    transpose=list(zip(*data))
    从转置的值创建一个列表,以便可以多次使用它们进行打印和文件写入