[Tutor] Splitting long string into same len parts
Victor Bouffier
victor at grupocdm.com
Sat Feb 11 04:48:57 CET 2006
Aha!!!
I believe this is what I was looking for in the first place (not that I
will use it anyway, given the alternatives provided by others).
I guess that coming from a Perl background, which as you know includes
regexes as part of the core language, you tend to look to all solutions
through this lens. I faced this problem before and solved it using
regexes but could not remember how.
Your re.findall() suggestion is nice though. Very clean.
Thanks Danny.
On Wed, 2006-02-08 at 18:55 -0800, Danny Yoo wrote:
>
> On Wed, 8 Feb 2006, Victor Bouffier wrote:
>
> > Hi to all,
> >
> > I'd like to split a long string into equally long strings (len(str) =
> > 3). I did the following using regexes:
> >
> > >>> n = 'xb1jyzqnd1eenkokqnhep6vp692qi9tmag3owzqw0sdq3zjf'
> > >>> o = re.split(r'(...)', n)
> > >>> print o
> > ['', 'xb1', '', 'jyz', '', 'qnd', '', '1ee', '', 'nko', '', 'kqn', '',
> > 'hep', '', '6vp', '', '692', '', 'qi9', '', 'tma', '', 'g3o', '', 'wzq',
> > '', 'w0s', '', 'dq3', '', 'zjf', '']
> >
> > Which gives me empty strings between each value.
>
> Hi Victor,
>
> Try using re.findall() instead of re.split(). The behavior you're seeing
> with split is perfectly logical: each pair of empty strings is being split
> by that three-character sequence.
>
>
> Best of wishes!
>
>
More information about the Tutor
mailing list