newname = "%s\%s"%(root,i[i.rindex("_")+1:len(i)]) ValueError: substring not found
时间: 2024-04-30 14:20:51 浏览: 11
这个错误通常是因为 `i` 这个字符串中不包含下划线 `_`,从而导致 `i.rindex("_")` 这个函数无法找到 `_` 的位置。
你可以使用 `if` 语句检查一下 `i` 是否包含 `_`,例如:
```python
if "_" in i:
newname = "%s\%s"%(root,i[i.rindex("_")+1:len(i)])
else:
# 处理 i 不包含下划线的情况
pass
```
这样就可以避免出现 `ValueError: substring not found` 的错误了。
相关问题
i_test = 0 print('-'*30) print('Creating test images...') print('-'*30) imgs_test = glob.glob(self.test_path+"/*."+"bmp") print(len(imgs_test)) imgdatas_test = np.ndarray((len(imgs_test),self.img_rows,self.img_cols,1), dtype=np.uint8) for imgname_test in imgs_test: midname_test = imgname_test[imgname_test.rindex("/")+1:] img_test = load_img(self.test_path + "/" + midname_test,grayscale = True) #img_test = scipy.io.loadmat(self.test_path + "/" + midname_test) #img_test = img_test['src'] #img_test=img_test.resize(hw) img_test = img_to_array(img_test) imgdatas_test[i_test] = img_test i_test += 1 print('loading test images done') imgs_test = imgdatas_test.astype('float32') imgs_test /= 255 return imgs_train, imgs_mask_train, imgs_test
这段代码是用于读取测试数据集中的图像数据,并将其转换为一个numpy数组。具体来说,它首先通过glob函数获取测试数据集中所有.bmp文件的路径,然后利用load_img函数将每个图像转换为numpy数组,并存储到imgdatas_test数组中。最后,它将imgdatas_test数组中的元素转换为float32类型,并将它们归一化到[0,1]的范围内。函数返回imgs_train, imgs_mask_train, imgs_test三个numpy数组。
class iStr(str): def __init__(self, *args): self.lowered = str.lower(self) def __repr__(self): return '%s(%s)' % (type(self).__name__, str.__repr__(self)) def __hash__(self): return hash(self._lowered) def lower(self): return self._lowered def _make_case_insensitive(name): str_meth = getattr(str, name) def x(self, other, *args): try: other = other.lower() except (TypeError, AttributeError, ValueError): pass return str_meth(self._lowered, other, *args) setattr(iStr, name, x) for name in 'eq lt le gt gt ne contains'.split(): _make_case_insensitive('__%s__' % name) for name in 'count endswith find index rfind rindex startswith'.split(): _make_case_insensitive(name) del _make_case_insensitive 将以上代码逐句逐行进行详细解释,超级超级详细,并且告知该如何输出,举例输出并打印
这段代码定义了一个名为`iStr`的自定义字符串类,它是对内置字符串类`str`的扩展,实现了不区分大小写的字符串比较和操作。
让我们逐句逐行详细解释代码:
```python
class iStr(str):
```
这段代码定义了一个名为`iStr`的类,它继承自内置字符串类`str`。
```python
def __init__(self, *args):
self.lowered = str.lower(self)
```
这是`iStr`类的初始化方法。它接受任意数量的参数,并将调用内置字符串类的`lower`方法将字符串转换为小写形式,并将结果存储在实例变量`lowered`中。
```python
def __repr__(self):
return '%s(%s)' % (type(self).__name__, str.__repr__(self))
```
这是`iStr`类的字符串表示方法。它返回一个表示该实例的字符串,格式为类名加上原始字符串的表示形式。
```python
def __hash__(self):
return hash(self._lowered)
```
这是`iStr`类的哈希方法。它返回实例的哈希值,使用小写形式的字符串进行哈希运算。
```python
def lower(self):
return self._lowered
```
这是`iStr`类的`lower`方法。它返回实例的小写形式字符串。
```python
def _make_case_insensitive(name):
str_meth = getattr(str, name)
def x(self, other, *args):
try:
other = other.lower()
except (TypeError, AttributeError, ValueError):
pass
return str_meth(self._lowered, other, *args)
setattr(iStr, name, x)
```
这是一个辅助函数`_make_case_insensitive`,它用于创建不区分大小写的字符串操作方法。它接受一个字符串方法的名称作为参数。
在函数内部,首先使用`getattr`函数获取内置字符串类`str`中的对应方法。
然后,定义了一个名为`x`的内部函数,它接受实例`self`、其他参数`other`和任意数量的额外参数。在函数内部,尝试将`other`转换为小写形式,并调用原始字符串方法来进行比较或操作。
最后,使用`setattr`函数将新定义的方法`x`添加到`iStr`类中,名称与原始字符串方法的名称相同。
```python
for name in 'eq lt le gt gt ne contains'.split():
_make_case_insensitive('__%s__' % name)
```
这个循环使用辅助函数`_make_case_insensitive`创建了一系列不区分大小写的比较操作方法,包括等于、小于、小于等于、大于、大于等于、不等于和包含。
```python
for name in 'count endswith find index rfind rindex startswith'.split():
_make_case_insensitive(name)
```
这个循环使用辅助函数`_make_case_insensitive`创建了一系列不区分大小写的字符串操作方法,包括计数、以什么结尾、查找、索引等。
```python
del _make_case_insensitive
```
最后,删除了辅助函数`_make_case_insensitive`,以避免在后续代码中被误用。
现在,让我们来看一个使用`iStr`类的示例:
```python
s = iStr("Hello World")
print(s) # 输出:iStr('Hello World')
print(s.lower()) # 输出:hello world
print(s.startswith("hello")) # 输出:True
print(s.contains("WORLD")) # 输出:True
print(s.count("o")) # 输出:2
```
在这个示例中,我们创建了一个`iStr`类的实例`s`,并调用了它的一些方法来进行字符串比较和操作。由于`iStr`类实现了不区分大小写的功能,所以不论传入的参数是大写还是小写,都能正确地进行比较和操作,并返回预期的结果。
相关推荐
![md](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)