[英]Python: Extract all characters between a two groups of characters in a string
Say I have a list of strings corresponding to a file name: 说我有一个与文件名相对应的字符串列表:
files = ['variable_timestep_model321_experiment123.csv',
'variable_timestep_model2_experiment21.csv',
'variable_timestep_model4321_experimentname1234.csv',
'variable_timestep_model0_experiment0.csv']
Where each file name has the format: 'variable_timestemp_modelname_experimentname.csv'
每个文件名的格式为: 'variable_timestemp_modelname_experimentname.csv'
In this filename variable
and timestemp
are not changing, however the modelname
and experimentname
are. 在此文件名variable
和timestemp
不变,但是modelname
和experimentname
名variable
。 I want to extract the model name, so the expected output would be: 我想提取模型名称,因此预期的输出将是:
['model321',
'model2',
'model4321',
'model0']
Because I know variable_timestemp_
stays the same, is there an easy way to extract X amount of characters between variable_timestep_
on the left and _
on the right? 因为我知道variable_timestemp_
保持不变,所以有没有一种简单的方法可以提取左侧的variable_timestep_
和右侧的_
之间的X个字符?
单程:
models = [name.split('_')[2] for name in files]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.