python - 按条件删除部分列表?
问题描述
目前我已经修复了从可执行文件中提取字符串列表的工具,我无法直接访问该工具的源代码来改变它的行为。
该工具还将从可执行文件中提取大量 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>',
]
但在列表中保留其他字符串。
寻找一些建议来完成这项工作。
解决方案
例如数据,您似乎必须保留所有数据,直到符合<?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
来控制列表的哪一部分保留以及跳过哪一部分。只有keep
在True
开始时您设置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)
推荐阅读
- facebook - 浏览器插件如何在没有选择加入的情况下获取 Facebook 中的群组成员详细信息?
- javascript - jsTree排序函数回收
- php - 如何在 Wordpress 上的帖子标题后添加内容?仅在帖子标题上
- visual-studio-2019 - Visual Studio 2019 16.6.1 缓慢且无响应
- soap - WSO2 Enterprise Integrator:在数据服务上启用安全性:无法对安全令牌进行身份验证或授权
- validation - 是否可以在 Yii2 中将自定义验证器与唯一验证器一起使用或在其内部使用?
- c# - 在同一个类中使用来自另一个方法的变量-c#
- firebase - 为什么我的 React Native FlatList keyExtractor 返回未定义?
- django - 如何使用序列化仅序列化某些字段?
- c - 解决在两个不同操作系统中定义的结构的重新定义