我想在下面的例子中提取包含单词"tk-nuke-writenode"
的路径。
我需要隔离那个特定的路径,并且只隔离那个路径。下面的路径不是固定的,所以我不能使用split函数并使用字段(例如[2]
(选择"tk-nuke-writenode"
路径。参见以下示例:
NUKE_PATH = os.environ['NUKE_PATH']
结果:
'X:\pipeline\app_config\release\extensions\global\nuke;X: \ppipeline\app_config\release\extensions\projects\sgtk\powerPlant\install\app_store\tk-nuke\v0.11.4\classic_startup\restart;X:/ppipeline/app_config/release/extensions/projects/sgtk/powerPlant/install/app_store/tk-nuke-writenode/v.141/gizmos'
NUKE_PATH.split(os.pathsep)[2]
结果:
'X:/pipeline/app_config/release/extensions/projects/sgtk/powerPlant/install/app_store/tk-nuke-writenode/v.14.1/gizmos'
想要的输出:
'X:/pipeline/app_config/release/extensions/projects/sgtk/powerPlant/install/app_store/tk-nuke-writenode/v.14.1/gizmos'
提前感谢您提供的任何帮助!
路径除以';'所以你可以使用这样的东西:
NUKE_PATH = os.environ['NUKE_PATH']
l = NUKE_PATH.split(';')
result = filter(lambda path: "tk-nuke-writenode" in path, l)
假设您的路径不包含任何分号,则可以执行
path = 'X:pipelineapp_configreleaseextensionsglobalnuke;X:pipelineapp_configreleaseextensionsprojectssgtkpowerPlantinstallapp_storetk-nukev0.11.4classic_startuprestart;X:/pipeline/app_config/release/extensions/projects/sgtk/powerPlant/install/app_store/tk-nuke-writenode/v1.4.1/gizmos'
matches = [p for p in path.split(';') if 'tk-nuke-writenode' in p]
matches[0]
'X:/pipeline/app_config/release/extensions/projects/sgtk/powerPlant/install/app_store/tk-nuke-writenode/v1.4.1/gizmos'
如果没有找到匹配项,这将引发异常,同时处理多个匹配项可能是个好主意。