0

http://www.pr2hub.com/files/server_status_2.txt

有 JSON 文件。我想将它们分成每个类别(“server_id”、“server_name”等)

我已经尝试了很多东西,但我什至不知道我有多接近。

我想像这样得到它:

import json

posts = ["{'author':'John Smith', 'translator':'Jane Doe'}"]

authors = []
translators = []

for post in posts:
    double_quotes_post = post.replace("'", '"')
    json_data = json.loads(double_quotes_post)

    author = json_data.get('author', None)
    translator = json_data.get('translator', None)

    if author: authors.append(author)
    if translator: translators.append(translator)
4

1 回答 1

1
>>> with open('server_status_2.txt', 'rb') as fp:
...   j = json.load(fp)
...   print j['servers'][0]
... 
{u'status': u'open', u'server_id': u'1', u'server_name': u'Derron', u'tournament': u'0', u'happy_hour': u'0', u'address': u'198.74.57.185', u'guild_id': u'0', u'port': u'9160', u'population': u'130'}
于 2013-08-18T04:09:55.930 回答