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 = []
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~'
* 注意每个文件的路径