问题描述
,内置字符串编码 c $ c> string_escape 转义单引号,而Unicode一个则不转义。假设我可以简单的说是安全的:
>>> escaped = my_string.encode('unicode-escape')。replace(',\\)
...并获得预期的行为?
编辑:只是为了超清楚,预期的行为正在获得
根据我对 unicode-escape 和CPIX 2.6.5源中的unicode repr 是的; repr(unicode_string)和 unicode_string.encode('unicode-escape')之间的唯一区别是包含包装引用和转义使用了哪个报价。
它们都由同一个函数驱动, unicodeescape_string 。此函数采用一个参数,其唯一功能是切换添加引号和转义该引号。
According to the docs, the builtin string encoding string_escape:
...while the unicode_escape:
So, they should have roughly the same behaviour. BUT, they appear to treat single quotes differently:
>>> print """before '" \0 after""".encode('string-escape') before \'" \x00 after >>> print """before '" \0 after""".encode('unicode-escape') before '" \x00 after
The string_escape escapes the single quote while the Unicode one does not. Is it safe to assume that I can simply:
>>> escaped = my_string.encode('unicode-escape').replace("'", "\\'")
...and get the expected behaviour?
Edit: Just to be super clear, the expected behavior is getting something suitable as a literal.
According to my interpretation of the implementation of unicode-escape and the unicode repr in the CPython 2.6.5 source, yes; the only difference between repr(unicode_string) and unicode_string.encode('unicode-escape') is the inclusion of wrapping quotes and escaping whichever quote was used.
They are both driven by the same function, unicodeescape_string. This function takes a parameter whose sole function is to toggle the addition of the wrapping quotes and escaping of that quote.
这篇关于Python“string_escape” vs“unicode_escape”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!