使用regex python和“ re”包分割字符串

时间:2019-01-02 18:31:40

标签: regex python-3.x

我在Windows 10上使用Python 3。请考虑以下字符串:

import re
s = ["12345", "67891", "01112"]

我想在3个字符处拆分这些zip以获得zip3,但是此代码引发错误。

re.split("\d{3}", s)
TypeError: cannot use a string pattern on a bytes-like object

我不太确定如何解决。帮助表示赞赏。谢谢。

1 个答案:

答案 0 :(得分:0)

要获取每一个的前三个,只需对它们进行字符串切片:

s = ["12345", "67891", "01112"]
first_tree = [p[0:3] for p in  s] 
print(first_tree)

输出:

['123', '678', '011'] # slicing

要将所有文本一分为三,请将其加入,然后使用分块获取3个字母的块:

s = ["12345", "67891", "01112"]
k = ''.join(s)
threesome = [k[i:i+3] for i in range(0,len(k),3)]
print(threesome)

输出:

['123', '456', '789', '101', '112']  # join + chunking

请参见How do you split a list into evenly sized chunks?Understanding Python's slice notation

切片和分块也适用于字符串-有关字符串的官方doku在这里:about strings and slicing


也要获取剩余部分:

s = ["12345", "67891", "01112"]
three_and_two = [[p[:3], p[3:]] for p in s] 
print(three_and_two) #  [['123', '45'], ['678', '91'], ['011', '12']]