看提示超出索引了,表示 split 有的数据没有切割,也就是没有 - 字符
然后可以利用find()查找这个字符的索引,就可以用切片分离中英文了。
文本内容如下:
This is a test.,这是一个测试。 This is a test2.,这是一个测试2。
def split_chinese(strings):
for _char in strings:
if '\u4e00' <= _char <= '\u9fa5':
return _char
with open('./测试文件', mode="r", encoding="utf-8") as file:
for line in file:
_char = split_chinese(line)
index = line.find(_char)
print("英文: ", line[: index])
print("中文: ", line[index:])
结果:
英文: This is a test.,
中文: 这是一个测试。英文: This is a test2.,
中文: 这是一个测试2。
目测'-'有问题,代码里和数据里的字符不一致,其中一个是中文全角字符