Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.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 3中字节中的某些特定内容_Python_String_Python 3.x_Byte - Fatal编程技术网

删除python 3中字节中的某些特定内容

删除python 3中字节中的某些特定内容,python,string,python-3.x,byte,Python,String,Python 3.x,Byte,我不知道如何从下面的字节中删除“\x00”。目前,我正在尝试将此字节写入python 3中的文本文件 b'Today, in the digital age, any type of data, such as text, images, and audio, can be\r\ndigitized, stored indefinitely, and transmitted at high speeds. Notwithstanding these\r\nadvantages, digita

我不知道如何从下面的字节中删除“\x00”。目前,我正在尝试将此字节写入python 3中的文本文件

b'Today, in the digital age, any type of data, such as text, images, and 
audio, can be\r\ndigitized, stored indefinitely, and transmitted at high 
speeds. Notwithstanding these\r\nadvantages, digital data also have a 
downside. They are easy to access illegally, tamper\r\nwith, and copy for 
purposes of copyright violation.\r\nThere is therefore a need to hide secret 
identification inside certain types of digital\r\ndata. This information can 
be used to prove copyright ownership, to identify attempts\r\nto tamper with 
sensitive data, and to embed annotations. Storing, hiding, or embedding\r
\nsecret information in all types of digital data is one of the tasks of the 
field of\r\nsteganography.\r\nSteganography is the art and science of data 
hiding. In contrast with cryptography,\r\nwhich secures data by transforming 
it into another, unreadable format, steganography\r\nmakes data invisible by 
hiding (or embedding) them in another piece of data, known\r\nalternatively as
 the cover, the host, or the carrier. The modified cover, including 
the\r\nhidden data, is referred to as a stego object. It can be stored or
 transmitted as a message.\r\nWe can think of cryptography as overt secret 
writing and of steganography as covert\r\nsecret writing.\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00'
我想从句子末尾删除多个\x00。请帮帮我

使用bytes.replace将子字符串替换为空字符串:

b = b'Today, in the digital age, any type of data, such as text, images, and audio, can be\r\ndigitized, stored indefinitely, and transmitted at high speeds. Notwithstanding these\r\nadvantages, digital data also have a downside. They are easy to access illegally, tamper\r\nwith, and copy for purposes of copyright violation.\r\nThere is therefore a need to hide secret identification inside certain types of digital\r\ndata. This information can be used to prove copyright ownership, to identify attempts\r\nto tamper with sensitive data, and to embed annotations. Storing, hiding, or embedding\r\nsecret information in all types of digital data is one of the tasks of the field of\r\nsteganography.\r\nSteganography is the art and science of data hiding. In contrast with cryptography,\r\nwhich secures data by transforming it into another, unreadable format, steganography\r\nmakes data invisible by hiding (or embedding) them in another piece of data, known\r\nalternatively as the cover, the host, or the carrier. The modified cover, including the\r\nhidden data, is referred to as a stego object. It can be stored or transmitted as a message.\r\nWe can think of cryptography as overt secret writing and of steganography as covert\r\nsecret writing.\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

b = b.replace(b'\x00', b'')
assert b.endswith(b'writing.')

字节对象的行为与许多其他iterables类似,这意味着切片和索引应该按预期工作。由于要删除的字符具体位于末尾,并且对象支持该方法,因此解决方案与从字符串末尾剥离字符的方法相同。只需确保传递所需的字符是字节

>>> my_bytes = b'blah\x00\x00\x00'
>>> my_bytes.rstrip(b'\x00')
b'blah'

为了防止这适用于其他情况,replace将替换流中任意位置的特定序列,而不仅仅是从末尾删除它。replace可以替换这些序列,但我使用assert获得了AssertionError。因此,我没有使用assert和rstrip“”来删除替换“\x00”所产生的空间。它是有效的。@MKM2我用b替换了b“\x00”,b是空字符串。字符串末尾不应该有空格。@PatrickHaughYes。如果句子后面有空格,这些空格也将被替换。因此,我反向循环此字节字符串并逐个删除“\x00”,直到没有“\x00”。在Python 3中,使用rstripb“\x00”后,“\x00”仍保留在字节字符串中。它只适用于空格之类的空白字符。我认为rstrip不适用于“\x00”。它确实有效。微妙之处在于rstrp创建了一个新的bytes对象,您必须将其存储在某个地方才能看到差异。所以,你真正想要的是my_bytes=my_bytes.rstripb'\x00'。是的!!!它起作用了。起初,我认为我只是像我的_bytes.rstripb'\x00'那样进行测试,而没有指定新的结果。