我正在尝试在 for 循环中获取子字符串。为此,我正在使用这个:
for peoject in subjects:
peoject_name = peoject.content
print(peoject_name, " : ", len(peoject_name), " : ", len(peoject_name.split('-')[1]))
我有一些项目的句子中没有任何“-”。我该如何处理这个问题?
我遇到这个问题:
builtins.IndexError: list index out of range
最佳答案
for peoject in subjects:
try:
peoject_name = peoject.content
print(peoject_name, " : ", len(peoject_name), " : ", len(peoject_name.split('-')[1]))
except IndexError:
print("this line doesn't have a -")
关于Python - 拆分函数 - 列表索引超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49876604/