CSV不正确导致关键错误的键值

时间:2019-02-02 16:07:57

标签: python csv keyerror

我有一个csv文件,试图从中提取特定的列,但出现keyerror。当我尝试打印CSV的键,我得到这样的结果:

[None, 'Id;"PostTypeId";"ParentId";"AcceptedAnswerId";"OwnerUserId";"LastEditorUserId";"OwnerDisplayName";"LastEditorDisplayName";"UserIdCombined"']

我尝试的代码:

    read_file = open ("ml_sample_complete.csv","r") # open input file for reading
    col_dict = {}
    with open('out.csv', 'wb') as f: # output csv file
        writer = csv.writer(f)
    with open('ml_sample_complete.csv','r') as csvfile: # input csv file
        reader = csv.DictReader(csvfile, delimiter=',')
        for row in reader:
            print(row.keys())
            print(row["Tags"])
            col_dict.update(row)
    print(col_dict)
    read_file.close()

请帮我明白这里有什么问题以及如何纠正它。

1 个答案:

答案 0 :(得分:0)

您确定您的delimter应该是“”而不是‘;’在dictreader函数中。