如何删除那些“ \x00\x00” [英] How to remove those "\x00\x00"
本文介绍了如何删除那些“ \x00\x00”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何删除字符串中的 \x00\x00?
我有很多这样的字符串(示例如下所示)。我可以使用 re.sub
替换那些 \x00。但是我想知道是否有更好的方法呢? Unicode,字节和字符串之间的转换总是令人困惑。
How to remove those "\x00\x00" in a string ?
I have many of those strings (example shown below). I can use re.sub
to replace those "\x00". But I am wondering whether there is a better way to do that? Converting between unicode, bytes and string is always confusing.
'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
字符串末尾的字符。
It removes all \x00
characters at the end of the string.
这篇关于如何删除那些“ \x00\x00”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文