Python_将txt文件转换为csv文件

import csv

out = open('data.csv', 'w', newline='')
csv_writer = csv.writer(out, dialect='excel')

f = open("data.txt", "r",encoding='utf-8')
for line in f.readlines():
    list = line.split()  # 将字符串转为列表,从而可以按单元格写入csv
    csv_writer.writerow(list)

你可能感兴趣的:(python,开发语言)