使用简单的双重替换预处理您的字符串 - 首先转义所有引号,然后用引号替换所有转义的撇号 - 这将简单地反转转义,例如:
# we'll define it as an object to keep the validity
src = "{\\'text\\' : \\'This is the first time I've tried really \"fancy food\" at a...\\'}"
# The double escapes are just so we can type it properly in Python.
# It's still the same underneath:
# {\'text\' : \'This is the first time I've tried really "fancy food" at a...\'}
preprocessed = src.replace("\"", "\\\"").replace("\\'", "\"")
# Now it looks like:
# {"text" : "This is the first time I've tried really \"fancy food\" at a..."}
它现在是一个有效的 JSON(顺便说一下,还有一个 Python 字典),因此您可以继续解析它:
import json
parsed = json.loads(preprocessed)
# {'text': 'This is the first time I\'ve tried really "fancy food" at a...'}
或者:
import ast
parsed = ast.literal_eval(preprocessed)
# {'text': 'This is the first time I\'ve tried really "fancy food" at a...'}
更新:
根据发布的行,您实际上有一个 7 元素元组的(有效)表示,其中包含字典的字符串表示作为其第三个元素,您根本不需要预处理字符串。您需要首先评估元组,然后使用另一个评估级别对内部 dict 进行后处理,即:
import ast
# lets first read the data from a 'input.txt' file so we don't have to manually escape it
with open("input.txt", "r") as f:
data = f.read()
data = ast.literal_eval(data) # first evaluate the main structure
data = data[:2] + (ast.literal_eval(data[2]), ) + data[3:] # .. and then the inner dict
# this gives you `data` containing your 'serialized' tuple, i.e.:
print(data[4]) # 31.328237,-85.811893
# and you can access the children of the inner dict as well, i.e.:
print(data[2]["types"]) # ['restaurant', 'food', 'point_of_interest', 'establishment']
print(data[2]["opening_hours"]["weekday_text"][3]) # Thursday: 7:00 AM – 9:00 PM
# etc.
话虽如此,我建议追踪生成此类数据的人,并说服他们使用某种适当形式的序列化,即使是最基本的 JSON 也会比这更好。