我一直在尝试学习自动分割,因此我在github上找到了一个代码,但我不知道为什么它无法运行。我不理解rs = re.match('.+', str(p))
,当我运行此行时,它得到了匹配并移至patient_images[file][rs.groups()[0]] = p
,但随后出现错误IndexError: tuple index out of range
。我打印出rs
并得到<re.Match object; span=(0, 60), match='C:/Users/user/Desktop/code/patients/pat01\\ma>
。为什么说它匹配所有60个字符,但只显示38个字符,这是下一行给出错误的原因?
图像目录的结构如下:
code:
patients:
pat01:
images:
export1.dcm
export2.dcm
export3.dcm
masks:
export1.dcm
export2.dcm
export3.dcm
代码:
import os, re
root_dir = f"C:/Users/user/Desktop/code/patients/"
patient_images = {}
for root, dirs, files in os.walk(root_dir):
for file in files:
if file.endswith('.dcm'):
if 'PATIENT_DICOM' in root:
if not patient_images.get(file,None):
patient_images[file] = {}
p = os.path.join(root,file)
patient_images[file]['real'] = p
elif 'masks' in root:
print(os.path.join(root,file))
if not patient_images.get(file,None):
patient_images[file] = {}
p = os.path.join(root,file)
rs = re.match('.+', str(p))
if rs:
patient_images[file][rs.groups()[0]] = p
print('match')
else:
print('Did not match groups')
对于rs = re.match(...)
,您想要rs.group()
(注意没有's'),它是匹配的字符串的一部分。
此外,'。+'的正则表达式将匹配具有至少一个字符的所有字符串,因此编写起来会更整洁:
p = os.path.join(root,file)
if len(p) > 0:
patient_images[file][p] = p