让python忽略字符串中的转义字符

Posted by Tesla9527 on August 1, 2018

在字符串中使用特殊字符时,python用反斜杠转义字符,但有时我们并不想让转义字符生效,我们只想所见即所得,这种情况下可以用r来定义原始字符串

不使用r时

1
print('{"CreditScore": "{\"success\":true,\"biz_no\":\"ZM201807313000000874700550325893\",\"zm_score\":\"577\"}"}')

输出

1
{"CreditScore": "{"success":true,"biz_no":"ZM201807313000000874700550325893","zm_score":"577"}"}

使用r时

1
print(r'{"CreditScore": "{\"success\":true,\"biz_no\":\"ZM201807313000000874700550325893\",\"zm_score\":\"577\"}"}')

输出

1
{"CreditScore": "{\"success\":true,\"biz_no\":\"ZM201807313000000874700550325893\",\"zm_score\":\"577\"}"}