用单引号和双引号解析字典时出现语法错误?

SyntaxError when parsing dict with single and double quotes?

所以,我正在使用 pytumblr API 从 Tumblr 检索博客 post。我想检索 post 并仅提取 post 内容。 技术上 Tumblr 以 dict 的形式将其发送给我,但格式非常非常混乱。除此之外,它还使用单引号和双引号!这是我的代码:

post = client.posts(blogName, type = 'text', tag = 'suggestion', limit = 1)
postformat = str(post[u'posts']).replace("[", "").replace("]", "")
blog = dict(ast.literal_eval(postformat))
print(post[u'body']).replace("<p>", "").replace("</p>", "")

首先,Tumblr 给了我一个非常大的字典,只有 3 个键,但每个键中都有一个字典! (???)。所以,我需要获取我要查找的第一个密钥,然后使用 ast 将该密钥的内容转回字典。当我尝试这样做时,我得到了一个不同的错误。因此,我删除了 post 周围的括号并使用 ast 来尝试解释内容。但是当我这样做时,t 在第 3 行抛出 SyntaxError。这里是 post[u'posts'] 的原始格式,非常混乱。但是因为 Whosebug 把它放在了一行,所以我也把它放在了 http://pastebin.com/9fnuS2F6

的 pastebin 上
{u'body': u'<p>Concept: we&rsquo;re in a cute little cottage, surrounded by flowers. I&rsquo;m making breakfast and you&rsquo;re on your way home from walking the dogs. We keep our own bees and collect honey from them; they are as happy and safe as we are.</p>', u'liked': False, u'followed': False, u'reblog_key': u'tlPNsk6e', u'reblog': {u'comment': u'<p>Concept: we\u2019re in a cute little cottage, surrounded by flowers. I\u2019m making breakfast and you\u2019re on your way home from walking the dogs. We keep our own bees and collect honey from them; they are as happy and safe as we are.</p>', u'tree_html': u''}, u'can_send_in_message': True, u'id': 146647556007L, u'post_url': u'http://affectionsuggestion.tumblr.com/post/146647556007/concept-were-in-a-cute-little-cottage', u'can_reply': True, u'title': None, u'tags': u'queued', u'suggestion', u'suggestion blog', u'concept', u'bees', u'love', u'future', u'happy', u'happiness', u'couple', u'relationship', u'nature', u'lesbians', u'cute', u'highlighted': , u'recommended_source': None, u'state': u'published', u'short_url': u'https://tmblr.co/ZzO-5i28au1_d', u'type': u'text', u'recommended_color': None, u'format': u'html', u'timestamp': 1467190899, u'note_count': 523, u'trail': {u'content': u'<p>Concept: we\u2019re in a cute little cottage, surrounded by flowers. I\u2019m making breakfast and you\u2019re on your way home from walking the dogs. We keep our own bees and collect honey from them; they are as happy and safe as we are.</p>', u'content_raw': u'<p>Concept: we\u2019re in a cute little cottage, surrounded by flowers. I\u2019m making breakfast and you\u2019re on your way home from walking the dogs. We keep our own bees and collect honey from them; they are as happy and safe as we are.</p>', u'is_current_item': True, u'blog': {u'active': True, u'theme': {u'title_font_weight': u'bold', u'title_color': u'#444444', u'header_bounds': u'', u'title_font': u'Gibson', u'link_color': u'#3D7291', u'header_image_focused': u'https://secure.assets.tumblr.com/images/default_header/optica_pattern_05.png?_v=671444c5f47705cce40d8aefd23df3b1', u'show_description': True, u'show_header_image': False, u'header_stretch': True, u'body_font': u'Helvetica Neue', u'show_title': True, u'header_image_scaled': u'https://secure.assets.tumblr.com/images/default_header/optica_pattern_05.png?_v=671444c5f47705cce40d8aefd23df3b1', u'avatar_shape': u'square', u'show_avatar': False, u'background_color': u'#FEB0B0', u'header_image': u'https://secure.assets.tumblr.com/images/default_header/optica_pattern_05.png?_v=671444c5f47705cce40d8aefd23df3b1'}, u'share_following': False, u'name': u'affectionsuggestion', u'share_likes': False}, u'is_root_item': True, u'post': {u'id': u'146647556007'}}, u'date': u'2016-06-29 09:01:39 GMT', u'slug': u'concept-were-in-a-cute-little-cottage', u'blog_name': u'affectionsuggestion', u'summary': u"Concept: we're in a cute little cottage, surrounded by flowers. I'm making breakfast and you're on your way home from walking..."}

语法错误是由于您删除了括号造成的。字典中的某个地方有一个标签列表,你的字符串是:

... u'title': None, u'tags': u'queued', u'suggestion', u'suggestion blog', ...

也就是说,它从字典表示法切换到列表表示法,因为标签周围的 [] 消失了。

我怀疑post['posts']只是一个帖子列表,post['posts'][0]是第一个,post['posts'][0]['body']是你要找的正文。