Python:合并多个csv文件
Python:合并csv文件1. 一次性合并2个小csv文件,用pandas库(还可同时去掉重复表头)。import osimport pandas as pdpath = 'output/test/'for folder in os.listdir(path):try:folder_path = path + folder + '/'excels = [pd.read_csv(folder_pa
·
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~'
* 注意每个文件的路径
更多推荐
已为社区贡献2条内容
所有评论(0)