user2292661 user2292661 - 2 years ago 274
Python Question

Best way to replace \x00 in python lists?

I have a list of values from a parsed PE file that include /x00 null bytes at the end of each section. I want to be able to remove the /x00 bytes from the string without removing all "x"s from the file. I have tried doing .replace and re.sub, but not which much success.

Using Python 2.6.6


import re

List = [['.text\x00\x00\x00'], ['.data\x00\x00\x00'], ['.rsrc\x00\x00\x00']]

while count < len(List):
test = re.sub('\\\\x00', '', str(list[count])
print test
count += 1

>>>tet (removes x, but I want to keep it)

I want to get the following output


Any ideas on the best way of going about this?

Answer Source
>>> L = [['.text\x00\x00\x00'], ['.data\x00\x00\x00'], ['.rsrc\x00\x00\x00']]
>>> [[x[0]] for x in L]
[['.text\x00\x00\x00'], ['.data\x00\x00\x00'], ['.rsrc\x00\x00\x00']]
>>> [[x[0].replace('\x00', '')] for x in L]
[['.text'], ['.data'], ['.rsrc']]

Or to modify the list in place instead of creating a new one:

for x in L:
    x[0] = x[0].replace('\x00', '')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download