python固定长度划分字符串
程序员文章站
2022-04-06 15:31:12
...
import re
text = '123456789abcdefg'
textArr = re.findall('.{3}', text)
textArr.append(text[(len(textArr) * 3):])
print(textArr)
# ['123', '456', '789', 'abc', 'def', 'g']
import re
def cut_text(text, lenth):
textArr = re.findall('.{' + str(lenth) + '}', text)
textArr.append(text[(len(textArr) * lenth):])
return textArr
print(cut_text('123456789abcdefg', 3))
# ['123', '456', '789', 'abc', 'def', 'g']