学习的过程是艰苦的
import csv import re def txt_to_csv(txt_file, csv_file): with open(txt_file, 'r', encoding='utf-8') as txtfile, open(csv_file, 'w', newline='', encoding='utf-8') as csvfile: csv_writer = csv.writer(csvfile) csv_writer.writerow(["o", "i", "t"]) for i, line in enumerate(txtfile, 1): columns = re.split(r'\s+', line.strip(), maxsplit=2) column1 = "。" print(columns) column2 = columns[1] column3 = columns[2] csv_writer.writerow([column1, column2, column3])
要慢慢体会