csv 如何将txt文件转换成csv文件

时间:2023-03-09 17:16:14
csv 如何将txt文件转换成csv文件
import csv
def convert_txt_to_csv(out_file_path, input_file_path, txt_sep): #定义输出路径,输入文件路径,txt的分隔符
with open("./temp.csv", "w", newline = "") as csv_file:
writer = csv.writer(csv_file)
mark = 0
with open(temp_path) as f:
for l in f:
row = l.split(txt_sep) #txt的分割名为
if(mark==0):
header = range(len(row))
writer.writerow(header) #写入列名
mark = 1 writer.writerow(row)