Python:合并csv文件

1. 一次性合并2个小csv文件,用pandas库(还可同时去掉重复表头)。

import os
import pandas as pd

path = 'output/test/'

for folder in os.listdir(path):
    try:
        folder_path = path + folder + '/'
        excels = [pd.read_csv(folder_path + fname) for fname in os.listdir(folder_path) if 'csv' in fname]
        df = pd.concat(excels)
        excel_name = 'output/test/' + str(folder) + '.csv'
        df.to_csv(excel_name)
    except Exception as e:
        print folder + str(e)

2. 合并多个csv文件,用csv库

import os
import csv

path = 'output/test/'
for files in os.listdir(path):
    datas = []
    # 读取csv文件
    for fname in os.listdir(path):
        if 'csv' in fname:
            fname_path = path + fname
            with open(fname_path, "r") as csvfile:
                reader = csv.reader(csvfile)
                # 去掉表头(第一行)
                reader = list(reader)[1:]
                for line in reader:
                    datas.append(line)
excel_name = 'output/out.csv'
csv_head = [
    'name',
    'aaa',
    'bbb',
    'ccc',
]

with open(excel_name, 'w') as csvfile2:
    writer = csv.writer(csvfile2)
    # 写表头
    writer.writerow(csv_head)
    writer.writerows(datas)

print 'finish~'

* 注意每个文件的路径

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐