python - 重写 python 脚本以使用 python3

标签 python api

我有以下代码,我需要帮助重写它以与 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/

相关文章:

python - 在哪里可以找到 Flask SQLAlchemy 列类型和选项的列表?

Python 正则表达式 : get everything except for string

python - 在 Python 中对连续日期进行分组

python - 在 Python 中循环映射

python - 如何从 Shopify 调用此电话? (获取/发布)python

javascript - 如何使用 twilio-csharp 库发送 IP 消息

api - 使用 session 与 token 进行 API 身份验证

Python Flask-Mysql KeyError : 'MYSQL_HOST'

azure - 通过 postman 向 azure 进行 api 调用时获取 "Unable to complete authentication for user due to looping logins"

R/googlesheets4 非交互式 session