我有一个图像,我使用image_slicer
将图像切成16个切片,图像命名为0804220001-5_01_01.png,0804220001-5_01_02.png,0804220001-5_01_03 .png
等等。使用 split
我需要文本 01_01,01_02,..
我尝试使用 split 和 rsplit 但没有得到结果。下面是我的代码。
#img1 is the path of the image which is
#Features/input/0804220001-5_02_04.png
name1 = img1.split('/')[-1]
patch = name1.rsplit('_')[1]
print(patch)
我得到的输出为01,但我需要输出为01_01,01_02
最佳答案
您可以将 re.search
与 _(.*)\.
模式结合使用:
import re
str = " 0804220001-5_02_04.png "
print(re.search('_(.*)\.', str).group(1))
提取 02_04
作为输出。
关于python - 使用 python split 从图像名称中获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58212981/