python将双反斜杠“\\“替换成单反斜杠“\“
python将双反斜杠"\\"替换成单反斜杠"\"
·
在调用接口时,可能接口要求在参数中传递"\",示例代码如下:
import json
mcuid = 'safsdfass'
oper_type = 1
pos_id = 1
body = {"appId":131312324,"endpointOS":1,"clientVersion":"","deviceId":"fasdfasdfa","mcuid":"111","datatype":1,"datapoint":"{\"index\":1,\"status\":0}","context":"sssssss"}
body["mcuid"] = mcuid
# if 1 == oper_type :
if oper_type in [1,3] :
if 1 == oper_type :
body["datapoint"] = "{\\\"index\\\":1,\\\"status\\\":0}"
else:
body["datapoint"] = "{\\\"index\\\":" + str(int(pos_id) + 1) + ",\\\"status\\\":0}"
# elif 2 == oper_type :
elif oper_type in [2,4] :
if 2 == oper_type:
body["datapoint"] = "{\\\"index\\\":1,\\\"status\\\":1}"
else:
body["datapoint"] = "{\\\"index\\\":" + str(int(pos_id)+1) + ",\\\"status\":1}"
re = json.dumps(body)
print(re)
bb = re.replace('\\\\', '\\').replace('\\\\', '\\')
print(bb)
执行截图如下:
更多推荐
所有评论(0)