合并不同的CSV文件

时间:2018-02-27 21:53:31

标签: python csv merge append concatenation

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

 Select * from Table Where col1 = "city_2" or col2 = "city_2" ....

合并:

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

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

    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文件与所有文件合并。

我该怎么做:

    Name1_Total_A.csv
    Name2_Total_A.csv

其中Name1_Total_A.csv按Name1_Stuff1_A.csv,Name1_Stuff2_A.csv和Name1_Stuff3_A.csv的顺序合并,其他文件也是如此

谢谢!

2 个答案:

答案 0 :(得分:1)

您可以按如下方式压缩代码:

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())

itertools.product()是编写嵌套for循环的另一种方法。尝试添加一些print语句,看看它是如何工作的。

答案 1 :(得分:0)

我想我得到了答案,但这太乏味了。有没有办法让它更有效率?感谢。

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)