Python,文件处理

Python,file handling

import csv
from _csv import writer
f1=open("formatted.txt")
with open("write.csv","wb") as f:
    writer = csv.writer(f, lineterminator='\n')
    reader=csv.reader(f)
    for line in f1:
        writer.writerow(line)

我有一些制服记录的文本文件。当我试图将其放入 excel 文件时,我将每个单元格中的每个字符分开。 我们如何确保每个指定的值都在单独的列中?

input text file

output file

试试这个:

from csv import writer

f1 = open('formatted.txt').read()

with open("write.csv", "w+") as csv_file:
    writer = writer(csv_file)
    for line in f1.split('\n'):
        writer.writerow(line.split())