在python中读取外部json文件的值时出现无效参数错误
我试过了:
import json
with open('https://www.w3schools.com/js/json_demo.txt') as json_file:
data = json.load(json_file)
#for p in data['people']:
print('Name: ' + data['name'])
给了我错误:
open('https://www.w3schools.com/js/json_demo.txt'( json_file: OSError: [Errno 22] 无效参数: "https://www.w3schools.com/js/json_demo.txt">
由于open
用于打开本地文件,而不是 jonrsharpe 评论的 URL,因此请使用 fl00r 注释的 urllib。
虽然他提供的链接是python-2
试试这个 (python-3
(:
import json
from urllib.request import urlopen
with urlopen('https://www.w3schools.com/js/json_demo.txt') as json_file:
data = json.load(json_file)
#for p in data['people']:
print('Name: ' + data['name'])
John
使用请求
import requests
response = requests.get('https://www.w3schools.com/js/json_demo.txt')
response.encoding = "utf-8-sig"
data = response.json()
print(data['name'])
>>> John