python - 字符串中反斜杠的替换
伊谢尔伦
伊谢尔伦 2017-04-18 10:28:51
0
1
683

在模仿一个客户端的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把“ ' ” 替换为 ' 反斜杠" ' 总是不成功

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(1)
伊谢尔伦

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)

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!