3

I am trying to convert 'b' (a string in which the column entries are separated by one delimiter and the the rows are separated by another delimiter) to 'a' (a 2d numpy array), like:

b='191.250\t0.00\t0\t1\n191.251\t0.00\t0\t1\n191.252\t0.00\t0\t1\n'
a=numpy.array([[191.25,0,0,1],[191.251,0,0,1],[191.252,0,0,1]])

The way I do it is (using my knowledge that there are 4 columns in 'a'):

a=numpy.array(filter(None,re.split('[\n\t]+',b)),dtype=float).reshape(-1,4)

Is there a better way?

Alex Riley
178k46 gold badges274 silver badges247 bronze badges
asked Dec 21, 2015 at 17:59
2
  • what is wrong with your way? Commented Dec 21, 2015 at 18:05
  • Nothing, my way works, I just cobbled it together from stuff I found online. I was just wondering if there was a "more correct" way of doing it, a way to do it without using regular expressions, and/or a way to do it without knowing a priori the number of columns. Commented Dec 21, 2015 at 18:21

2 Answers 2

6

Instead of splitting and filtering, you could use np.fromstring:

>>> np.fromstring(b, sep='\t').reshape(-1, 4)
array([[ 191.25 , 0. , 0. , 1. ],
 [ 191.251, 0. , 0. , 1. ],
 [ 191.252, 0. , 0. , 1. ]])

This always returns a 1D array so reshaping is necessary.

Alternatively, to avoid reshaping, if you already have a string of bytes (as strings are in Python 2), you could use np.genfromtxt (with the help of the standard library's io module):

>>> import io
>>> np.genfromtxt(io.BytesIO(b))
array([[ 191.25 , 0. , 0. , 1. ],
 [ 191.251, 0. , 0. , 1. ],
 [ 191.252, 0. , 0. , 1. ]])

genfromtxt handles missing values, as well as offering much more control over how the final array is created.

answered Dec 21, 2015 at 18:10
Sign up to request clarification or add additional context in comments.

Comments

2

Here's what I did to get the result you're looking for:

import numpy as np
b='191.250\t0.00\t0\t1\n191.251\t0.00\t0\t1\n191.252\t0.00\t0\t1\n'
a = np.array([[float(j) for j in i.split('\t')] for i in b.splitlines()])
answered Dec 21, 2015 at 18:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.