此给定文本采用 json 格式.由于我是 python 的新手,所以告诉我如何仅从用户那里获取 id


  "user":{"contributors_enabled":false,
  created_at":"Wed Jul 21 00:30:00 +0000 2010","
 default_profile":false,
 "default_profile_image":false,"description":"",
 "entities":{"description":{"urls":[]}},
 "favourites_count":0,"follow_request_sent":false,

你可以像这样从user得到id -

records = {
    "user":{
            "contributors_enabled":false,
            "created_at":"Wed Jul 21 00:30:00 +0000 2010",
            "default_profile":false,
            "default_profile_image":false,
            "description":"",
            "entities":{
                        "description":
                                    {"urls":[]}
                        },
            "favourites_count":0,
            "follow_request_sent":false,
            "has_extended_profile":false,
            "id":168873000,
            "id_str":"168873000",
            "is_translation_enabled":false,
            "is_translator":false,
            "lang":null,
            "listed_count":0,
            "location":"",
            "name":"elaine cristina lima",
            "notifications":false,
            "profile_background_color":"FF6699"
            }
}

法典:

records['user']['id']   #168873000 

这将从user返回id

参考:

https://realpython.com/python-json/

编辑

通过从 .json 文件读取从user获取id的代码 -

def records_from(json_path):
    '''Return the records read from .json file json_path
    '''
    with open(json_path, 'r') as json_file:
        records = json.load(json_file)
    return records
jsondata = records_from(filename)
print(jsondata['user']['id'])  # prints 168873000

最新更新