0

I'm trying to write the contents of a URL (not a text file) to a dictionary. The URL has a dictionary of dictionaries in text like so:

{{"id":15160,"icimsId":247092,"shortDescription":"ABC Leadership Development Program - Retail & Consumer\n\n\u00a3 Competitive basic salary + sign on bonuses + benefits\nClosing date: 30th June 2014\n\nWork hard. Have fun. Make history.\n\nABC opened its","url":"\/jobs\/247092\/ABC-mba-leadership-development-program-uk","title":"ABC MBA Leadership Development Program (UK) ","jobCategory":"Buying, Planning, & Instock Management","location":"GB, Slough","teamCategory":"University Recruiting","team":null}}

My guess is to save this URL as a TXT file and then use this approach:

Writing a dict to txt file and reading it back?

However, I'm struggling because once I make it into a txt file and try to eval it, the escape characters cause some issues.

Meanwhile this works just fine:

dict_string = r"""{{"id":15160,"icimsId":247092,"shortDescription":"ABC Leadership Development Program - Retail & Consumer\n\n\u00a3 Competitive basic salary + sign on bonuses + benefits\nClosing date: 30th June 2014\n\nWork hard. Have fun. Make history.\n\nABC opened its","url":"\/jobs\/247092\/ABC-mba-leadership-development-program-uk","title":"ABC MBA Leadership Development Program (UK) ","jobCategory":"Buying, Planning, & Instock Management","location":"GB, Slough","teamCategory":"University Recruiting","team":null}}"""

dict_string = eval(dict_string)
print dict_string["jobs"]
Community
  • 1
  • 1
pugmastaflex
  • 470
  • 1
  • 5
  • 17

1 Answers1

3

As one of possible solutions, I propose to use json module, to parse this string.

Something like this:

import json

with open('file_with_dict.txt', 'r') as f:
    dict_string = json.load(f)

print(dict_string['jobs'])

Hope, this help you.

drjackild
  • 473
  • 4
  • 17