python - 如何使用 json.load 将信息从 txt 保存到字典

标签 python json dictionary dump

我正在尝试在字典上使用 json.load;但是,我收到此错误:

Traceback (most recent call last):
File "C:\Python34\Main.py", line 91, in
main()
File "C:\Python34\Main.py", line 87, in main
match_histories = json.load("match_histories.txt")
File "C:\Python34\lib\json__init__.py", line 265, in load
return loads(fp.read(),
AttributeError: 'str' object has no attribute 'read'

这是我的代码(我先导入了 json):

import json
match_histories = {}
match_histories["age"] = 25
json.dump(match_histories, "match_histories.txt")
match_histories = json.load(open("match_histories.txt"))

嗯,现在我在转储中遇到错误:

Traceback (most recent call last):
File "C:\Python34\Main.py", line 91, in
main()
File "C:\Python34\Main.py", line 82, in main
match_histories=get_match_histories(challenger_Ids)
File "C:\Python34\Main.py", line 50, in get_match_histories
json.dump(match_histories, "match_histories.txt")
File "C:\Python34\lib\json__init__.py", line 179, in dump
fp.write(chunk)
AttributeError: 'str' object has no attribute 'write'

在尝试了建议之后,我将代码更改为

import json
match_histories = {}
match_histories["age"] = 25
with open("match_histories.txt") as match_file:
    match_histories = json.dump(match_histories, "match_histories.txt")
with open("match_histories.txt") as match_file:
    match_histories = json.load(match_file)

但还是报错

File "C:\Python34\lib\json__init__.py", line 179, in dump
fp.write(chunk)
AttributeError: 'str' object has no attribute 'write'

(抱歉,我是 python 和编程新手)

最佳答案

AttributeError: 'str' object has no attribute 'read'

如您所见,它尝试在传递的对象上调用 read(),但您传递的是字符串对象而不是文件(或任何其他实现 read() 的对象) )

json.load() 想要一个文件对象,而不是它的路径字符串,所以使用 open() 打开文件然后传递它,或者类似的东西代码如下:

with open('file.json') as json_file:    
    json_data = json.load(json_file)

如果你正在打开要写入的文件,你应该将 w 模式设置为 open() 就像 open('file.json', ' w')

来自docs

json.load(fp, ...)

Deserialize fp (a .read()-supporting file-like object containing a JSON document) to a Python object using this conversion table.

你对 json.dump 也有同样的问题

AttributeError: 'str' object has no attribute 'write'

json.dump(obj, ...) Serialize obj as a JSON formatted stream to fp (a .write()-supporting file-like object) using this conversion table.

例子

import json
match_histories = {}
match_histories["age"] = 25

print 'Python dict is', match_histories

with open('match.json', 'w') as json_file:
    json.dump(match_histories, json_file)

with open('match.json', 'r') as json_file:
    match_histories = json.load(json_file)

print 'Python dict restored from JSON', match_histories

运行它

python jsontest.py

Python dict is {'age': 25}
Python dict restored from JSON {u'age': 25}

JSON

{"age": 25}

关于python - 如何使用 json.load 将信息从 txt 保存到字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31375514/

相关文章:

android - 使用 volley 发送 firebase 推送通知

javascript - 如何打印数组中列出的字典中的键和值,javascript

java - 使用 weka 和 python 加载分类器模型

python - Pandas 根据另一列的子字符串中的数字从子字符串切片中创建新列

python - 将 Python Pandas 数据框行与加法结合起来

arrays - jq 递归删除出现在架构中任意位置的数组中的值

php - 从另一个域获取 XML 文件到 Mysql 数据库

Python matplotlib 绘制具有多个值的字典

python - 使用map求嵌套列表的平均值

python - 将 python-dotenv 与 pytest 一起使用的最佳方法,或者使用单独配置的 pytest 测试/开发环境的最佳方法