合并不同的CSV文件

2024-09-22 20:37:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我想根据文件名的条件合并不同的文件。例如

Name1_Stuff1_A.csv  
Name1_Stuff1_B.csv  
Name1_Stuff2_A.csv    
Name1_Stuff2_B.csv    
Name1_Stuff3_A.csv  
Name1_Stuff3_B.csv 

合并:

Name1_Stuff1_A + Name1_Stuff2_A + Name1_Stuff3_A -> Name1_Total_A  
Name1_Stuff1_B + Name1_Stuff2_B + Name1_Stuff3_B -> Name1_Total_B  
Then move on to another name, e.g., Name2, and so on

我试过:

    for name in names:
        with open('{}_Total_A.csv'.format(name), 'a') as merged_file:
            for file in glob.glob('*.csv'):
                for line in open(file, 'r'):
                    merged_file.write(line)

但它只返回A(没有B):

    Name1_Total_A.csv
    Name2_Total_A.csv

A文件与所有文件合并。你知道吗

我该怎么做:

    Name1_Total_A.csv
    Name2_Total_B.csv
    Name1_Total_A.csv
    Name2_Total_B.csv

其中Name1\u Total\u A.csv按Name1\u Stuff1\u A.csv、Name1\u Stuff2\u A.csv和Name1\u Stuff3\u A.csv的顺序合并,其他文件也是如此

谢谢!你知道吗


Tags: 文件csvnameinforonopenmerged
2条回答

我想我知道答案了,但太乏味了。有没有办法提高效率?谢谢。你知道吗

for each_name in names:
    with open('/{}_Total_A.csv'.format(each_name), 'a') as merged_file:
        stuff1 = open('/{}_Stuff1_A.csv'.format(each_name), 'r').read()
        merged_file.write(stuff1)
        stuff2 = open('/{}_Stuff2_A.csv'.format(each_name), 'r').read()
        merged_file.write(stuff2)
        stuff3 = open('/{}_Stuff3_A.csv'.format(each_name), 'r').read()
        merged_file.write(stuff3)

    with open('/{}_Total_B.csv'.format(each_name), 'a') as merged_file:
        stuff1 = open('/{}_Stuff1_B.csv'.format(each_name), 'r').read()
        merged_file.write(stuff1)
        stuff2 = open('/{}_Stuff2_B.csv'.format(each_name), 'r').read()
        merged_file.write(stuff2)
        stuff3 = open('/{}_Stuff3_B.csv'.format(each_name), 'r').read()
        merged_file.write(stuff3)

您可以将代码压缩如下:

from itertools import product

for name, ab in product(range(1, 4), ['A', 'B']):
    with open('Name{}_Total_{}.csv'.format(name, ab), 'a') as merged_file:
        for stuff in range(1, 4):
            with open('/Name{}_Stuff{}_{}.csv'.format(name, stuff, ab), 'r') as f_input:
                merged_file.write(f_input.read())

^{}是编写嵌套for循环的另一种方法。尝试添加一些print语句,看看它是如何工作的。你知道吗

相关问题 更多 >