CSV文件拆分_行拆分

#先要把CSV文档保存为utf-8格式
import os.path
import os
import csv
import re

with open('Email.csv', 'w+',newline ='', encoding='UTF-8') as csvfile:
   writer = csv.writer(csvfile)
   with open("D:\Datebase\data1\Edata\email.csv",'r+', encoding='UTF-8') as f:
      reader = csv.reader(f, dialect='excel', delimiter=',')  # 读取文件到list中
      for row in reader:           # 读取list中每一行
         # print (row[1] )
         if ';' in row[7]:     # 判断是否符合划分
            a = re.split(';', row[7])   # 拆分的值存储在lise  a 中起来
            #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:#不划分就直接录入新的csv文件中
            writer.writerow([row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8]])
阅读更多
换一批

没有更多推荐了,返回首页