Python String Double Splitting?

So, my problem. I was given a line like this.

01000200030004020511050006000702051108020511090205111002051111020511120205111300140205111500160017001800190020002100 

then I have to divide it into something that will look 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, at first I thought that let's try to use split and maybe it will work, so this was my first attempt, and it just happened like this:

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

After that, although I myself, I would have to split twice to get rid of β€œ00” and β€œ020511”, so I used the method

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

By executing this method, I get it back ...

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

But it is not in the format in which I wanted it, and it does not work out correctly when I split it, it just gets rid of the values ​​that I like, for example, "10" will come out as 1, because the program breaks 0 and Im not quite sure how to come up with a solution for this so that any help is appreciated .. Thanks.

+5
source share
1 answer

You can use re.findall() to find numbers with a length of 2, then 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 to get the result in a list, you can use re.finditer , which returns an iterator and a list comprehension to convert 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']] >>> 
+5
source

All Articles