我正试图像这样转换字符串列表
['{"What is the purpose of a noun?":"To name something or someone."}', '{"What is the purpose of a verb?":"To show action"}']
像这样的口述清单
[{"What is the purpose of a noun?":"To name something or someone."}, {"What is the purpose of a verb?":"To show action"}]
这是txt文件中原始字符串的样子
{"What is the purpose of a noun?":"To name something or someone."}
{"What is the purpose of a verb?":"To show action in a sentence."}
json模块不工作
a = []
with open("proans.txt",'r') as proans:
#transform string in the txt file into list of string by \n
pa = proans.read().split('\n')
#iterate through the list of string, convert string to dict and put them
#into a list
for i in range(len(pa)):
json_acceptable_string = pa[i].replace("\"", "'")
ret_dict = json.loads(json_acceptable_string)
a.append(ret_dict)
我犯了这样的错误
ValueError: Expecting property name: line 1 column 2 (char 1)
如何将这种类型的字符串列表转换为dict列表?谢谢
去掉replace行:
json_acceptable_string = ...
。没有必要逃避引用。你知道吗类似于带有
StringIO
对象的原始代码的示例:相关问题 更多 >
编程相关推荐