0

I have a string as :-

    '[{
                  "@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.”"}]'

In order to iterate dictionary inside string. I first tring to remove quotes from string as:

string=eval(string)

It gives me the error as

    "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

what does it exactly means?

Learner
  • 800
  • 1
  • 8
  • 23

3 Answers3

2

You are probably getting the error because of the new line character.

Try:

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', ''))

Output:

[{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'}]
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

Here string evaluation facing EOL - End of Line in the given string. Below code may solve your issue.

string=string.rstrip()

Python String rstrip() The rstrip() method returns a copy of the string with trailing characters removed (based on the string argument passed). The rstrip() removes characters from the right based on the argument (a string specifying the set of characters to be removed).

0

The main issue you are facing is new line. You need to replace the newline character first, then do eval. Check the below example.

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)

output:

<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.”'}]

As mentioned by FHTMitchell :

You should not use eval because of the points mentioned here

UPDATE CODE WITHOUT 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)

Output:

<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.”'}]
Arghya Saha
  • 5,599
  • 4
  • 26
  • 48