Python 使用字典文本和转义字符将URL保存到字典(忽略/删除转义字符)

Python 使用字典文本和转义字符将URL保存到字典(忽略/删除转义字符),python,python-2.7,eval,Python,Python 2.7,Eval,我正在尝试将URL(不是文本文件)的内容写入字典。该URL有一个文本字典,如下所示: {{"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\nW

我正在尝试将URL(不是文本文件)的内容写入字典。该URL有一个文本字典,如下所示:

{{"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}}
我的猜测是将此URL保存为TXT文件,然后使用以下方法:

然而,我很挣扎,因为一旦我把它变成一个txt文件并尝试对其求值,转义字符就会引起一些问题

同时,这也很好:

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"]

作为一种可能的解决方案,我建议使用
json
module来解析这个字符串

大概是这样的:

import json

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

print(dict_string['jobs'])

希望,这对您有所帮助。

作为可能的解决方案之一,我建议使用
json
模块来解析此字符串

大概是这样的:

import json

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

print(dict_string['jobs'])

希望这对你有帮助。

谢谢杰基尔德博士。我刚刚尝试了一下,得到了一个错误“TypeError:excpected string or buffer”@pugmastaflex抱歉,我的错。我已经编辑了答案。使用json.load()而不是json.load(),谢谢drjackild。我刚刚尝试了一下,得到了一个错误“TypeError:excpected string or buffer”@pugmastaflex抱歉,我的错。我已经编辑了答案。使用json.load()而不是json.loads()