【问题标题】:Iterate dictionary inside list which is inside string在字符串内部的列表中迭代字典
【发布时间】:2018-08-21 11:06:38
【问题描述】:

我有一个字符串:-

    '[{
                  "@context": "http://database.org",
                  "mainEntityOfPage":"https://www.nyttimes.com/world/world_army.html",
                  "@type": "NewsArticle",
                  "text": "Now, eight months later, the $23,000 he invested in several digital tokens is worth about $4,000, 
and he is clearheaded about what happened.

    “I got too caught up in the fear of missing out and trying to make a quick buck,” he said last week. “The losses have pretty much left me financially ruined.”"}]'

为了在字符串中迭代字典。我首先尝试从字符串中删除引号:

string=eval(string)

它给了我错误

    "text": "Now, eight months later, the $23,000 he invested in several digital tokens is worth about $4,000, 
        and he is clearheaded about what happened.
                                                   ^
        SyntaxError: EOL while scanning string literal

具体是什么意思?

【问题讨论】:

  • 不要用eval()解析JSON,使用json.loads()

标签: python arrays string python-3.x


【解决方案1】:

由于换行符,您可能会收到错误消息。

试试:

import re
import json
s = '''[{
                  "@context": "http://database.org",
                  "mainEntityOfPage":"https://www.nyttimes.com/world/world_army.html",
                  "@type": "NewsArticle",
                  "text": "Now, eight months later, the $23,000 he invested in several digital tokens is worth about $4,000, and he is clearheaded about what happened.

    “I got too caught up in the fear of missing out and trying to make a quick buck,” he said last week. “The losses have pretty much left me financially ruined.”"}]'''

print( json.loads(re.sub(r"\n", "", s)) )   #or  json.loads(s.replace('\n', ''))

输出:

[{u'@context': u'http://database.org',
  u'@type': u'NewsArticle',
  u'mainEntityOfPage': u'https://www.nyttimes.com/world/world_army.html',
  u'text': u'Now, eight months later, the $23,000 he invested in several digital tokens is worth about $4,000, and he is clearheaded about what happened.    \u201cI got too caught up in the fear of missing out and trying to make a quick buck,\u201d he said last week. \u201cThe losses have pretty much left me financially ruined.\u201d'}]

【讨论】:

  • 我们现在不是超越了python 2 吗?这也被标记为 python 3. +1。
  • {u'@context'... 是 python 2 输出
  • @Rakesh 当我同时为 5000 个相同格式的字符串提取键 url 的值时。我收到一个错误JSONDecodeError: Expecting ',' delimiter: line 1 column 1574 (char 1573)
  • @Mavrick。试试print( json.loads(re.sub(r"\n", "", s.replace(r"“", "'"))) )
【解决方案2】:

此处的字符串评估面向 EOL - 给定字符串中的行尾。下面的代码可能会解决您的问题。

string=string.rstrip()

Python String rstrip() rstrip() 方法返回删除了尾随字符的字符串副本(基于传递的字符串参数)。 rstrip() 根据参数(指定要删除的字符集的字符串)从右侧删除字符。

【讨论】:

    【解决方案3】:

    您面临的主要问题是换行。您需要先替换换行符,然后执行 eval。检查以下示例。

    from pprint import pprint
    
    b = '''[{
                      "@context": "http://database.org",
                      "mainEntityOfPage":"https://www.nyttimes.com/world/world_army.html",
                      "@type": "NewsArticle",
                      "text": "Now, eight months later, the $23,000 he invested in several digital tokens is worth about $4,000, 
    and he is clearheaded about what happened.
        “I got too caught up in the fear of missing out and trying to make a quick buck,” he said last week. “The losses have pretty much left me financially ruined.”"}]'''
    x= eval(b.replace('\n', ' '))
    print(type(x))
    pprint(x)
    

    输出:

    <class 'list'>
    [{'@context': 'http://database.org',
      '@type': 'NewsArticle',
      'mainEntityOfPage': 'https://www.nyttimes.com/world/world_army.html',
      'text': 'Now, eight months later, the $23,000 he invested in several digital '
              'tokens is worth about $4,000,  and he is clearheaded about what '
              'happened.     “I got too caught up in the fear of missing out and '
              'trying to make a quick buck,” he said last week. “The losses have '
              'pretty much left me financially ruined.”'}]
    

    正如 FHTMitchell 所说:

    你不应该使用 eval 因为points mentioned here

    没有 EVAL 的更新代码

    import json
    from pprint import pprint
    
    b = '''[{
                      "@context": "http://database.org",
                      "mainEntityOfPage":"https://www.nyttimes.com/world/world_army.html",
                      "@type": "NewsArticle",
                      "text": "Now, eight months later, the $23,000 he invested in several digital tokens is worth about $4,000, 
    and he is clearheaded about what happened.
        “I got too caught up in the fear of missing out and trying to make a quick buck,” he said last week. “The losses have pretty much left me financially ruined.”"}]'''
    x= json.loads(b.replace('\n', ' '))
    print(type(x))
    pprint(x)
    

    输出:

    <class 'list'>
    [{'@context': 'http://database.org',
      '@type': 'NewsArticle',
      'mainEntityOfPage': 'https://www.nyttimes.com/world/world_army.html',
      'text': 'Now, eight months later, the $23,000 he invested in several digital '
              'tokens is worth about $4,000,  and he is clearheaded about what '
              'happened.     “I got too caught up in the fear of missing out and '
              'trying to make a quick buck,” he said last week. “The losses have '
              'pretty much left me financially ruined.”'}]
    

    【讨论】:

    • 不要使用 eval,即使 OP 使用了它
    • 正确。我已经更新了我的答案。因此并提到了不使用 eval 的原因。
    猜你喜欢
    • 2010-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    • 1970-01-01
    相关资源
    最近更新 更多