我无法将生成的字符串解码为 Python dict

I can not decode generated string to Python dict

我有一个字符串,但我无法使用 ast.literal_eval

将其解码为字典

发现错误,字符串编码错误!

它应该转换的字符串:(Link 到 google Dock) https://docs.google.com/document/d/1jGjIPEzB9j48i1LDKQ2__Nhg5OE4R_jeaGCFq_DFr2M/edit?usp=sharing

回退:

Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:/Users/user/Documents/Python/Documents/pickle_viewer/PickleViewer.py", line 444, in selectItem
    item_dict = ast.literal_eval(itemInfo["tags"][2])
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\ast.py", line 46, in literal_eval
    node_or_string = parse(node_or_string, mode='eval')
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\ast.py", line 35, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
  File "<unknown>", line 1

要转换的代码:

item_dict = ast.literal_eval(itemInfo["tags"][2])

这会生成字符串:

    def json_tree(tree, parent, dictionary):
                tmp_key = tree.insert(parent, 'end', uid, text=key + ' [...]', value="[...]", tag=(uid, True, dictionary[key]))

我认为这是因为所有的反斜杠,但我不知道它们来自哪里

Link 到 google 文档上的复制代码:https://docs.google.com/document/d/1CDSNqi3FqgRaVUv-N5eoV5R3xxS_atbSybaYXmC5cNE/edit?usp=sharing

有人可以帮助我吗?谢谢。

更新的答案:

您需要清理源数据,因为它的格式无效,然后将其解析为 json 字符串:

Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
import json
source = <your_very_long_input_loaded_as_raw_string_comes_here>
source = source.replace('\', '')
source = source.replace('\'', '"')
source = source.replace('} {"comments"', '}, {"comments"')
source = "[" + source + "]"
d = json.loads(source)

对不起,那都是我的错,我错误地解码了起始字符串。现在它已正确解码,这为我节省了所有剩余时间,我可以轻松使用 ast.literal_eval(字符串) 再次.

不过,非常感谢您的回答。