txt_to_csv

 1 import csv
 2 
 3 def txt2csv(inf, outf):
 4     with open(inf,'r') as fin, open(outf,'w',newline='') as fout:
 5         wrt = csv.writer(fout)
 6         lines = fin.readlines()
 7         for line in lines:
 8             txt = line.strip().split()
 9             wrt.writerow(txt)
10             
11 
12 inf = 'train.txt'
13 outf = 'train.csv'
14 txt2csv(inf,outf)

txt 格式转 csv ,字段中间以空格键间隔

原文地址:https://www.cnblogs.com/hfdkd/p/8652262.html