Python csv 重新排列列并从字典调用(否 pandas)

Python csv rearrange columns and calling from a dictionary (no pandas)

我需要代码在不使用 Pandas 的情况下更改 header 名称时重新排列列。我还需要省略新的 csv 文件中的一些列。

示例输入 csv 文件:

    Alfa,Beta,Charlie,Delta,Echo,Foxtrot,Golf,Hotel,India,Juliett,Kilo
    A1,B1,C1,D1,E1,F1,G1,H1,I1,J1,K1
    A2,B2,C2,D2,E2,F2,G2,H2,I2,J2,K2
    A3,B3,C3,D3,E3,F3,G3,H3,I3,J3,K3
    A4,B4,C4,D4,E4,F4,G4,H4,I4,J4,K4
    A5,B5,C5,D5,E5,F5,G5,H5,I5,J5,K5
    A6,B6,C6,D6,E6,F6,G6,H6,I6,J6,K6
    A7,B7,C7,D7,E7,F7,G7,H7,I7,J7,K7
    A8,B8,C8,D8,E8,F8,G8,H8,I8,J8,K8
    A9,B9,C9,D9,E9,F9,G9,H9,I9,J9,K9

我目前拥有的:

import csv

#Opens and readers csv file
open_cd_csv = open("book1.csv", "r", encoding="utf-8", errors='ignore')
reader_cd_csv = csv.DictReader(open_cd_csv, delimiter=',', quotechar='"')
header_csv = next(reader_cd_csv)

#dictionary sample
dictionary_sample = {
    "Beta_New" : reader_cd_csv["Beta"],
    "Echo_New" : reader_cd_csv["Echo"],
    "Foxtrot_New_All" : reader_cd_csv["Foxtrot"],
    "Hotel_New" : reader_cd_csv["Hotel"],
    "India_New" : reader_cd_csv["India"],
    "Charlie_New" : reader_cd_csv["Charlie"],
    }
    
#Opens and writes csv file
output_test_csv_file = "xtest_file.csv"
open_output_test_csv = open(output_test_csv_file, "w", encoding="utf-8", errors='ignore')
writer_output_test_csv = csv.writer(open_output_test_csv, delimiter=',', quotechar='"')


sample_list = []
for row in reader_cd_csv:
    sample_list.append(dictionary_sample(row))
    writer_output_test_csv.writerow(sample_list)

输出 csv 应该是什么样子:

Beta_New,Echo_New,Foxtrot_New_ALL,Hotel_New,India_New,Charlie_New
B1,E1,F1,H1,I1,C1
B2,E2,F2,H2,I2,C2
B3,E3,F3,H3,I3,C3
B4,E4,F4,H4,I4,C4
B5,E5,F5,H5,I5,C5
B6,E6,F6,H6,I6,C6
B7,E7,F7,H7,I7,C7
B8,E8,F8,H8,I8,C8
B9,E9,F9,H9,I9,C9

你想要这样的东西:

import csv

fieldnames_dict = {
    'Beta': 'Beta_New',
    'Echo': 'Echo_New',
    'Foxtrot': 'Foxtrot_New_ALL',
    'Hotel': 'Hotel_New',
    'India': 'India_New',
    'Charlie': 'Charlie_New'
}

with open("book1.csv", "r", encoding="utf-8", errors='ignore') as csv_in:
    with open("xtest_file.csv", "w", encoding="utf-8", errors='ignore') as csv_out:
        reader = csv.DictReader(csv_in, delimiter=',', quotechar='"')
        writer = csv.DictWriter(csv_out, delimiter=',', quotechar='"',
                                fieldnames=list(fieldnames_dict.values()))
        writer.writeheader()
        for row_in in reader:
            row_out = {new: row_in[old] for old, new in fieldnames_dict.items()}
            writer.writerow(row_out)

请注意,您的行 header_csv = next(reader_cd_csv) 不应该存在,因为它跳过了第一行数据,而不是 header 行,该行会自动读取以了解列键。

另请注意,您像函数一样使用 dictionary_sample,但它是一个 dict – 一个不起作用的字典,因为 reader_cd_csv 是一个 DictReader , 不能用方括号索引。

with statement 中打开文件是一种很好的做法,它提供了一个上下文,该上下文会导致文件在退出上下文时关闭。在这种情况下,阅读和写作的上下文是嵌套的,因为我选择阅读和写作“同时”完成。

我用了一个DictWriter for the output, which is the counterpart of the DictReader。请参阅文档以了解如何使用两者。除此之外所需要做的就是将每一行的输入字典转换为相应的输出字典,这是我用字典理解完成的。