0

So I have the following code, and I keep getting a "SyntaxError: EOL while scanning string literal" error. What is formatted wrong? For context, the following is a list of articles that I read using a chrome extension and all I want to do is print the URL.

import json
json_data = "{
    "bookmarks": [
        {
            "article__excerpt": "i When Tom Wainwright became the Mexico correspondent for The Economist in 2010, he found himself covering the country's biggest businesses, including t",
            "favorite": true,
            "date_archived": "2016-02-17T15:38:46",
            "article__url": "http://www.npr.org/2016/02/15/466491812/narconomics-how-the-drug-cartels-operate-like-wal-mart-and-mcdonalds",
            "date_added": "2016-02-17T15:38:35",
            "date_favorited": "2016-02-17T15:38:33",
            "article__title": "'Narconomics': How The Drug Cartels Operate Like Wal-Mart And McDonald's",
            "archive": true
        },
        {
            "article__excerpt": null,
            "favorite": false,
            "date_archived": null,
            "article__url": "https://www.jacobinmag.com/2013/07/an-empire-of-our-lives/",
            "date_added": "2016-02-17T15:38:21",
            "date_favorited": null,
            "article__title": "https://www.jacobinmag.com/2013/07/an-empire-of-our-lives/",
            "archive": false
        },
        {
            "article__excerpt": "Socialism is love. For Valentine’s Day, skip the flowers and chocolate and follow this link for $14.30 gift subscriptions. After a breakup a friend will inevitably turn to you over a drink,…",
            "favorite": false,
            "date_archived": null,
            "article__url": "https://www.jacobinmag.com/2016/02/internet-dating-commodification-love-valentine/",
            "date_added": "2016-02-17T15:37:37",
            "date_favorited": null,
            "article__title": "To Fall in Love, Click Here",
            "archive": false
        },
        {
    ],
    "recommendations": []
}"
data = json.loads(json_data)
for article in json_data["bookmarks"]:
    print(article["article__url"]) 
1
  • 3
    That whole strong is formatted wrong - you need to make it """multiline""" to allow the quotes and line breaks. Commented Feb 17, 2016 at 19:54

1 Answer 1

1

Use triple quotes at the beginning and end of your json. This way newline characters won't break the string.

import json
json_data = """{
    "bookmarks": [
        {
            "article__excerpt": "i When Tom Wainwright became the Mexico correspondent for The Economist in 2010, he found himself covering the country's biggest businesses, including t",
            "favorite": true,
            "date_archived": "2016-02-17T15:38:46",
            "article__url": "http://www.npr.org/2016/02/15/466491812/narconomics-how-the-drug-cartels-operate-like-wal-mart-and-mcdonalds",
            "date_added": "2016-02-17T15:38:35",
            "date_favorited": "2016-02-17T15:38:33",
            "article__title": "'Narconomics': How The Drug Cartels Operate Like Wal-Mart And McDonald's",
            "archive": true
        },
        {
            "article__excerpt": null,
            "favorite": false,
            "date_archived": null,
            "article__url": "https://www.jacobinmag.com/2013/07/an-empire-of-our-lives/",
            "date_added": "2016-02-17T15:38:21",
            "date_favorited": null,
            "article__title": "https://www.jacobinmag.com/2013/07/an-empire-of-our-lives/",
            "archive": false
        },
        {
            "article__excerpt": "Socialism is love. For Valentine’s Day, skip the flowers and chocolate and follow this link for $14.30 gift subscriptions. After a breakup a friend will inevitably turn to you over a drink,…",
            "favorite": false,
            "date_archived": null,
            "article__url": "https://www.jacobinmag.com/2016/02/internet-dating-commodification-love-valentine/",
            "date_added": "2016-02-17T15:37:37",
            "date_favorited": null,
            "article__title": "To Fall in Love, Click Here",
            "archive": false
        },
        {
    ],
    "recommendations": []
}"""
data = json.loads(json_data)
for article in json_data["bookmarks"]:
    print(article["article__url"]) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.