Python 3.9.5: 一个字典赋值覆盖多个键[BUG?]

Python 3.9.5: One dictionary assignment is overwriting multiple keys [BUG?]

我正在阅读名为 courses 的 .csv。每行对应一门课程,其中有 idnameteacher。它们将存储在字典中。一个例子:

list_courses = { 
    1: {'id': 1, 'name': 'Biology', 'teacher': 'Mr. D'},
    ... 
 }

在使用 enumerate(file_csv.readlines()) 迭代行时,我正在执行以下操作:

list_courses={}

for idx, row in enumerate(file_csv.readlines()):
                # Skip blank rows.
                if row.isspace(): continue
                
                # If we're using the row, turn it into a list.
                row = row.strip().split(",")

                # If it's the header row, take note of the header. Use these values for the dictionaries' keys.
                # As of 3.7 a Dict remembers the order in which the keys were inserted.
                # Since the order is constant, simply load each other row into the corresponding key.           
                if not idx: 
                    sheet_item = dict.fromkeys(row)
                    continue
                
                # Loop through the keys in sheet_item. Assign the value found in the row, converting to int where necessary.
                for idx, key in enumerate(list(sheet_item)):
                    sheet_item[key] = int(row[idx].strip()) if key == 'id' or key == 'mark' else row[idx].strip()


                # Course list
                print("ADDING COURSE WITH ID {} TO THE DICTIONARY:".format(sheet_item['id']))
                list_courses[sheet_item['id']] = sheet_item
                print("\tADDED: {}".format(sheet_item))
                print("\tDICT : {}".format(list_courses))

因此,list_courses 字典在每个 sheet_item 添加到它之后被打印出来。

现在问题来了 - 在阅读两门课程时,我希望 list_courses 应该是:

list_courses = { 
    1: {'id': 1, 'name': 'Biology', 'teacher': 'Mr. D'},
    2: {'id': 2, 'name': 'History', 'teacher': 'Mrs. P'}
 }

然而,我的打印语句的输出(由我程序中的错误证实)是:

ADDING COURSE WITH ID 1 TO THE DICTIONARY:
        ADDED: {'id': 1, 'name': 'Biology', 'teacher': 'Mr. D'}
        DICT : {1: {'id': 1, 'name': 'Biology', 'teacher': 'Mr. D'}}
ADDING COURSE WITH ID 2 TO THE DICTIONARY:
        ADDED: {'id': 2, 'name': 'History', 'teacher': 'Mrs. P'}
        DICT : {1: {'id': 2, 'name': 'History', 'teacher': 'Mrs. P'}, 2: {'id': 2, 'name': 'History', 'teacher': 'Mrs. P'}}

因此,将 sheet_item 添加到 courses_listid 是正确的(1 或 2),但是第二门课程的作业似乎是覆盖键 1 的值。我什至不确定这怎么可能。请告诉我你的想法。

您对 header 和所有行使用了相同的字典。您永远不会在 header 之后创建任何新词典。键分配正在覆盖以前的键,因为没有新的字典可以写入。

将键存储在列表中,并在 for 循环之前创建一个新的 sheet_item

list_courses={}
keys = None # Let Python know this is defined

for idx, row in enumerate(file_csv.readlines()):
                # Skip blank rows.
                if row.isspace(): continue
                
                # If we're using the row, turn it into a list.
                row = row.strip().split(",")

                # If it's the header row, take note of the header. Use these values for the dictionaries' keys.
                # As of 3.7 a Dict remembers the order in which the keys were inserted.
                # Since the order is constant, simply load each other row into the corresponding key.           
                if not idx: 
                    keys = row
                    continue
                
                sheet_item = {}
                # Loop through the keys in sheet_item. Assign the value found in the row, converting to int where necessary.
                for idx, key in enumerate(keys):
                    sheet_item[key] = int(row[idx].strip()) if key == 'id' or key == 'mark' else row[idx].strip()


                # Course list
                print("ADDING COURSE WITH ID {} TO THE DICTIONARY:".format(sheet_item['id']))
                list_courses[sheet_item['id']] = sheet_item
                print("\tADDED: {}".format(sheet_item))
                print("\tDICT : {}".format(list_courses))