首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从python 3的byte中删除一些特定的内容

从python 3的byte中删除一些特定的内容
EN

Stack Overflow用户
提问于 2018-08-08 19:29:12
回答 2查看 5.3K关注 0票数 2

我不知道如何从下面的byte.Currently中删除'\x00‘,我正在尝试将这个字节写入Python3的文本文件中。

代码语言:javascript
复制
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。请帮帮我!

EN

回答 2

Stack Overflow用户

发布于 2018-08-08 20:00:54

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

代码语言:javascript
复制
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.')
票数 2
EN

Stack Overflow用户

发布于 2018-08-08 20:01:53

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

代码语言:javascript
复制
>>> my_bytes = b'blah\x00\x00\x00'
>>> my_bytes.rstrip(b'\x00')
b'blah'
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51745600

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档