Python“string_escape” vs“unicode_escape” [英] Python "string_escape" vs "unicode_escape"

查看:203
本文介绍了Python“string_escape” vs“unicode_escape”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文档,内置字符串编码 string_escape


生成适合作为Python源代码中的字符串字符串的字符串


...而 unicode_escape


在Python源代码中生成适合Unicode字符串的字符串



$ b $所以,他们应该有大致相同的行为。但是,他们似乎以不同的方式处理单引号:

 >>>在$$之后的\\之前打印。$之前的$$$ \\在之前的'\0之前打印。在


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:

Produce[s] a string that is suitable as string literal in Python source code

...while the unicode_escape:

Produce[s] a string that is suitable as Unicode literal in Python source code

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”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆