简体   繁体   English

通过 JSON 文件中的 Python 访问字典中的列表

[英]Accessing list within a dictionary via Python from a JSON file

I am new to Python and could use some help, please.我是 Python 的新手,需要一些帮助。 I'm trying to use Python to read from my JSON file and get the values of the list within in a dictionary.我正在尝试使用 Python 从我的 JSON 文件中读取并获取字典中列表的值。

Once I read the JSON into my program, I have:一旦我将 JSON 读入我的程序,我就会:

request body = {
  "providerName": "ProviderNameXYZ",
  "rateRequestPayments": [
    {
      "amount": 128.0,
      "creditorProfileId": "7539903942457444658",
      "debtorProfileId": "2072612712266192555",
      "paymentMethodId": "2646780961603748694016",
      "currency": "EUR",
      "userReference": "INVALID user ref automation single",
      "reconciliationId": "343546578753349"
    },
    {
      "amount": 129.0,
      "creditorProfileId": "7539903942457444658",
      "debtorProfileId": "2072612712266192555",
      "paymentMethodId": "2646780961603748694016",
      "currency": "EUR",
      "userReference": "INVALID user ref automation single",
      "reconciliationId": "343546578753340"
    }
  ]
}
  1. I now want to be able to grab the value of any particular key.我现在希望能够获取任何特定键的值。 I've tried accessing it via several routes:我试过通过几种途径访问它:
  • rateRequestPayments[0].amount
  • rateRequestPayments()[0].amount
  • for k, v in request_body.rateRequestPayments(): print(k, v)
  • for each in request_body.rateRequestPayments: print(each('amount'))
  • values = eval(request_body['rateRequestPayments']) print(values[0]) All of these end up with errors. values = eval(request_body['rateRequestPayments']) print(values[0])所有这些都以错误告终。 Per the 2nd comment below: request_body['rateRequestPayments'][0]['amount'] This works!根据下面的第二条评论: request_body['rateRequestPayments'][0]['amount']这行得通!
  1. I also want to be able to delete the whole key-value pair ("amount": 128.0,) from the request_body.我还希望能够从 request_body 中删除整个键值对 ("amount": 128.0,)。 request_body['rateRequestPayments'][0]['amount'] does not work for this. request_body['rateRequestPayments'][0]['amount']不适用于此。 Not sure how to reference this.不知道如何引用这个。

I know I am missing something simple but I'm just unsure what it is.我知道我遗漏了一些简单的东西,但我不确定它是什么。 Any help would be greatly appreciated.任何帮助将不胜感激。

Python is great for data science, and has many features that make it fit for the job. Python 非常适合数据科学,并且具有许多适合该工作的功能。 One of those features is finding data in a data set.这些功能之一是在数据集中查找数据。 body['rateRequestPayments'][<!WHICH DATA SET YOU WANT TO ACCESS>][<!VALUE YOU WANT TO FIND>]

First you'll need to put an underscore between requests and body so its requests_body = {...} .首先,您需要在requestsbody之间添加一个下划线,这样它的requests_body = {...}

To access a key-value pair:要访问键值对:

requests_body["rateRequestPayments"] #to access the list of two dicts
requests_body["rateRequestPayments"][0] #to access the individual dicts by index
requests_body["rateRequestPayments"][0]["amount"] #to access the value of a particular key (in this case "amount" is the key)

To delete a key value pair, just use the del statement:要删除键值对,只需使用del语句:

del requests_body["rateRequestPayments"][0]["amount"]

This will delete the key-value pair in the variable requests_body , so trying to access this key again will raise a KeyError error.这将删除变量requests_body中的键值对,因此再次尝试访问该键将引发KeyError错误。

To access and delete a key-value pair, use .pop() :要访问删除键值对,请使用.pop()

value = requests_body["rateRequestPayments"][0].pop("amount")
#value is now 128.0 and the key-value pair is deleted

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM