Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python String Double Splitting?

So heres my issue I am given a string like this one

01000200030004020511050006000702051108020511090205111002051111020511120205111300140205111500160017001800190020002100

then I have to separate it into something that will end up looking like this

[['01', 00], ['02', 00], ['03', 00], ['04', 020511], ['05', 00], ['06', 00], ['07', 020511, ['08', 020511], ['09', 020511], ['10', 020511,], ['11', 020511], ['12', 020511], ['13', 00], ['14', 020511], ['15', 00], ['16', 00], ['17', 00], ['18', 00], ['19', 00], ['20', 00], ['21', 00]]

So first I thought lets try using split and maybe that will work so that was my first try and it just came out like this

['01', '02', '03', '0402051105', '06', '0702051108020511090205111', '20511110205111202051113', '1402051115', '16', '17', '18', '19', '2', '021', '']

After that failed I though to myself that I am going to have to split twice to get rid of the '00' and the '020511', so I used the method

re.split('020511|00', theStr)

Doing that method I get this back...

['01', '02', '03', '04', '05', '06', '07', '08', '09', '1', '2051111', '12', '13', '14', '15', '16', '17', '18', '19', '2', '021', '']

But its not in the format that I wanted it in and its not coming out right, when I split it will just get rid of the values that I was like say '10' will come out as 1 because the program splits the 0's and Im not entirely sure how to come up with a solution for this so any help is apreciated.. Thanks.

like image 945
cunniemm Avatar asked Jul 28 '15 13:07

cunniemm


1 Answers

You can use re.findall() to find the digits with length 2 that followed by 00 or 020511:

>>> re.findall('(\d{2})(020511|00)', theStr)
[('01', '00'), ('02', '00'), ('03', '00'), ('04', '020511'), ('05', '00'), ('06', '00'), ('07', '020511'), ('08', '020511'), ('09', '020511'), ('10', '020511'), ('11', '020511'), ('12', '020511'), ('13', '00'), ('14', '020511'), ('15', '00'), ('16', '00'), ('17', '00'), ('18', '00'), ('19', '00'), ('20', '00'), ('21', '00')]
>>> 

And if you want the result in a list you can use re.finditer that returns an iterator and a list comprehension to convert the relative groups to list:

>>> [list(i.group(1,2)) for i in re.finditer('(\d{2})(020511|00)', theStr)]
[['01', '00'], ['02', '00'], ['03', '00'], ['04', '020511'], ['05', '00'], ['06', '00'], ['07', '020511'], ['08', '020511'], ['09', '020511'], ['10', '020511'], ['11', '020511'], ['12', '020511'], ['13', '00'], ['14', '020511'], ['15', '00'], ['16', '00'], ['17', '00'], ['18', '00'], ['19', '00'], ['20', '00'], ['21', '00']]
>>> 
like image 191
Mazdak Avatar answered Oct 11 '22 13:10

Mazdak