簡體   English   中英

讀取csv文件的標頭,查看其是否與字典鍵匹配,然后將該鍵的值寫入行

[英]Reading header of csv file and seeing if it matches a dictionary key, then write value of that key to row

基本上,我會有一堆小詞典,例如:

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

然后我有一個csv文件,其中有一整列的列以及標題中的單詞,如下所示: 在此處輸入圖片說明 可能有500列,每列有1個字,而且我不知道列的顯示順序。 但是,我知道我的小詞典中的任何單詞都應與列中的單詞匹配。

我想遍歷文件的標題(首先跳到5列標題),每次查看是否可以在字典中找到標題名稱,如果可以,則將該值添加到該行中,如果找不到,則添加一個“沒有”。 這將逐行完成,其中每一行對應一個小詞典。 使用上述字典的該文件的結果將是:
在此處輸入圖片說明

到目前為止,我已經可以嘗試以下並非真正有效的方法:

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")

“熊貓”可能會對您有所幫助。

這是網站http://pandas.pydata.org/pandas-docs/stable/

您可以通過使用處理csv文件pandas.read_csv()方法,當你想通過添加一些數據Dataframe.append()方法。

希望這些對您有所幫助。

您的問題似乎是要確保您的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)

給定一個輸入文件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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM