[英]How to convert a csv file to a Dictionary in Python?
我有一個 csv 文件,其中包含創建 yaml 文件的配置信息(最終所需的結果)。 首先,我試圖將 csv 文件的每一行轉換為字典,然后我可以使用 yaml.dump(Created_Dictionary) 輕松地將字典轉換為 yaml 文件
示例輸入文件 (test.csv):
fieldname|type|allowed|coerce
field_A|String|10,20,30|to_str
field_B|Integer||to_int
我使用熊貓庫的源代碼:
df = pd.read_csv("test.csv", "|")
df_to_dict = df.to_dict(orient='records')
print(df_to_dict) # print the dictionary
test_yaml = yaml.dump(df_to_dict)
print(test_yaml) # print the yaml file
我得到的字典輸出(df_to_dict):
[{'fieldname': 'field_A', 'type': 'String', 'allowed': '10,20,30'}, {'fieldname': 'field_B', 'type': 'Integer', 'allowed': nan}]
我為 yaml (test_yaml) 得到的輸出:
- allowed: 10,20,30
fieldname: field_A
type: String
- allowed: .nan
fieldname: field_B
type: Integer
所需的字典輸出(df_to_dict)是:
[
{'field_A':
{'type': 'String', 'allowed': '10,20,30', 'coerce': to_str}
},
{'field_B':
{'type': 'String', 'allowed': '', 'coerce': to_int}
}
]
所需的 yaml 輸出(test_yaml)是:
field_A:
type: String
allowed:
- '10'
- '20'
- '30'
coerce: to_str
field_B:
type: Integer
allowed:
coerce: to_int
我看到變量 df_to_dict 是一個字典列表。 我是否必須遍歷每個列表項,然后為每一行構建字典? 我不明白正確的方法。 任何幫助表示贊賞。
嘗試:
my_dict = df.set_index("fieldname").to_dict("index")
test_yaml = yaml.dump(my_dict, sort_keys=False)
>>> print(test_yaml)
field_A:
allowed: 10,20,30
type: String
field_B:
allowed: .nan
type: Integer
你想玩弄你的熊貓數據幀的索引。
>>> df = pd.read_csv("test.csv", sep="|", index_col=0)
>>> df
type allowed
fieldname
field_A String 10,20,30
field_B Integer NaN
>>> df.to_dict(‘index’) # returns dict like {index -> {column -> value}}
{'field_A': {'type': 'String', 'allowed': '10,20,30'}, 'field_B': {'type': 'Integer', 'allowed': nan}}
>>> print(yaml.dump(df.to_dict(‘index’)))
field_A:
allowed: 10,20,30
type: String
field_B:
allowed: .nan
type: Integer
您必須處理自定義轉儲或過濾器的.nan
。
看
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html
如果您不需要 Pandas,並且在您的描述或示例中我認為不需要它,請使用 Python 的內置csv庫及其DictReader類。
import csv
with open('sample.csv', newline='') as f:
reader = csv.DictReader(f, delimiter='|')
for row in reader:
fname = row['fieldname']
yaml_d = {fname: row} # "index" row by fieldname
del yaml_d[fname]['fieldname'] # remove now-extraneous fieldname from row
print(yaml_d)
讓我:
{'field_A': {'type': 'String', 'allowed': '10,20,30'}}
{'field_B': {'type': 'Integer', 'allowed': ''}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.