读取两个包含 json 数据的文件并使用 2 个文件数据创建新字典
我的文件的数据为
file1.txt
Student={'name'='john','age'=13,'roll_no':22445,'gender'='male'},{'name'='jimmy','age'=14,'roll_no':45622,'gender'='male'}
file2.txt。
Address1={'name':'john','roll_no'=22445,'street':'st johns','pin':56902,'country':'US'},{'name':'jimmy','roll_no'=45622,'street':'st peters','pin':56502,'country':'US'}
当我读取文件并尝试将其加载到 json.i 时,
with open('file1.txt','r') as inputfile1:
new_file=json.dumps(inputfile1)
new_file=json.loads(new_file)
with open('file2.txt','r') as inputfile2:
new_file2=json.dumps(inputfile2)
new_file2=json.loads(new_file2)
print(type(new_file1))
print(type(new_file2))
我获取的是列表而不是 dict python 新手,有人可以帮忙吗
My files have data as
file1.txt
Student={'name'='john','age'=13,'roll_no':22445,'gender'='male'},{'name'='jimmy','age'=14,'roll_no':45622,'gender'='male'}
file2.txt
Address1={'name':'john','roll_no'=22445,'street':'st johns','pin':56902,'country':'US'},{'name':'jimmy','roll_no'=45622,'street':'st peters','pin':56502,'country':'US'}
when i read the file and try loading it to json.i was getting list instead of dict
with open('file1.txt','r') as inputfile1:
new_file=json.dumps(inputfile1)
new_file=json.loads(new_file)
with open('file2.txt','r') as inputfile2:
new_file2=json.dumps(inputfile2)
new_file2=json.loads(new_file2)
print(type(new_file1))
print(type(new_file2))
New to python, can someone help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
文件中的数据是无效的 json 字符串。在将数据写入文件之前,尝试使其有效 json。如果不可能,这里是如何修复它的示例:
Ur data into files is invalid json string. Try make better it valid json before write data into files. If it impossible, here is example how to fix it: