python语言--------csv模块将txt文件转换为表格csv格式(常用、掌握)

上代码:
import csv

csvFile = open("C:/Users/Administrator/Desktop/1.csv", 'w', newline='', encoding='utf-8')
writer = csv.writer(csvFile)
csvRow = []

f = open("C:/Users/Administrator/Desktop/np.txt", 'r', encoding='GB2312')
for line in f:
csvRow = line.split()
writer.writerow(csvRow)

f.close()
csvFile.close()
实现效果如下:

 

 

posted @ 2023-08-09 20:29  往事已成昨天  阅读(123)  评论(0编辑  收藏  举报