#1创建txt 文件并写入相应的内容
import csv
with open(r'C:\Users\haoran\Desktop\temp.txt','w',encoding='utf-8') as f:
f.write('name,sex'+'\n'+'攀攀,男')
f.close()
#2 打开创建好的TXT文件, 创建CSV 文件,把TXT的文件逐行读取并写入到CSV下。
f = open(r'C:\Users\haoran\Desktop\temp.txt','r',encoding='utf-8')
f1 = open(r'C:\Users\haoran\Desktop\temp.csv','w',encoding="utf-8",newline='')
writer = csv.writer(f1)
for r in f:
contents = r.split(',')
writer.writerow((contents[0],contents[1]))
f.close()
f1.close()