我有以下代码,我需要帮助重写它以与 python3 一起使用
from urllib2 import Request, urlopen
headers = {'Authorization': '< Enter your Basic Authorization string here >'}
request = Request('https://ssapi.shipstation.com/orders/orderId', headers=headers)
request.get_method = lambda: 'DELETE'
response_body = urlopen(request).read()
print response_body
最佳答案
更改导入
try:
from urllib2 import Request, urlopen
except ImportError:
# python3
from urllib.request import Request, urlopen
同时更改打印
print(response_body)
您的代码现在是交叉兼容的,并且应该可以在 Python 2 和 Python 3 上运行。
关于python - 重写 python 脚本以使用 python3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44442171/