What does the 'b' character do in front of a string literal?

前端 未结 9 767
醉梦人生
醉梦人生 2020-11-21 05:07

Apparently, the following is the valid syntax:

my_string = b\'The string\'

I would like to know:

  1. What does this b
9条回答
  •  旧巷少年郎
    2020-11-21 05:28

    You can use JSON to convert it to dictionary

    import json
    data = b'{"key":"value"}'
    print(json.loads(data))
    

    {"key":"value"}


    FLASK:

    This is an example from flask. Run this on terminal line:

    import requests
    requests.post(url='http://localhost(example)/',json={'key':'value'})
    

    In flask/routes.py

    @app.route('/', methods=['POST'])
    def api_script_add():
        print(request.data) # --> b'{"hi":"Hello"}'
        print(json.loads(request.data))
    return json.loads(request.data)
    

    {'key':'value'}

提交回复
热议问题