.strip() 方法不去除神秘空白字符
.strip() method not stripping mystery whitespace characters
我正在从这样的文件中读取一些 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' 有关,但我看起来似乎认为它应该是。是什么赋予了?我怎样才能删除这些字符而不必做一些非常笨拙的事情?
我找不到解决此问题的 post。
NUL 不是空格,因此 strip()
没有参数不会删除它们。您应该改用 strip('[=12=]')
:
>>> 'hello[=10=][=10=][=10=][=10=]'.strip('[=10=]')
'hello'
我正在从这样的文件中读取一些 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' 有关,但我看起来似乎认为它应该是。是什么赋予了?我怎样才能删除这些字符而不必做一些非常笨拙的事情?
我找不到解决此问题的 post。
NUL 不是空格,因此 strip()
没有参数不会删除它们。您应该改用 strip('[=12=]')
:
>>> 'hello[=10=][=10=][=10=][=10=]'.strip('[=10=]')
'hello'