我有一个字符串,如:-
'[{
"@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
这到底是什么意思?
答案 0 :(得分:2)
由于换行符,您可能会收到错误消息。
尝试:
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'}]
答案 1 :(得分:0)
在此面向EOL的字符串评估-给定字符串中的行尾。以下代码可以解决您的问题。
string=string.rstrip()
Python字符串rstrip()rstrip()方法返回字符串的副本,其中删除了尾随字符(基于传递的字符串参数)。 rstrip()根据参数(指定要删除的字符集的字符串)从右侧删除字符。
答案 2 :(得分:0)
您面临的主要问题是换行。您需要先替换换行符,然后进行评估。检查以下示例。
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不带验证码更新代码:
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.”'}]