可以优化,但你这个优化有问题,数字字符串再长3位数,就报越界了。
我写的代码不会触发这种错误,只是不对“极”之后的再4分隔插入汉字分隔符而已。
File "test.py", line 60, in <module>
print(cn_digit('23423423423456156874656748956441334964654654946546465246549846.95'))
File "test.py", line 54, in cn_digit
result = intpart[i-4:i] + dem[-i//4-1] + result
IndexError: string index out of range
【 在 moudy 的大作中提到: 】
: 都已经re.match了可以直接取组更省事吧
: [code=python]
: def cn_digit(s):
: ...................
--
修改:poocp FROM 171.221.29.*
FROM 171.221.29.*