Python csv 字典从字典调用以根据字典重复 csv 中的行

Python csv dictionary calling from a dictionary to repeat rows in a csv according to dictionary

大家好,我有一个关于我遇到的问题的问题。

这是一个示例输入 csv:

Alfa,Beta,Charlie,Delta,Echo,Foxtrot,Golf,Hotel,India,Juliett,Kilo
A,B1,C1,D1,E1,F1,G1,H1,I1,J1,
A,B2,C2,D2,E2,F2,G2,H2,I2,J2,1
B,B3,C3,D3,E3,F3,G3,H3,I3,J3,
B,B4,C4,D4,E4,F4,G4,H4,I4,J4,

这是我拥有的代码版本:

import csv

fieldnames_dict = {
    'Alfa': 'Alfa_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)

这段代码的作用是根据字典重新排列列。

但是我需要根据新字典重复 csv 文件的某些行,并重命名该列行的值。

例如:

second_dictionary = {
    'A' : '1ST,2ND,3RD",
    'B' : '4TH',
    }

这个字典将需要与列“Alfa”进行比较,当它发现它的值是'A',然后它将用值'1ST'、'2ND'和'3RD'重复该行。 如果“Alfa”列的值为“B”,它将查看字典并仅将其自身替换为第 4 个.

输出应该是这样的:

Alfa_New,Echo_New,Foxtrot_New_ALL,Hotel_New,India_New,Charlie_New
1ST,E1,F1,H1,I1,C1
2ND,E1,F1,H1,I1,C1
3RD,E1,F1,H1,I1,C1
1ST,E2,F2,H2,I2,C2
2ND,E2,F2,H2,I2,C2
3RD,E2,F2,H2,I2,C2
4TH,E3,F3,H3,I3,C3
4TH,E4,F4,H4,I4,C4

如您所见,因为输入文件有这一行:

Alfa,Beta,Charlie,Delta,Echo,Foxtrot,Golf,Hotel,India,Juliett,Kilo
A,B1,C1,D1,E1,F1,G1,H1,I1,J1,

对于第二个字典,它需要替换 Alfa 中的 'A' 并自行复制 3 次,但替换第二个字典中的值:

Alfa_New,Echo_New,Foxtrot_New_ALL,Hotel_New,India_New,Charlie_New
1ST,E1,F1,H1,I1,C1
2ND,E1,F1,H1,I1,C1
3RD,E1,F1,H1,I1,C1

我需要为此做什么?

也许这会有所帮助:

import csv

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

second_dictionary = {
    'A': '1ST,2ND,3RD',
    'B': '4TH',
}

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()}
            column_name = 'Alfa_New'
            column_key = row_out[column_name]
            if column_key in second_dictionary.keys():
                for item in second_dictionary[column_key].split(','):
                    row_out[column_name] = item
                    writer.writerow(row_out)
            else:
                writer.writerow(row_out)