Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何删除这些“文件”\x00\x00“;_Python_String_Byte - Fatal编程技术网

Python 如何删除这些“文件”\x00\x00“;

Python 如何删除这些“文件”\x00\x00“;,python,string,byte,Python,String,Byte,如何删除字符串中的“\x00\x00”? 我有许多这样的字符串(如下所示的示例)。我可以使用re.sub替换那些“\x00”。但我想知道是否有更好的方法?在unicode、字节和字符串之间转换总是令人困惑 'Hello\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\

如何删除字符串中的“\x00\x00”? 我有许多这样的字符串(如下所示的示例)。我可以使用
re.sub
替换那些“\x00”。但我想知道是否有更好的方法?在unicode、字节和字符串之间转换总是令人困惑

'Hello\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'.

使用
rstrip

>>> text = 'Hello\x00\x00\x00\x00'
>>> text.rstrip('\x00')
'Hello'

它删除字符串末尾的所有
\x00
字符。

我认为更通用的解决方案是使用:

>>> a = 'Hello\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' 
>>> a.replace('\x00','')
'Hello'
cleanstring = nullterminatedstring.split('\x00',1)[0]
它将
拆分
字符串,使用
\x00
作为delimeter
1
时间。
split(…)
返回一个2元素列表:null之前的所有内容以及null之后的所有内容(它删除delimeter)。追加
[0]
只返回第一个空(\x00)字符之前的字符串部分,我相信这就是您要查找的内容

在某些语言中,特别是类似C的语言中,约定是一个空字符标记字符串的结尾。例如,您还应该看到如下字符串:

'Hello\x00dpiecesofsomeoldstring\x00\x00\x00'

这里提供的答案将处理这种情况以及其他示例。

基于提供的答案,我建议strip()在清理数据包方面比rstrip()更通用,因为strip()从提供的字符串的开头和结尾移除字符,而rstrip()则只需从字符串末尾删除字符

>>> a = 'Hello\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' 
>>> a.replace('\x00','')
'Hello'
但是,默认情况下,strip()不会将NUL字符视为空白,因此需要显式指定。这可能会让您大吃一惊,因为print()当然不会显示NUL字符。我使用的解决方案是使用“
.strip().strip('\x00')
”清理字符串:


这为您提供了所需的字符串/字节数组,每端没有NUL字符,并且还保留了“数据包”中的任何NUL字符,这对于接收到的可能包含有效NUL字符的字节数据(例如C类型结构)非常有用。注意。在这种情况下,数据包必须被“包装”,即被非NUL字符(前缀和后缀)包围,以允许正确检测,从而只剥离不需要的NUL字符。

我尝试了
剥离
rstrip
,但它们不起作用,但这一次成功了; 使用
split
然后
join
结果
list

if '\x00' in name:
    name=' '.join(name.split('\x00'))

除了从字符串中删除\x00之外,您可能还需要考虑一下为什么首先要使用它们。也许构建字符串的代码可以处理它们?您编写的生成此字符串的代码可能是错误的。@Neil,这是因为我是一个C-wrapper,我应该为字符串提供最大可能长度。对于那些短的字符串,“\x00”s在末尾被追加。@ LuffyCyliu,在C包装器中,考虑在将char数组转换为Python对象时包含数据的实际长度。数组上的code>strlen
将比Python str.rstrip或str.replace更快,并使Python界面更直观。也许和
PyBytes\u FromStringAndSize()
会有帮助。很好的回答,对我帮助很大。顺便说一句,值得一提的是,在某些情况下,您需要按b'\x00'而不是'\x00'进行拆分(如果您实际使用的是字节,在这种情况下可能会发生这种情况),这应该是可接受的答案,问题是如何从字符串中删除模式,而不是像当前可接受的答案那样从“字符串结尾”中删除模式,或者“字符串的结束和开始”等。