【发布时间】:2022-08-22 08:52:40
【问题描述】:
我有一个 .txt 文件(test.txt),里面有一个示例字典
{\"1\": John, \"2\": Jeremy, \"3\": Jake}
在 python 中,我试图从这个文本文件中获取字典,并将其用作我的程序中的字典,但变量的类不是字典,它是 \'str\' 类。
dictionary = open(\"test.txt\", mode=\"r\")
print(dictionary)
print(type(dictionary)
Output:
{\"1\": John, \"2\": Jeremy, \"3\": Jake}
<class \'str\'>
只想知道如何使这个变量成为字典而不是字符串
谢谢
-
如果键总是字符串,您可以使用
json.load。
标签: python dictionary