python 如何将 '\xff\xfe' 转义为可读字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1979171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how can i escape '\xff\xfe' to a readable string
提问by zjm1126
i see a string in this code:
我在这段代码中看到一个字符串:
data[:2] == '\xff\xfe'
i don't know what '\xff\xfe' is,
我不知道 '\xff\xfe' 是什么,
so i want to escape it ,but not successful
所以我想逃避它,但没有成功
import cgi
print cgi.escape('\xff\xfe')#print \xff\xfe
how can i get it.
我怎么才能得到它。
thanks
谢谢
采纳答案by sorin
You cannot escape or encode an invalid string.
您无法对无效字符串进行转义或编码。
You should understand that you are working with stringsand not byte streamsand there are some characters you cannot accept in them, first of them being 0x00
- and also your example that is happening to be a BOMsequence.
您应该明白您正在使用字符串而不是字节流,并且其中有一些您无法接受的字符,首先是0x00
- 还有您的示例恰好是BOM序列。
So if you need to include non-valid strings characters (unicode or ascii) you will have to stop using strings for this.
因此,如果您需要包含无效的字符串字符(unicode 或 ascii),则必须停止为此使用字符串。
Take a look at PEP-0358
看看PEP-0358
回答by MatrixFrog
'\xFF' means the byte with the hex value FF. '\xff\xfe' is a byte-order mark: http://en.wikipedia.org/wiki/Byte_order_mark
'\xFF' 表示十六进制值为 FF 的字节。'\xff\xfe' 是一个字节顺序标记:http: //en.wikipedia.org/wiki/Byte_order_mark
You could also represent it as two separate characters but that probably won't tell you anything useful.
您也可以将其表示为两个单独的字符,但这可能不会告诉您任何有用的信息。
回答by Ignacio Vazquez-Abrams
>>> print '\xff\xfe'.encode('string-escape')
\xff\xfe
回答by John Machin
What is the connection between "i don't know what '\xff\xfe' is" and "so i want to escape it"? What is the purpose of "escaping" it?
“我不知道 '\xff\xfe' 是什么”和“所以我想逃避它”之间有什么联系?“逃避”的目的是什么?
It would help enormously if you gave a little more context than data[:2] == '\xff\xfe'
(say a few line before and after) ... however it looks like it is testing whether the first two bytes of data
could possibly represent an UTF-16 littleendian byte order mark. In that case you could do something like:
如果您提供的上下文比data[:2] == '\xff\xfe'
(例如前后几行)多一点,那将非常有帮助……但是看起来它正在测试 的前两个字节data
是否可能代表 UTF-16 littleendian 字节顺序标记。在这种情况下,您可以执行以下操作:
UTF16_LE_BOM = "\xff\xfe"
# much later
if data[:2] == UTF16_LE_BOM:
do_something()