Skip to main content
 首页 » 编程设计

python之.strip() 方法不去除神秘空白字符

2025年05月04日21zdz8207

我正在从这样的文件中读取一些 utf-8 编码的数据:

with open (filename, 'rb') as f: 
    bytes= f.read(offset, length) 
    #bytes is b'hello\x00\x00\x00\x00' 
    text = bytes.decode('utf-8') 
    #text is 'hello    ' 
    stripped_text = text.strip() 
    #stripped_text is 'hello    ' 

您可以使用像

这样的简单行重新创建它
thing = b'hello\x00\x00\x00\x00'.decode('utf8').strip() 
print(thing) 
#the output is 'hello    ' 

如您所见,未删除尾随的 nul 字符 - 我认为这与 .strip() 未识别的 '\x00' 有关,但我看起来似乎认为应该如此。是什么赋予了?我怎样才能删除这些字符而不必做一些非常笨拙的事情?

我找不到解决此问题的帖子。

请您参考如下方法:

NUL 不是空格,因此没有参数的 strip() 不会去除它们。您应该改为使用 strip('\0'):

>>> 'hello\0\0\0\0'.strip('\0') 
'hello'