- #-*-coding:utf-8 -*-
-
- import csv
- import time
-
- def getLineData(constant):
- # 读要转换的txt文件
- with open('python测试数据.txt', 'r', encoding='utf-8') as filein:
- for line in filein:
- a, b = line.strip('\n').split(',')
- yield a, b[b.rfind('/')+1:], constant
-
- def saveDataToCsv(fileName):
-
- constant = time.strftime('%Y%m%d') + '-xl'
-
- with open(fileName, 'w', newline='' ) as csvfile:
- spamwriter = csv.writer(csvfile, dialect='excel')
- for line in getLineData(constant):
- spamwriter.writerow(line)
-
- if __name__ == "__main__":
-
- fileName = 'File.csv'
- saveDataToCsv(fileName)
复制代码 回复 6# superman |