逐行读取/保存数据到 csv


import csv
def read_file():

    file_path = 'abc.txt'
    save_path = '001.csv'

    out = open(save_path, 'a', newline='')
    csv_write = csv.writer(out, dialect='excel')

    i = 0
    for line in open(file_path):
        print('i : ', i)
        arr = line.split('-')
        src = arr[0]
        tgt = arr[1] 
        desc = arr[2] 

        data = [src, tgt, desc]
        csv_write.writerow(data)

        i += 1

你可能感兴趣的:(Python)