读取csv文件的标头,查看其是否与字典键匹配,然后将该键的值写入行

时间:2018-11-28 03:20:48

标签: python csv dictionary file-io

基本上,我会有一堆小字典,像这样:

dictionary_list = [
{"eight": "yes", "queen": "yes", "we": "yes", "eighteen": "yes"},
{"nine": "yes", "king": "yes","we": "yes", "nineteen": "yes"}
]

然后我有一个csv文件,其中包含一堆带有标题的单词的列,如下所示: enter image description here  可能有500列,每列有1个字,而且我不知道列的显示顺序。但是,我确实知道我的小词典中的任何单词都应与列中的单词匹配。

我想遍历文件的标题(首先跳到5列标题),每次查看是否可以在字典中找到标题名称,如果可以,将值添加到该行中(如果没有) ,添加一个“否”。这将逐行完成,其中每一行对应一个小词典。使用上述字典的该文件的结果将是:
enter image description here

到目前为止,我已经可以尝试以下并非真正有效的方法:

f = open("file.csv", "r")
writer = csv.DictWriter(f)
for dict in dictionary_list: # this is the collection of little dictionaries
    # do some other stuff
    for r in writer: 
        #not sure how to skip 10 columns here. next() seems to work on rows 
        for col in r:
            if col in dict.keys():
                 writer.writerow(dict.values())
             else:
                 writer.writerow("no")

3 个答案:

答案 0 :(得分:1)

“熊猫”可能会对您有所帮助。

这里是网站http://pandas.pydata.org/pandas-docs/stable/

您可以使用pandas.read_csv()方法处理csv文件,并使用Dataframe.append()方法根据需要添加一些数据。

希望这些对您有所帮助。

答案 1 :(得分:1)

您的问题似乎是要确保您的dictionary_list中的字段存在记录。如果该字段最初存在于记录中,则将字段值设置为yes,否则将其添加到记录中并将该值设置为no。

#!/usr/bin/env python3

import csv


dictionary_list = [
    {"eight": "yes", "queen": "yes", "we": "yes", "eighteen": "yes"},
    {"nine": "yes", "king": "yes","them": "yes", "nineteen": "yes"}
]

"""
flatten all the dicionary keys into a uniq list as the
key names will be used for field names and can't be duplicated
"""
field_check = set([k for d in dictionary_list for k in d.keys()])

if __name__ == "__main__":

    with open("file.csv", "r") as f:
        reader = csv.DictReader(f)

        # do not consider the first 10 colums
        field_tail = set(reader.fieldnames[10:])

        """
        initialize yes and no fields as they
        should be the same for every row in the file
        """
        yes_fields = set(field_check & field_tail)
        no_fields = field_check.difference(yes_fields)
        yes_dict = {k:"yes" for k in yes_fields}
        no_dict = {k:"no" for k in no_fields}
        for row in reader:
            row.update(yes_dict)
            row.update(no_dict)
            print(row)

答案 2 :(得分:1)

给出输入文件headers.csv

row1,row2,row3,row4,row5,bad,good,eight,nine,queen,three,eighteen,nineteen,king,jack,ace,we,them,you,two

以下代码生成您的输出:

import csv

dictionary_list = [{"eight": "yes", "queen": "yes", "we": "yes", "eighteen": "yes"},
                   {"nine": "yes", "king": "yes","we": "yes", "nineteen": "yes"}]

# Read the input header line as a list
with open('headers.csv',newline='') as f:
    reader = csv.reader(f)
    headers = next(reader)

# Generate the fixed values for the first 5 rows.
rowvals = dict(zip(headers[:5],['x'] * 5))

with open('file.csv', 'w', newline='') as f:
    # When writing a row, restval is the default value when it isn't in the dict row.
    # extrasaction='ignore' prevents complaining if all columns are not present in dict row.
    writer = csv.DictWriter(f,headers,restval='no',extrasaction='ignore')
    writer.writeheader()
    for dictionary in dictionary_list:
        D = dictionary.copy() # needed if the original shouldn't be modified.
        D.update(rowvals)
        writer.writerow(D)

输出:

row1,row2,row3,row4,row5,bad,good,eight,nine,queen,three,eighteen,nineteen,king,jack,ace,we,them,you,two
x,x,x,x,x,no,no,yes,no,yes,no,yes,no,no,no,no,yes,no,no,no
x,x,x,x,x,no,no,no,yes,no,no,no,yes,yes,no,no,yes,no,no,no