我想将CSV文件转换为字典列表。例如,我有一个CSV文件,其数据顺序如下:
名称、爱好、年龄
萨米足球6
安吉拉象棋12
,输出应该像这样:
(
{"name": "Sammy", "hobby": "football", "age": "6"},
{"name": "Angela", "hobby": "chess", "age": "12"}
)你有什么建议吗?
如果您可以使用Pandas
,则可以使用-
import pandas as pd
df = pd.read_csv('/path/to/csv/file')
records = df.to_dict(orient='records')
输出应该像-
[
{"name": "Sammy", "hobby": "football", "age": "6"},
{"name": "Angela", "hobby": "chess", "age": "12"}
]
这里,我们将csv文件读取为pandas DataFrame,然后将dataframe
转换为dict
。如果pandas
不可用,请使用
pip install pandas
您可以将此代码仅用于csv模块:
import csv
with open(filename, mode='r') as infile:
reader = csv.reader(infile, skipinitialspace=True)
keys = next(reader)
ret_list = []
for row in reader:
ret_list.append({})
for key, value in zip(keys, row):
ret_list[-1][key] = value
更新:这是一个更实用的解决方案:
import csv
with open(filename, mode='r') as infile:
reader = csv.DictReader(infile, skipinitialspace=True)
d = [r for r in reader]
这是一个创建字典列表的方法;
import pandas as pd
# Replace './a.xlsx' with path to your file
# In case file is in csv use pd.read_csv instead
df = pd.read_excel('./a.xlsx')
# Create an empty list to hold the list of dictionaries
list_of_dicts = list()
# Use iterrows to iterate over all rows
for index, row in df.iterrows():
# Empty dictionary to be used as tmp value for each dict in list
dict_person = {}
# Iterate over each column on the row, update the dictionary key and value
for col in range(len(row.index)):
dict_person.update({str(row.index[col]) : row[col]})
# Add the temporary dict to the list
list_of_dicts.append(dict_person)
将产生结果;
[{'name': 'Sammy', 'hobby': 'football', 'age': 6},
{'name': 'Angela', 'hobby': 'chess', 'age': 12}]