我有一个输入文本文件,每行格式为
Line[X]: [AAA] [BBB] [CCC] :1234
我想使用" :"作为分隔符并将每列写入excel文件。我尝试了以下代码,但不确定这是否是正确的方法。任何投入都受到高度赞赏。
import csv
Text_File = open("some_text_file.txt", "w+")
csv_results = open ("Results.csv", 'w')
for eachline in Text_File:
csv_results.writer(Text_File, delimiter ='**:**',quotechar='**:**', quoting=csv.QUOTE_MINIMAL)
谢谢!
答案 0 :(得分:0)
import csv
txt = open("some_text_file.txt", "w+")
csv_results = open ("Results.csv", 'w')
text_reader = reader(txt)
#this delimiter is for your output file to be read by excel
csv_results.writer(Text_File, delimiter =',')
for line in txt:
split_line = line.split(":")
csv_results.writerow(split_line)
txt.close()
csv_results.close()
应该按照您的意愿行事。如果要删除第二列任一侧的空格,可以使用正则表达式。