1

I have a line in a file:

['18G01G03G06G09G11G12G17G19G23G31G32R01']

I need to split it up so that it results in:

['18', 'G01', 'G03', 'G06', 'G09', 'G11', 'G12', 'G17', 'G19', 'G23', 'G31', 'G32', 'R01']

I cant find a way on stackoverflow which explains this although i'm probs wrong. Can anyone help?

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
  • 2
    Looks like you might be looking for something like this: http://stackoverflow.com/questions/2277352/python-split-a-string-at-uppercase-letters – PtK Feb 10 '16 at 12:18

2 Answers2

4

You may use re.findall

import re
s = ['18G01G03G06G09G11G12G17G19G23G31G32R01']
re.findall(r'[A-Z]?\d+', s[0])
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0

Without using regex you could do it logically.

Code:

lst = ['18G01G03G06G09G11G12G17G19G23G31G32R01']
ind =0
count =0
new_lst=[]
for index, val in enumerate(lst[0]):
    if val.isalpha():
        new_lst.append(lst[0][ind:index])
        ind=index

if ind<len(lst[0]):
    new_lst.append(lst[0][ind:])
print new_lst

Output:

['18', 'G01', 'G03', 'G06', 'G09', 'G11', 'G12', 'G17', 'G19', 'G23', 'G31', 'G32', 'R01']
The6thSense
  • 8,103
  • 8
  • 31
  • 65