use*_*661 9 python regex replace
我有一个解析的PE文件的值列表,在每个部分的末尾包含/ x00空字节.我希望能够从字符串中删除/ x00字节而不从文件中删除所有"x".我曾尝试过.replace和re.sub,但没有那么多成功.
使用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
>>>test (removes x, but I want to keep it) #changed from tet to test
>>>data
>>>rsrc
Run Code Online (Sandbox Code Playgroud)
我想获得以下输出
文本数据rsrc
有关最佳方式的任何想法吗?
jam*_*lak 11
>>> 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']]
Run Code Online (Sandbox Code Playgroud)
或者修改列表而不是创建新列表:
for x in L:
x[0] = x[0].replace('\x00', '')
Run Code Online (Sandbox Code Playgroud)
lst = (i[0].rstrip('\x00') for i in List)
for j in lst:
print j,
Run Code Online (Sandbox Code Playgroud)