简体   繁体   English

python从json数据提取列表到文本

[英]python extract list from json data to text

i have this json data https://pastebin.com/W422EtLn , returned form keywordtool.io api, need extract all string in text file, my acctual python code is this : 我有这个json数据https://pastebin.com/W422EtLn ,从keywordtool.io api返回表格,需要提取文本文件中的所有字符串,我的实际python代码是这样的:

#http://keywordtool.io/api/documentation

def get_data(keyword):
    url = 'http://api.keywordtool.io/v1/search/google?apikey=[api]&keyword={keyword}&country={country}&language={language}&output=json'\
        .format(keyword=keyword, country='it', language='it')
    data = json.loads(urllib2.urlopen(url).read())
    pprint.pprint(data)

get_data('roma')

need output > 需要输出>

0 romano
roma 0 12
roma 0-2 man utd
roma 0 lazio 1

If you just want to 'extract all string elements' from 'results' in the json: 如果您只想从json中的“结果”中“提取所有字符串元素”:

outFile = open('output.txt', 'w')
for element in data['results']:
    tempList = data['results'][element]
    for dict in tempList:
        #print(dict['string'])
        out.write(dict['string']+'\n')

out.close()

Output: 输出:

h&m romania
h romadske tv
roma h&m
roma 2 bologna 3
(etc.)

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

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