将JSON导出到CSV,并通过get()中的dict中缺少键
我是Python的新手,需要使用Python来完成以下工作。
我正在使用Python来获得一个关键对的价值(这是我从API调用获得的JSON响应),但是,其中一些具有值,而其中一些可能没有值,如下所示:
"attributes": [
{
"key": "TK_GENIE_ACTUAL_TOTAL_HOURS_EXCLUDE_CORRECTIONS",
"alias": "Annual Leave"
},
{
"key": "TK_GENIE_ACTUAL_TOTAL_HOURS_EXCLUDE_CORRECTIONS",
"alias": "Other Non-Prod Hours"
},
{
"key": "TK_GENIE_ACTUAL_TOTAL_HOURS_EXCLUDE_CORRECTIONS",
"alias": "Non-Prod Hours"
},
{
"key": "EMP_COMMON_PRIMARY_JOB",
"alias": "Primary Job",
"rawValue": "RN",
"value": "RN"
},
{
"key": "TIMECARD_TRANS_APPLY_DATE",
"alias": "Apply Date",
"rawValue": "2022-05-19",
"value": "19/05/2022"
},
是一个嵌套的孩子之一
如您所见,以上 输入CSV,带有“别名”是列名称,“值”是行值
如下所示CSV示例:
所以,我使用下面的Python代码来提取每个键的值,并根据指定的列将其放入CSV中。
AL=item['attributes'][0]['value']
Date=item['attributes'][4]['value']
spamwriter.writerow([AL,'2','3',date,'5'])
但是,它提出了一个错误代码,
File "jsoncsv.py", line 47, in <module>
al=item['attributes'][0]['value']
KeyError: 'value'
我认为我理解了此错误,因为此特定的“年假”键没有值。 但是,我怎么说,例如,如果该键没有值,则值= 0,然后将0放在“年假”列下的CSV中,然后移至Next(这是“其他非生产小时” ,在这种情况下,哪个也没有价值,但可能对其他一些孩子有价值)?
我发现get(),但不确定该如何编码,我正在尝试以下代码:
value=it.get('value')
if len(value)>0:
AL=item['attributes'][0]value
Date=item['attributes'][4]value
spamwriter.writerow([AL,'2','3',date,'5'])
但结果是语法错误。
可以取悦任何Python专家提供帮助。
非常感谢。
WB
I am very new to Python, and need to using Python to get the below work done.
I am using Python to get value out for a key pairs (which is the JSON response I got from API call), however, some of them has the value while some of them might not have the value, example of JSON response as below:
"attributes": [
{
"key": "TK_GENIE_ACTUAL_TOTAL_HOURS_EXCLUDE_CORRECTIONS",
"alias": "Annual Leave"
},
{
"key": "TK_GENIE_ACTUAL_TOTAL_HOURS_EXCLUDE_CORRECTIONS",
"alias": "Other Non-Prod Hours"
},
{
"key": "TK_GENIE_ACTUAL_TOTAL_HOURS_EXCLUDE_CORRECTIONS",
"alias": "Non-Prod Hours"
},
{
"key": "EMP_COMMON_PRIMARY_JOB",
"alias": "Primary Job",
"rawValue": "RN",
"value": "RN"
},
{
"key": "TIMECARD_TRANS_APPLY_DATE",
"alias": "Apply Date",
"rawValue": "2022-05-19",
"value": "19/05/2022"
},
The above is one of the children under a nested others, as you can see, for the above one, there is no value for "Annual Leave", however, other children might has a valid value for "Annual Leave"
I am exporting those infor into CSV, with "alias" is the column name, and "value" is the row value
like below csv sample:
So, I using the below python code to extract the value for each key and put them into csv as per column specified.
AL=item['attributes'][0]['value']
Date=item['attributes'][4]['value']
spamwriter.writerow([AL,'2','3',date,'5'])
However, it raised an error code
File "jsoncsv.py", line 47, in <module>
al=item['attributes'][0]['value']
KeyError: 'value'
I think I understand the error, where there is no value for this particular "Annual Leave" key.
But how do I say,like, if there is no value for this key, then value = 0, and put 0 in the CSV under "Annual Leave" column, then, move to next (which is "Other Non-Prod Hours", which also has no value in this case, but might have value for some other children)?
I found get(), but not sure how should I code it, I was trying below code:
value=it.get('value')
if len(value)>0:
AL=item['attributes'][0]value
Date=item['attributes'][4]value
spamwriter.writerow([AL,'2','3',date,'5'])
But result is syntax error.
Could please any Python expert provide help.
Much Appreciated.
WB
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如用户@richarddodson指出的那样,您可以做到这一点:
而不是
0
,您可能需要考虑使用none
,这清楚地表明没有值,避免了与实际值为0
,IE:的混乱相同:
as
.get() /代码>如果没有值得获得的价值。
更明确的方法是:
但是使用
.get()
的解决方案更简单,更快,因此可能是可取的。As user @richarddodson pointed out, you can do this:
Instead of
0
, you may want to consider usingNone
, which is a clear indication that there is no value, avoiding confusion with the case where value actually is0
, i.e.:Which is the same as:
As
.get()
returnsNone
if there is no value to get.A more explicit way to do the same would be:
But the solution using
.get()
is simpler and faster and thus probably preferable.