[Numpy-discussion] String manipulation

Sebastien Binet seb.binet@gmail....
Mon May 11 09:12:10 CDT 2009


hi,

> 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

how about this, then:

import numpy as np
def massage(data):
  fmt = np.dtype([('hdr', '|S8'), ('mid', '|S8'), ('tail','|S8')])
  data = data.replace(' ','')
  assert len(data)==3*8, "contract failed or invalid assumption"
  return np.asarray(data,dtype=fmt).tolist()

assert(massage(line_a) == massage(line_b))

cheers,
sebastien.
-- 
#########################################
# Dr. Sebastien Binet
# Laboratoire de l'Accelerateur Lineaire
# Universite Paris-Sud XI
# Batiment 200
# 91898 Orsay
#########################################



More information about the Numpy-discussion mailing list