[Numpy-discussion] String manipulation
Nils Wagner
nwagner at iam.uni-stuttgart.de
Mon May 11 09:53:39 EDT 2009
On Mon, 11 May 2009 15:03:02 +0200
Sebastien Binet <seb.binet at gmail.com> wrote:
> On Monday 11 May 2009 14:36:17 Nils Wagner wrote:
>> On Mon, 11 May 2009 14:25:46 +0200
>>
>> Francesc Alted <faltet at pytables.org> wrote:
>> > 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.
>>
>> Indeed.
>> I would like to split strings made of digits after eight
>> characters each.
>>
>> >>> line_a
>>
>> '111111.1222222.2333333.3'
>>
>> >>> line_b
>>
>> '111111.1 222222.2 333333.3'
>>
>> >>> line_b.split()
>>
>> ['111111.1', '222222.2', '333333.3']
>>
>> How can I accomplish that ?
> would this suit you ?
>>>> np.asarray(line_b,dtype=[('hdr','|S8'),('mid','|S8'),('tail','|S8')])
> array(('111111.1', '222222.2', '333333.3'),
> dtype=[('hdr', '|S8'), ('mid', '|S8'), ('tail',
>'|S8')])
>
> hth,
> sebastien.
> --
> #########################################
> # Dr. Sebastien Binet
> # Laboratoire de l'Accelerateur Lineaire
> # Universite Paris-Sud XI
> # Batiment 200
> # 91898 Orsay
> #########################################
>
>
here is my workaround.
from numpy import arange
line_a = '111111.1222222.2333333.3' # without
separator
line_b = '111111.1 222222.2 333333.3' # including space
as a delimiter
div, mod = divmod(len(line_a),8)
liste = []
for j in arange(0,div):
liste.append(line_a[j*8:(j+1)*8])
print liste
print line_b.split() # Works for line_b
but not for line_a
Cheers,
Nils
More information about the NumPy-Discussion
mailing list