将JSON导出到CSV,并通过get()中的dict中缺少键

发布于 2025-01-30 12:35:36 字数 2093 浏览 0 评论 0原文

我是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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌生 2025-02-06 12:35:36

正如用户@richarddodson指出的那样,您可以做到这一点:

al = item['attributes'][0].get('value', 0)

而不是0,您可能需要考虑使用none,这清楚地表明没有值,避免了与实际值 0,IE:的混乱

al = item['attributes'][0].get('value', None)

相同:

al = item['attributes'][0].get('value')

as .get() /代码>如果没有值得获得的价值。

更明确的方法是:

al = None if 'value' in item['attributes'][0] else item['attributes'][0]['value']

但是使用.get()的解决方案更简单,更快,因此可能是可取的。

As user @richarddodson pointed out, you can do this:

al = item['attributes'][0].get('value', 0)

Instead of 0, you may want to consider using None, which is a clear indication that there is no value, avoiding confusion with the case where value actually is 0, i.e.:

al = item['attributes'][0].get('value', None)

Which is the same as:

al = item['attributes'][0].get('value')

As .get() returns None if there is no value to get.

A more explicit way to do the same would be:

al = None if 'value' in item['attributes'][0] else item['attributes'][0]['value']

But the solution using .get() is simpler and faster and thus probably preferable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文