简体   繁体   English

使用str.index()方法时如何解决此错误?

[英]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.

相关问题 ValueError:使用 str.index 时未找到子字符串 - ValueError: Substring not found when using str.index python str.index时间复杂度 - python str.index time complexity str.index() 和 list.index() 有什么区别? - What is the difference between str.index() and list.index()? for循环中str.count和str.index的时间复杂度是多少 - What's the time complexity of str.count and str.index in a for loop 如何使用实例激活 __str__ dunder 方法并修复错误:“__str__ 返回非字符串(类型 NoneType)” - how to activate __str__ dunder method using instance and fix error:"__str__ returned non-string (type NoneType)" 使用python将json str插入postgresql时如何修复“类型json的无效输入语法” - How to fix "invalid input syntax for type json" when inserting json str into postgresql with copy method, using python 使用 scrapy 时如何修复“TypeError:无法混合 str 和非 str 参数”? - How do I fix “TypeError: Cannot mix str and non-str arguments” when using scrapy? 如何修复 [类型错误:+= 不支持的操作数类型:'builtin_function_or_method' 和 'str'] Python 中的错误 - How to fix [Type Error: unsupported operand type(s) for +=: 'builtin_function_or_method' and 'str'] error in Python str 的 .index 方法如何在 python 中工作? - How does .index method of str work in python? 使用十六进制,八进制或二进制整数作为Python的str.format()方法的参数索引时的KeyError - KeyError when using hex, octal, or binary integer as argument index with Python's str.format() method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM