【问题标题】:PYTHON | Replace json response with custom text蟒蛇 |用自定义文本替换 json 响应
【发布时间】:2022-01-10 16:54:29
【问题描述】:

所以我有一个简单的 python 脚本来打印网站发布请求的状态代码...

这是我得到的输出:

{"success": false, "error": "Incomplete request"}

但我希望它打印出来:

Error! Incomplete Request

这可能吗?谢谢!

【问题讨论】:

  • 这不是很简单吗?是的,有可能。

标签: python json post request


【解决方案1】:

Python3:

if not response['success']:
    print(f"Error! {response['error']}")

python2:

if not response['success']:
    print("Error! {0}".format(response['error']))

【讨论】:

    【解决方案2】:

    你的意思是这样的:

    # suppose response={"success": false, "error": "Incomplete request"}
    # I suppose false is False since the dict should have been decoded from json to Python
    if (not response['success'] and response['error'] == 'Incomplete request'):
        print('Error! Incomplete request')
    

    【讨论】:

    • @WooxHimself,如果有效,您可以接受答案。无论如何,在提出新的直截了当的问题之前,您应该阅读 StackOverflow 的问题指南。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-07
    • 2021-12-26
    • 2010-10-27
    • 1970-01-01
    相关资源
    最近更新 更多