使用python从json文件读取数据

2024-05-16 10:02:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试使用python 3.8从json文件读取数据,但我总是收到此回溯错误

Traceback (most recent call last):
  File "C:/Users/Powerful Richie/Desktop/My Python Codes/roster.py", line 1, in <module>
    import json
  File "C:/Users/Powerful Richie/Desktop/My Python Codes\json.py", line 11, in <module>
    uh = urllib.request.urlopen(address)
  File "C:\Users\Powerful Richie\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\Powerful Richie\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 509, in open
    req = Request(fullurl, data)
  File "C:\Users\Powerful Richie\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 328, in __init__
    self.full_url = url
  File "C:\Users\Powerful Richie\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 354, in full_url
    self._parse()
  File "C:\Users\Powerful Richie\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 383, in _parse
    raise ValueError("unknown url type: %r" % self.full_url)
ValueError: unknown url type: 'roster_data.json'

谁能帮我纠正这个错误?


Tags: inpyurlrequestliblocallineurllib
1条回答
网友
1楼 · 发布于 2024-05-16 10:02:09

我希望您正在添加要查找的文件的完整位置

import json

with open('path_to_file/roster_data.json') as f:
data = json.load(f)

print(data)

它不应该只是花名册_data.json或检查文件的正确位置

相关问题 更多 >