从字符串中获取ID

时间:2013-12-13 23:55:51

标签: python

我有一个字符串,我想抓住“id”号12079500908。我正在尝试使用ast.literal_eval,但收到了ValueError: malformed string。有没有其他方法可以从下面的字符串中获取id数字?

doc_request = urllib2.Request("https://api.box.com/2.0/search?query=SEARCHTERMS", headers=doc_headers)
doc_response = urllib2.urlopen(doc_request)
view_doc_response = doc_response.read()
doc_dict=ast.literal_eval(view_doc_response)

修改 输出:

view_doc_response = '{"total_count":1,"entries":[{"type":"file","id":"12079500908","sequence_id":"1","etag":"1","sha1":"6887169228cab0cfb341059194bc980e1be8ad90","name":"file.pdf","description":"","size":897838,"path_collection":{"total_count":2,"entries":[{"type":"folder","id":"0","sequence_id":null,"etag":null,"name":"All Files"},{"type":"folder","id":"1352745576","sequence_id":"0","etag":"0","name":"Patient Files"}]},"created_at":"2013-12-03T10:23:30-08:00","modified_at":"2013-12-03T11:17:52-08:00","trashed_at":null,"purged_at":null,"content_created_at":"2013-12-03T10:23:30-08:00","content_modified_at":"2013-12-03T11:17:52-08:00","created_by":{"type":"user","id":"20672372","name":"name","login":"email"},"modified_by":{"type":"user","id":"206732372","name":"name","login":"email"},"owned_by":{"type":"user","id":"206737772","name":"name","login":"email"},"shared_link":{"url":"https:\\/\\/www.box.net\\/s\\/ymfslf1phfqiw65bunjg","download_url":"https:\\/\\/www.box.net\\/shared\\/static\\/ymfslf1phfqiw65bunjg.pdf","vanity_url":null,"is_password_enabled":false,"unshared_at":null,"download_count":0,"preview_count":0,"access":"open","permissions":{"can_download":true,"can_preview":true}},"parent":{"type":"folder","id":"1352745576","sequence_id":"0","etag":"0","name":"Patient Files"},"item_status":"active"}],"limit":30,"offset":0}'

调用doc_dict会给出:

ValueError: malformed string

1 个答案:

答案 0 :(得分:7)

ast.literal_eval用于解析有效的Python语法,你拥有的是JSON。有效的JSON看起来很像Python语法,除了JSON可以包含nulltruefalse,它们映射到NoneTrue和{{ 1}}在Python中通过JSON解码器传递。您可以使用json.loads。代码可能如下所示:

False

请注意,这假设您在字符串末尾手动添加了import json doc_dict = json.loads(view_doc_response) first_id = doc_dict['entries'][0]['id'] # with your data, should be 12079500908 ,大概是在缩短字符串之后。如果...实际上在你的代码中,那么你有无效的JSON,你需要做一些处理才能生效。