这对大多数人来说可能是一个超级简单的问题,但一个苦苦挣扎的学生会感激一些帮助。我有一个带有嵌套字典列表的字典。有些项目缺失或顺序错误。
我想导出csv文件,按特定顺序(list)我需要比较列表中的项目键名并返回值。如果键不存在返回'N/A'
my_list=["_id", "count", "f_name", "l_name", "number", "country]
my_dict = [{
"_id": 123,
"count": 345,
"account": [
{
"f_name": "Adam",
"l_name": "Adamson",
"number": 1,
"country": "Argentina"
},
{,
"l_name": "Charlson",
"country": "Canada"
},
{
"f_name": "David",
"country": "Denmark",
"number": 3,
"l_name": "Davidson"
},
]
},
{
"_id": 678,
"count": 981,
"account": [
{
"f_name": "Edvin",
"number": 4,
"country": "Equador"
},
]
}]
预期输出
_id | count | f_name | l_name | number | country
------------------------------------------------------
123 | 345 | Adam | Adamson | 1 | Argentina
------------------------------------------------------
123 | 345 | N/A | Charlosn | N/A | Canada
------------------------------------------------------
123 | 345 | David | Davidson | 3 | Denmark
------------------------------------------------------
678 | 981 | Edwin | N/A | 4 | Equador
------------------------------------------------------
任何帮助或建议都将不胜感激。
你可以像这样实现一个循环。
fmtstr = "{:<4} | {:<6} | {:<8} | {:<8} | {:^6} | {:<8}"
line = "--------------------------------------------------------"
print(fmtstr.format(*my_list))
print(line)
for entry in my_dict:
id_ = entry.get('_id')
count = entry.get('count')
for nentry in entry.get('account', []):
print(fmtstr.format(id_,
count,
nentry.get('f_name', 'N/A'),
nentry.get('l_name', 'N/A'),
nentry.get('number', 'N/A'),
nentry.get('country', 'N/A')))
print(line)
使用您的数据,输出是:
_id | count | f_name | l_name | number | country
--------------------------------------------------------
123 | 345 | Adam | Adamson | 1 | Argentina
--------------------------------------------------------
123 | 345 | N/A | Charlson | N/A | Canada
--------------------------------------------------------
123 | 345 | David | Davidson | 3 | Denmark
--------------------------------------------------------
678 | 981 | Edvin | N/A | 4 | Equador
--------------------------------------------------------
如果您需要CSV格式的数据,解决方案更简单:
print(','.join(my_list))
for entry in my_dict:
id_ = entry.get('_id')
count = entry.get('count')
for nentry in entry.get('account', []):
print(("{},{},{},{},{},{}"
.format(id_,
count,
nentry.get('f_name', 'N/A'),
nentry.get('l_name', 'N/A'),
nentry.get('number', 'N/A'),
nentry.get('country', 'N/A'))))
这次输出将是:
_id,count,f_name,l_name,number,country
123,345,Adam,Adamson,1,Argentina
123,345,N/A,Charlson,N/A,Canada
123,345,David,Davidson,3,Denmark
678,981,Edvin,N/A,4,Equador
一旦您修复了列表和字典中的错字,您就可以将字典转换为表(列表的列表)并使用csv模块导出它:
table = [ [d.get(k,a.get(k,'N/A')) for k in my_list]
for d in my_dict for a in d['account'] ]
输出:
for row in table:print(row)
[123, 345, 'Adam', 'Adamson', 1, 'Argentina']
[123, 345, 'N/A', 'Charlson', 'N/A', 'Canada']
[123, 345, 'David', 'Davidson', 3, 'Denmark']
[678, 981, 'Edvin', 'N/A', 4, 'Equador']
然后保存为csv文件:
import csv
with open('table.csv','w') as f:
output = csv.writer(f)
output.writerow(my_list)
for row in table:
output.writerow(row)
:
for row in [my_list,*table]:
print("|".join(f" {str(c):10}" for c in row))
print("-"*70)
_id | count | f_name | l_name | number | country
----------------------------------------------------------------------
123 | 345 | Adam | Adamson | 1 | Argentina
----------------------------------------------------------------------
123 | 345 | N/A | Charlson | N/A | Canada
----------------------------------------------------------------------
123 | 345 | David | Davidson | 3 | Denmark
----------------------------------------------------------------------
678 | 981 | Edvin | N/A | 4 | Equador
----------------------------------------------------------------------