Escape double quotes for JSON in Python

前端 未结 4 526
情话喂你
情话喂你 2020-12-03 04:04

How can I replace double quotes with a backslash and double quotes in Python?

>>> s = \'my string with \"double quotes\" blablabla\'
>>>          


        
4条回答
  •  北海茫月
    2020-12-03 05:00

    You should be using the json module. json.dumps(string). It can also serialize other python data types.

    import json
    
    >>> s = 'my string with "double quotes" blablabla'
    
    >>> json.dumps(s)
    <<< '"my string with \\"double quotes\\" blablabla"'
    

提交回复
热议问题