Home  >  Q&A  >  body text

python - 字符串中反斜杠的替换

在模仿一个客户端的POST行为。

构造表单并抓包后,发现自己构造的请求字符串与官方客户端的并不一致。

官方的是:

{"jsonstr":
"{\"pageindex\":1,\"start\":\"2017-03-01\",\"end\":\"2017-03-25\"}"
}

我用python构造的是:

{"jsonstr": 
"{'pageindex': 1,'start': '2017-03-01', 'end': '2017-03-25'}"
}

用python的replace把“ ' ” 替换为 ' 反斜杠" ' 总是不成功

伊谢尔伦伊谢尔伦2741 days ago685

reply all(1)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:30:51

    You can directly use the Json library that comes with Python

    In [9]: s = {'pageindex':1,"start":"2017-03-01","end":"2017-03-25"}
    
    In [10]: import json
    
    In [11]: json.dumps(s)
    Out[11]: '{"end": "2017-03-25", "pageindex": 1, "start": "2017-03-01"}'

    In fact, out[11] is equivalent to the official string, because Python strings can be surrounded by single quotes (in this case, double quotes within the string do not need to be escaped)

    reply
    0
  • Cancelreply