【问题标题】:Delete some specific content from byte in python 3从python 3中的字节中删除一些特定内容
【发布时间】:2018-08-08 11:29:11
【问题描述】:

我不知道如何从以下字节中删除 '\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。请帮帮我!

【问题讨论】:

    标签: python string python-3.x byte


    【解决方案1】:

    使用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.')
    

    【讨论】:

    • 以防万一这适用于其他情况,replace() 将替换流中任何位置的特定序列,而不仅仅是从末尾删除。
    • Replace 可以替换这些,但我使用 assert 得到了 AssertionError。所以,我不使用 assert 并使用 rstrip(' ') 来删除替换 '\x00' 的空间。它可以工作.
    • @MKM2 我将b'\x00' 替换为空字符串b''。字符串末尾不应有任何空格。
    • @PatrickHaugh是的。如果句子后面有一些空格,这些空格也会被替换。所以,我将这个字节串反向循环,并将'\x00'一个一个删除,直到没有'\ x00'。
    【解决方案2】:

    Bytes 对象的行为与许多其他可迭代对象一样,这意味着切片和索引应该按预期工作。由于您要删除的字符专门位于末尾并且对象支持该方法,因此解决方案与从字符串末尾删除字符相同。只需确保传递所需的字符是字节即可。

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

    【讨论】:

    • 在 Python 3 中,使用 rstrip(b'\x00') 后,'\x00' 仍保留在字节字符串中。它仅适用于空格等空白字符。我认为 rstrip 不适用于 '\x00'。
    • 它确实有效。微妙之处在于 rstrp() 创建了一个新的字节对象,您必须将其存储在某个地方才能看到差异。所以,你真正想要的是my_bytes = my_bytes.rstrip(b'\x00')
    • 是啊!!!有用。起初我认为我只测试了 my_bytes.rstrip(b'\x00') 而没有分配新结果。
    猜你喜欢
    • 2022-12-02
    • 2021-12-21
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多