欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

txt转CSV格式文件python

程序员文章站 2022-03-03 16:13:24
...
#-*-coding:utf-8 -*-

import csv
with open('file.csv', 'wb') as csvfile:
    spamwriter = csv.writer(csvfile, dialect='excel')
    # 读要转换的txt文件,文件每行各词间以@@@字符分隔
    with open('C:\\Users\\1701\Desktop\\20191122 104216.txt', 'rb') as filein:
        for line in filein:
            line_list = line.decode().strip().split(",")
            print(line_list[0:5])
            A=pd.DataFrame(line_list)
            print(A.head())
            A.to_csv('C:\\Users\\1701\Desktop\\file.csv')