欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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']