[英]How to fix this error when using the str.index() method?
I am having a text string- "The class also contains a string for the collection's title and a scalar Boolean variable indicating whether the collection is currently editable." 我有一个文本字符串- “该类还包含一个字符串,该字符串表示集合的标题和标量布尔变量,指示该集合当前是否可编辑。”
Here, I'm trying to fetch the index of the sub-string "collection's" but getting the below error. 在这里,我试图获取子字符串“ collection's”的索引,但出现以下错误。 ValueError: substring not found
ValueError:找不到子字符串
I understand that the apostrophe in the sub-string in the actual text is in different font. 我了解实际文本中的子字符串中的撇号使用不同的字体。 So, is there any way where we can bypass difference in the font.
因此,有什么方法可以绕过字体的差异。
txt = "The class also contains a string for the collection’s title and a scalar Boolean variable indicating whether the collection is currently editable."
print(txt.index("collection's"))
Expected is to have the index of the sub-string- "collection's" 期望具有子字符串的索引-“集合的”
actual result is: ValueError: substring not found enter code here
实际结果是:ValueError:找不到子字符串
enter code here
因为'
和'
是不同的字符
Just make sure your text and the string you are looking for use the same form of apostrophe '
只要确保您要查找的文本和字符串使用相同的撇号
'
txt = "The class also contains a string for the collection's title and a scalar" \
"Boolean variable indicating whether the collection is currently editable."
print(txt.index("collection's"))
#41
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.