python:根据部分标题提取文件,标题中有拆分条件
python: extracting files based on partial title with split criteria in title
我有一个命名不一致的文件列表:
pLst = ['CO_002_2016_Q4_Merge.loc', 'CO_002_2016_Merge.zip', 'CO_002_2016_q4_alias.loc', 'CO_002_2017_here_2017_q1_streets_alias.loc.xml', 'CO_002_2017_here_2017_q1_streets_parity.loc', 'AuburnAliasGCS_1984_1106.lox', 'CA_ORG_BCP.loc.xml', 'CA_ORG_BCP.loc', 'Co52 Alias Address Locator.lox', 'CO_002_2017_here_2017_q1_streets_parity.loc.xml', 'CentralCostaCountyStreets.lox', 'CO_002_2016_q4_alias.lox']
我想提取包含此列表中的字符串的文件:
exCrt = ["2016_Q4", "2016_q4","2017"]
我想将包含 exCrt 中任何元素的所有文件添加到提取列表中,但我没有看到将拆分标准与部分选择标准(即“_”)隔离开来的方法.
我尝试使用任何:
if any(x in pLst for x in exCrt):
exLst.add(x)
这导致了一个空集。
我还尝试将 exCrt 更改为 ["2016","q4","Q4","2017"]
,然后使用 'and' 'or' 方法:
for i in pLst:
if exCrt[0] and exCrt[1] or exLst[0] and exCrt[2] or exCrt[3] in i.split("_"):
exLst.add(i)
但这并没有排除任何不需要的文件。
我希望输出为
( 'CO_002_2016_Q4_Composite.loc',
'CO_002_2016_q4_alias.loc.xml',
'CO_002_2016_Q4_Composite.loc.xml',
'CO_002_2016_Q4_Merge.lox',
'CO_002_2016_Q4_Merge.loc.xml',
'CO_002_2016_Q4_Merge.loc',
'CO_002_2016_q4_alias.loc',
'CO_002_2016_q4_alias.lox',
'CO_002_2017_here_2017_q1_streets_alias.lox',
'CO_002_2017_here_2017_q1_streets_alias.loc',
'CO_002_2017_here_2017_q1_streets_alias.loc.xml',
'CO_002_2017_here_2017_q1_streets_parity.loc',
'CO_002_2017_here_2017_q1_streets_parity.loc.xml')
如果您按如下方式调整,您使用 any
的方法将会奏效:
exCrt = ["2016_Q4", "2016_q4", "2017"]
exLst = []
for p in pLst:
if any(x in p for x in exCrt):
exLst.append(p)
您可以使用列表理解:
pLst = ['CO_002_2016_Q4_Merge.loc', 'CO_002_2016_Merge.zip', 'CO_002_2016_q4_alias.loc', 'CO_002_2017_here_2017_q1_streets_alias.loc.xml', 'CO_002_2017_here_2017_q1_streets_parity.loc', 'AuburnAliasGCS_1984_1106.lox', 'CA_ORG_BCP.loc.xml', 'CA_ORG_BCP.loc', 'Co52 Alias Address Locator.lox', 'CO_002_2017_here_2017_q1_streets_parity.loc.xml', 'CentralCostaCountyStreets.lox', 'CO_002_2016_q4_alias.lox']
exCrt = ["2016_Q4", "2016_q4", "2017"]
final_pLst = [i for i in pLst if any(b in i for b in exCrt)]
输出:
['CO_002_2016_Q4_Merge.loc', 'CO_002_2016_q4_alias.loc', 'CO_002_2017_here_2017_q1_streets_alias.loc.xml', 'CO_002_2017_here_2017_q1_streets_parity.loc', 'CO_002_2017_here_2017_q1_streets_parity.loc.xml', 'CO_002_2016_q4_alias.lox']
可以像这样使用一个简单的列表理解:
exLst = [i for i in pLst for j in exCrt if j in i]
这应该有用!
我有一个命名不一致的文件列表:
pLst = ['CO_002_2016_Q4_Merge.loc', 'CO_002_2016_Merge.zip', 'CO_002_2016_q4_alias.loc', 'CO_002_2017_here_2017_q1_streets_alias.loc.xml', 'CO_002_2017_here_2017_q1_streets_parity.loc', 'AuburnAliasGCS_1984_1106.lox', 'CA_ORG_BCP.loc.xml', 'CA_ORG_BCP.loc', 'Co52 Alias Address Locator.lox', 'CO_002_2017_here_2017_q1_streets_parity.loc.xml', 'CentralCostaCountyStreets.lox', 'CO_002_2016_q4_alias.lox']
我想提取包含此列表中的字符串的文件:
exCrt = ["2016_Q4", "2016_q4","2017"]
我想将包含 exCrt 中任何元素的所有文件添加到提取列表中,但我没有看到将拆分标准与部分选择标准(即“_”)隔离开来的方法.
我尝试使用任何:
if any(x in pLst for x in exCrt):
exLst.add(x)
这导致了一个空集。
我还尝试将 exCrt 更改为 ["2016","q4","Q4","2017"]
,然后使用 'and' 'or' 方法:
for i in pLst:
if exCrt[0] and exCrt[1] or exLst[0] and exCrt[2] or exCrt[3] in i.split("_"):
exLst.add(i)
但这并没有排除任何不需要的文件。
我希望输出为
( 'CO_002_2016_Q4_Composite.loc',
'CO_002_2016_q4_alias.loc.xml',
'CO_002_2016_Q4_Composite.loc.xml',
'CO_002_2016_Q4_Merge.lox',
'CO_002_2016_Q4_Merge.loc.xml',
'CO_002_2016_Q4_Merge.loc',
'CO_002_2016_q4_alias.loc',
'CO_002_2016_q4_alias.lox',
'CO_002_2017_here_2017_q1_streets_alias.lox',
'CO_002_2017_here_2017_q1_streets_alias.loc',
'CO_002_2017_here_2017_q1_streets_alias.loc.xml',
'CO_002_2017_here_2017_q1_streets_parity.loc',
'CO_002_2017_here_2017_q1_streets_parity.loc.xml')
如果您按如下方式调整,您使用 any
的方法将会奏效:
exCrt = ["2016_Q4", "2016_q4", "2017"]
exLst = []
for p in pLst:
if any(x in p for x in exCrt):
exLst.append(p)
您可以使用列表理解:
pLst = ['CO_002_2016_Q4_Merge.loc', 'CO_002_2016_Merge.zip', 'CO_002_2016_q4_alias.loc', 'CO_002_2017_here_2017_q1_streets_alias.loc.xml', 'CO_002_2017_here_2017_q1_streets_parity.loc', 'AuburnAliasGCS_1984_1106.lox', 'CA_ORG_BCP.loc.xml', 'CA_ORG_BCP.loc', 'Co52 Alias Address Locator.lox', 'CO_002_2017_here_2017_q1_streets_parity.loc.xml', 'CentralCostaCountyStreets.lox', 'CO_002_2016_q4_alias.lox']
exCrt = ["2016_Q4", "2016_q4", "2017"]
final_pLst = [i for i in pLst if any(b in i for b in exCrt)]
输出:
['CO_002_2016_Q4_Merge.loc', 'CO_002_2016_q4_alias.loc', 'CO_002_2017_here_2017_q1_streets_alias.loc.xml', 'CO_002_2017_here_2017_q1_streets_parity.loc', 'CO_002_2017_here_2017_q1_streets_parity.loc.xml', 'CO_002_2016_q4_alias.lox']
可以像这样使用一个简单的列表理解:
exLst = [i for i in pLst for j in exCrt if j in i]
这应该有用!