中文的isalpha()
会被判定为True,需要使用unicode来区分中文和英文。
def calculate_character_start_end(strs, idx):
start = idx - 1
end = idx + 1
while (strs[start].encode('utf-8').isalpha()):
start = start - 1
if start == 0:
break
if not strs[start].encode('utf-8').isalpha():
start = start + 1
while (strs[end].encode('utf-8').isalpha()):
end = end + 1
if end == len(strs) - 1:
break
if not strs[end].encode('utf-8').isalpha():
end = end - 1
return start, end