Python - 在提取数据之前计算 JSON 元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31204269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:38:31  来源:igfitidea点击:

Python - Count JSON elements before extracting data

pythonjson

提问by Hyperion

I use an API which gives me a JSON file structured like this:

我使用了一个 API,它给了我一个结构如下的 JSON 文件:

{
offset: 0,
results: [
{
  source_link: "http://www.example.com/1",
  source_link/_title: "Title example 1",
  source_link/_source: "/1",
  source_link/_text: "Title example 1"
},
{
  source_link: "http://www.example.com/2",
  source_link/_title: "Title example 2",
  source_link/_source: "/2",
  source_link/_text: "Title example 2"
},
...

And I use this code in Python to extract the data I need:

我在 Python 中使用此代码来提取我需要的数据:

import json
import urllib2

u = urllib2.urlopen('myapiurl')
z = json.load(u)
u.close
link = z['results'][1]['source_link']
title = z['results'][1]['source_link/_title']

The problem is that to use it I have to know the number of the element from which I'm extracting the data. My resultscan have different length every time, so what I want to do is to count the number of elements in resultsat first, so I would be able to set up a loop to extract data from each element.

问题是要使用它,我必须知道从中提取数据的元素的编号。我results每次都可以有不同的长度,所以我想做的是首先计算元素的数量results,这样我就可以设置一个循环来从每个元素中提取数据。

采纳答案by LexyStardust

To check the length of the results key:

要检查结果键的长度:

len(z["results"])

But if you're just looping around them, a for loop is perfect:

但是如果你只是在它们周围循环,那么 for 循环是完美的:

for result in x["results"]:
    print(result["source_link"])

回答by fasouto

You didn't need to know the length of the result, you are fine with a for loop:

您不需要知道结果的长度,您可以使用 for 循环:

for result in z['results']:
    # process the results here

Anyway, if you want to know the length of 'results': len(z.results)

无论如何,如果您想知道“结果”的长度: len(z.results)

回答by crsxl

If you want to get the length, you can try:

如果你想得到长度,你可以尝试:

len(z['result'])

But in python, what we usually do is:

但是在python中,我们通常做的是:

for i in z['result']:
    # do whatever you like with `i`

Hope this helps.

希望这可以帮助。

回答by CrazyCasta

You don't need, or likely want, to count them in order to loop over them, you could do:

您不需要或可能不想计算它们以循环它们,您可以这样做:

import json
import urllib2

u = urllib2.urlopen('myapiurl')
z = json.load(u)
u.close
for result in z['results']:
    link = result['source_link']
    title = result['source_link/_title']
    # do something with link/title

Or you could do:

或者你可以这样做:

u = urllib2.urlopen('myapiurl')
z = json.load(u)
u.close
link = [result['source_link'] for result in z['results']]
title = [result['source_link/_title'] for result in z['results']]
# do something with links/titles lists

回答by Reut Sharabani

Few pointers:

几点指点:

  1. No need to know results's length to iterate it. You can use for result in z['results'].
  2. lists start from 0.
  3. If you do need the index take a look at enumerate.
  1. 不需要知道results的长度来迭代它。您可以使用for result in z['results'].
  2. lists 从0.
  3. 如果您确实需要索引,请查看enumerate.