'str' object has no attribute 'read'
时间: 2023-08-05 11:04:43 浏览: 295
这个错误通常是因为在使用json.load()函数时,传入的参数是一个字符串而不是一个文件对象。正确的用法是使用open()函数打开文件并将文件对象传递给json.load()函数。以下是几种解决方法:
方法一:
```python
import json
with open("社区图层_wgs84_修正街道ID.geojson", encoding='utf8') as f:
data = json.load(f)
```
方法二:
```python
import json
f = open('AlphaPose-pytorch/examples/res/alphapose-results.json')
data = json.load(f)
f.close()
```
方法三:
```python
import json
with zfile.open(name, mode='r') as json_file:
json_bytes = str(json_file.read())
json_list = json_bytes.split("\\r\\n")
for i in json_list\[0:1\]:
python_obj = json.loads(i)
```
请注意,方法三中使用的是json.loads()函数而不是json.load()函数,因为它接受一个字符串作为参数而不是文件对象。希望这些解决方法能够帮助到你解决这个问题。\[1\]\[2\]\[3\]
#### 引用[.reference_title]
- *1* [python读取json文件报错“AttributeError: ‘str‘ object has no attribute ‘read‘”](https://blog.csdn.net/u014543416/article/details/121950855)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [python读取json文件报错AttributeError: ‘str‘ object has no attribute ‘read](https://blog.csdn.net/weixin_43793510/article/details/117168969)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [已解决AttributeError: ‘str‘ object has no attribute ‘read](https://blog.csdn.net/yuan2019035055/article/details/127567798)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文