-1

I'm using Python 3 on Windows 10. Consider the following string:

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

I want to split these zips at the 3 character to get the zip3, but this code throws an error.

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

I'm not quite sure how to get around. Help appreciated. Thanks.

Todd Shannon
  • 409
  • 1
  • 3
  • 15

1 Answers1

0

To get the first three of each, simply string-slice them:

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

Outtput:

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

To split all text in threes, join it, then use chunking to get chunks of 3 letters:

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

Outtput:

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

See How do you split a list into evenly sized chunks? and Understanding Python's slice notation

Slicing and chunking works on strings as well - the official doku about strings is here: about strings and slicing


To get the remainder as well:

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']]
Patrick Artner
  • 43,256
  • 8
  • 36
  • 57