import json
data1 = {'b': 789, 'c': 456, 'a': 123}
encode_line = json.dumps(data1)
decode_json = json.loads(encode_line)
print type(encode_line)
print type(decode_line)
类型(decode_line)是[字典],但是当我们保存DATA1到txt文件,并且使用下面的脚本阅读:的Python:变换[STR]类型JSON数据到[字典]类型
file = open('test.txt','r')
for line in file:
encode_line = json.dumps(line)
decode_line = json.loads(encode_line)
print type(encode_line)
print type(decode_line)
现在,类型(decode_line)是[unicode]。为什么?
我想从txt文件读取数据,然后从字典类型中检索信息。我应该怎么做 ?
谢谢!
谢谢。是的,我想从一个txt文件中得到一个Python字典,但是失败了。文件内容为{'b':789,'c':456,'a':123} {'b':753,'c':446,'a':223} – noteahp
由于文本文件不包含有效的JSON。 JSON使用双引号分隔键,而不是单引号。另外,应该有一个JSON对象;在你的文件中,似乎有两个。你是否试图处理多个由换行符分隔的JSON对象? –
感谢您的帮助。双引号是问题。是的,在txt文件中有30000多行JSON格式的表单,我想要检索一行的部分信息。 – noteahp