Python_合并多个文件夹下的多个csv文件

# -*- coding:utf8 -*-
import os.path
import os
import csv
import re

path = "D:\Datebase\data1\DataChallengeOne"
#i = 0
files = os.listdir(path)
with open('Email.csv', 'w+',newline ='', encoding='UTF-8') as csvfile:
    writer = csv.writer(csvfile)
    for f in files:  # f是倒数第二级文件夹eg:2017-11-01的列表
        #i = i + 1
        print(f)
        if (os.path.isdir(path + '/' + f)):  # 判断是否是文件夹
            if (f[0] == '.'):  # 排除隐藏文件夹
                pass
            else:  # 添加非隐藏文件
                for filenames in os.listdir(path + '/' + f):  # filenames是3三级文件的一个列表
                    print(filenames)
                    path_1 = os.path.join(path ,f ,filenames )#路径合成
                    if (filenames == "email.csv"):
                        with open(path_1, 'r+', encoding='UTF-8') as fr:
                            reader = csv.reader(fr, dialect='excel', delimiter=',' )  # 读取文件到list中
                            for row in reader:  # 读取list中每一行
                                if 'sport' in row:
                                    pass
                                else:
                                    if ';' in row[7]:  # 判断是否符合划分
                                        a = re.split(';', row[7])  # 拆分的值存储起来
                                        #print (a)
                                        for i in a:
                                            row[7] = i
                                            #print (row[7])
                                            writer.writerow([row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7],row[8]])
                                    else:
                                        writer.writerow([row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8]])
                    else:
                        continue
必须是先把csv文件转换为utf-8的格式才能转换成功


你可能感兴趣的:(Python,chinavis2018,csv)