如何使用Python 2.7从以下JSON文件中读取特定值(“文本”)?

时间:2018-08-06 14:27:10

标签: python json

我正在尝试从以下JSON文件中读取文本值。

https://www.ted.com//talks/marina_abramovic_an_art_made_of_trust_vulnerability_and_connection/transcript.json?language=en

我想打印“文本”键中的所有内容。

我正在尝试这段代码,但没有得到结果:

import json
import urllib

url = "https://www.ted.com//talks/marina_abramovic_an_art_made_of_trust_vulnerability_and_connection/transcript.json?language=en"
response = urllib.urlopen(url)
data = json.loads(response.read())

def iterate(data):
    for key, value in data.items():
        if isinstance(value, dict):
            print(value)
            iterate(value)
            continue


iterate(data)

1 个答案:

答案 0 :(得分:1)

可以请您试试吗?

import urllib
import json
url = "https://www.ted.com//talks/marina_abramovic_an_art_made_of_trust_vulnerability_and_connection/transcript.json?language=en"
response = urllib.urlopen(url)
data = json.loads(response.read())

def iterate(data):
    if "paragraphs" not in data: # check if paragraphs node exits
        return
    for cues in data['paragraphs']: #iterate through paragraphs node
        for d in cues['cues']: #iterate through cues
            print d['text']  


iterate(data)