从CSV数据写入多个文本文件

时间:2019-09-11 05:47:50

标签: python

因此,我创建了一个程序来从.csv文件中获取数据,并将其导出为可以在自建Wiki上使用的格式。

因此,每次添加新行时,代码显然都会循环。

我想在循环时必须使用从.txt文件中获得的名称将数据写入新的.txt文件中。请协助这样做

因此,我尝试将整个代码定义为可验证的并将其导出,但是我对Python确实并不熟悉,希望您的帮助...

import csv

with open('123.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        if line_count == 0:
            print(f'Column names are {", ".join(row)}')
            line_count += 1
        else:
            print(f'========{row[7]} - {row[1]}========')
            line_count += 1
        #Info
            print(f'===General Info===')
            line_count += 1
            print(f'Coordinates: {row[5]}  {row[10]}')
            line_count += 1
        if row[8] == 'n':
            print(f'Eskom Site')
        else:
            print(f'Solar Site')
            line_count += 1
    print(f'Processed {line_count} lines.')

输入.csv文件

No,Site,Rek Nr,Naam ,Nommer,Coordinates,Sectors,No,Solar Site,Camera,,Monitor Address
1,Nedbank,,,,1,35,128,n,,\\,n
2,Silicon,,,,2,16,129,n,,\\,n
3,Twilight,,,,3,17,130,n,,\\,n
4,Percy,,,,4,20,131,n,,\\,n
5,Rey,,,,5,13,132,n,,\\,n

输出:

========128 - Nedbank========
===General Info===
Coordinates: 1  \\
Eskom Site
========129 - Silicon========
===General Info===
Coordinates: 2  \\
Eskom Site
========130 - Twilight========
===General Info===
Coordinates: 3  \\
Eskom Site
========131 - Percy========
===General Info===
Coordinates: 4  \\
Eskom Site
========132 - Rey========
===General Info===
Coordinates: 5  \\
Eskom Site

因此,每当一行从======== x-x ========开始时,它都必须将其写入新的txt文件中

1 个答案:

答案 0 :(得分:0)

修改了代码,以将打印语句写入.txt文件中。 注意:这会将所有内容写入单个文件。

import csv

with open('123.csv', 'r') as csv_file, open("xyz.txt", "w") as txt_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        if line_count == 0:
            txt_file.write('Column names are { %s }\n'%(','.join(row)))
        else:
            txt_file.write('========{} - {}========\n'.format(row[7], row[1]))
        #Info
            txt_file.write('===General Info===\n')
            txt_file.write('Coordinates: {}  {}\n'.format(row[5], row[10]))
        if row[8] == 'n':
            txt_file.write('Eskom Site\n')
        else:
            txt_file.write('Solar Site\n')
        line_count += 1
    txt_file.write('Processed {} lines.\n'.format(str(line_count)))

但是,如果要将.csv文件的每一行(这有点令人难以理解,为什么!)写入到单独的文件中,则只需调整上面的程序即可做到这一点-

import csv

with open('123.csv', 'r') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        new_file_name = row[8] + ' - ' + row[4]
        with open(new_file_name, 'a+') as txt_file:
            if line_count == 0:
                txt_file.write('Column names are { %s }\n'%(','.join(row)))
            else:
                txt_file.write('========{} - {}========\n'.format(row[7], row[1]))
                #Info
                txt_file.write('===General Info===\n')
                txt_file.write('Coordinates: {}  {}\n'.format(row[5], row[10]))
            if row[8] == 'n':
                txt_file.write('Eskom Site\n')
            else:
                txt_file.write('Solar Site\n')
            line_count += 1
        #txt_file.write('Processed {} lines.\n'.format(str(line_count)))

在处理完所有行之后,您要写入哪个文件的最后一条语句-Processed {} lines?如果要将其写入另一个文件,可以执行此操作-

last_file_name = str(line_count)
with open(last_file_name, 'w') as txt_file:
    txt_file.write('Processed {} lines.\n'.format(str(line_count)))