首页 > 解决方案 > 按条件删除部分列表?

问题描述

目前我已经修复了从可执行文件中提取字符串列表的工具,我无法直接访问该工具的源代码来改变它的行为。

该工具还将从可执行文件中提取大量 XML、HTML 标记内容,这对我当前的 ML 算法来说是噪声。

我想从列表中删除那些 XML、HTML 标记内容,但保留其他字符串,例如,我有以下字符串列表:

["S'&W ",
 'GetModuleHandleA',
  ......
 'CoInitialize',
 'SHELL32.DLL',
 'ShellExecuteExA',
 'SHLWAPI.DLL',
 'PathQuoteSpacesA',
 '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>',
 '<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">',
 '  <assemblyIdentity',
 '    version="1.0.0.0"',
 '    processorArchitecture="X86"',
 '    name="CompanyName.ProductName.YourApp"',
 '    type="win32" />',
 '  <description></description>',
 '  <dependency>',
 '    <dependentAssembly>',
 '      <assemblyIdentity',
 '        type="win32"',
 '        name="Microsoft.Windows.Common-Controls"',
 '        version="6.0.0.0"',
 '        processorArchitecture="X86"',
 '        publicKeyToken="6595b64144ccf1df"',
 '        language="*" />',
 '    </dependentAssembly>',
 '  </dependency>',
 '</assembly>',
 '']

我想删除那些属于 XML 标记的字符串:

['<?xml version="1.0" encoding="UTF-8" standalone="yes"?>',
 '<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">',
 '  <assemblyIdentity',
 '    version="1.0.0.0"',
 '    processorArchitecture="X86"',
 '    name="CompanyName.ProductName.YourApp"',
 '    type="win32" />',
 '  <description></description>',
 '  <dependency>',
 '    <dependentAssembly>',
 '      <assemblyIdentity',
 '        type="win32"',
 '        name="Microsoft.Windows.Common-Controls"',
 '        version="6.0.0.0"',
 '        processorArchitecture="X86"',
 '        publicKeyToken="6595b64144ccf1df"',
 '        language="*" />',
 '    </dependentAssembly>',
 '  </dependency>',
 '</assembly>',
]

但在列表中保留其他字符串。

寻找一些建议来完成这项工作。

标签: pythonstringlist

解决方案


例如数据,您似乎必须保留所有数据,直到符合<?xml>

所以你可以逐行获取并放入新列表,直到你

new_data = []

for line in data:
    if line.startswith('<?xml'):
        break
    new_data.append(line)

如果您有更复杂的数据,那么您可能需要检查其他单词并跳过它们。您也可以在删除空格后检查它。但这需要很长的单词列表(我只显示 3 个项目)

for line in data:
    if not line.strip().startswith( ('<', 'version=', 'processorArchitecture=' ) ):
       new_data.append(line)

或者您可以检查线是否以或以或以内部开始<或结束>=

new_data = []

for line in data:
    temp = line.strip()
    if not temp.startswith('<') and not temp.endswith('>') and ('=' not in temp):
        new_data.append(line)

data = ["S'&W ",
 'GetModuleHandleA',

 'CoInitialize',
 'SHELL32.DLL',
 'ShellExecuteExA',
 'SHLWAPI.DLL',
 'PathQuoteSpacesA',
 '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>',
 '<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">',
 '  <assemblyIdentity',
 '    version="1.0.0.0"',
 '    processorArchitecture="X86"',
 '    name="CompanyName.ProductName.YourApp"',
 '    type="win32" />',
 '  <description></description>',
 '  <dependency>',
 '    <dependentAssembly>',
 '      <assemblyIdentity',
 '        type="win32"',
 '        name="Microsoft.Windows.Common-Controls"',
 '        version="6.0.0.0"',
 '        processorArchitecture="X86"',
 '        publicKeyToken="6595b64144ccf1df"',
 '        language="*" />',
 '    </dependentAssembly>',
 '  </dependency>',
 '</assembly>',
 '']

print('--- version 1 ---')

new_data = []

for line in data:
    if line.startswith('<?xml'):
        break
    new_data.append(line)

for line in new_data:
    print(line)

print('--- version 2 ---')

new_data = []

for line in data:
    temp = line.strip()
    if not temp.startswith('<') and not temp.endswith('>') and ('=' not in temp):
        new_data.append(line)

for line in new_data:
    print(line)

编辑:

另一种方法是使用变量 iekeep = True来控制列表的哪一部分保留以及跳过哪一部分。只有keepTrue

开始时您设置keep = True. 当你找到<?xml然后你设置keep = False。当你发现关闭'</assembly>'>然后你再次设置keep = True。如果你总是xml有缩进,那么你甚至可以检查行是否</以获取最后一行xml

data = [
 "S'&W ",
 'GetModuleHandleA',
 'CoInitialize',
 'SHELL32.DLL',
 'ShellExecuteExA',
 'SHLWAPI.DLL',
 'PathQuoteSpacesA',
 '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>',
 '<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">',
 '  <assemblyIdentity',
 '    version="1.0.0.0"',
 '    processorArchitecture="X86"',
 '    name="CompanyName.ProductName.YourApp"',
 '    type="win32" />',
 '  <description></description>',
 '  <dependency>',
 '    <dependentAssembly>',
 '      <assemblyIdentity',
 '        type="win32"',
 '        name="Microsoft.Windows.Common-Controls"',
 '        version="6.0.0.0"',
 '        processorArchitecture="X86"',
 '        publicKeyToken="6595b64144ccf1df"',
 '        language="*" />',
 '    </dependentAssembly>',
 '  </dependency>',
 '</assembly>',
 'VARIABLE-AFTER-XML-1',
 'VARIABLE-AFTER-XML-2',
 'VARIABLE-AFTER-XML-3',
]

print('--- version 3 ---')

new_data = []
keep = True

for line in data:
    if line.startswith('<?xml'):
        keep = False

    if keep:
        new_data.append(line)

    #if line == '</assembly>':
    if line.startswith('</'):
        keep = True
        
for line in new_data:
    print(line)

推荐阅读