1.
fp =open(r'C:\Users\宇豪\Desktop\test.txt','a')
fp.write('name,sex'+'\n'+'攀攀,男')
fp.close()
2.
importcsv
fp1 =open(r'C:\Users\宇豪\Desktop\test.txt','r')
fp2 =open(r'C:\Users\宇豪\Desktop\test.csv','w',newline='')
writer = csv.writer(fp2)
forrinfp1:
contents = r.split(',')
writer.writerow((contents[0],contents[1]))
fp1.close()
fp2.close()