[Numpy-discussion] String manipulation

Francesc Alted faltet@pytables....
Mon May 11 07:25:46 CDT 2009


A Monday 11 May 2009, Nils Wagner escrigué:
> Hi all,
>
> Please consider two strings
>
> >>> line_a
>
> '12345678abcdefgh12345678'
>
> >>> line_b
>
> '12345678 abcdefgh 12345678'
>
> >>> line_b.split()
>
> ['12345678', 'abcdefgh', '12345678']
>
> Is it possible to split line_a such that the output
> is
>
> ['12345678', 'abcdefgh', '12345678']

Mmh, your question is a bit too generic.  If what you want is to 
separate the strings made of digits and the ones made of letters, it is 
worth to use regular expressions:

In [22]: re.split("(\d*)", line_a)[1:-1]
Out[22]: ['12345678', 'abcdefgh', '12345678']

Although regular expressions seems a bit thought to learn, they will 
payoff your effort in many occasions.

Cheers,

-- 
Francesc Alted

"One would expect people to feel threatened by the 'giant
brains or machines that think'.  In fact, the frightening
computer becomes less frightening if it is used only to
simulate a familiar noncomputer."

-- Edsger W. Dykstra
   "On the cruelty of really teaching computer science"


More information about the Numpy-discussion mailing list