简体   繁体   中英

Python remove everything in bytestring before certain character

I have the following bytestring b'removethis\\x00\\x002020.10.14\\x00\\xf2\\x00^\\n\\x84>\\x01\\x00\\x10\\x01\\x14\\x00\\x00\\x00\\x8d\\xec\\xdc0\\x1bo\\xe7\\x15^\\n\\x84>\\x01\\x00\\x10\\x01\\x04\\x9b_\\x18'

i want everything before the first \\x00 removed so the substring removethis gets removed basically.

So the problem is that i only want everything before the first \\x00 to get removed not any future \\x00 's that may come in the string and im not sure how to do this.

You can build a regex for this.

import re

txt = "b'removethis\x00\x002020.10.14\x00\xf2\x00^\n\x84>\x01\x00\x10\x01\x14\x00\x00\x00\x8d\xec\xdc0\x1bo\xe7\x15^\n\x84>\x01\x00\x10\x01\x04\x9b_\x18'"
x = re.search("\\x00.*", txt)
print(x)

Here we find the first occurance of \\x00 and then take everything to the end of the string as well using .* . The result is your string minus 'removethis'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM