我如何将'\xff\xfe'转义为可读字符串 [英] how can i escape '\xff\xfe' to a readable string

查看:2312
本文介绍了我如何将'\xff\xfe'转义为可读字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这段代码中看到一个字符串:

i see a string in this code:

data[:2] == '\xff\xfe'

我不知道'\xff\xfe'是什么,

i don't know what '\xff\xfe' is,

所以我想逃避它,但不成功

so i want to escape it ,but not successful

import cgi
print cgi.escape('\xff\xfe')#print \xff\xfe

我如何得到它。

谢谢

推荐答案

你不能您应该明白,您正在使用字符串不是字节流,而是

You cannot escape or encode an invalid string.

有一些你不能接受的字符,其中首先是 0x00 - 而且你的例子是一个 BOM 序列。

You should understand that you are working with strings and not byte streams and there are some characters you cannot accept in them, first of them being 0x00 - and also your example that is happening to be a BOM sequence.

所以如果你需要包含非有效的字符串字符(unicode或ascii)你将不得不停止使用字符串。

So if you need to include non-valid strings characters (unicode or ascii) you will have to stop using strings for this.

看看 PEP-0358

这篇关于我如何将'\xff\xfe'转义为可读字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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