2

i'm having trouble with 3.4 using numpy. My question is to know how can i have a numpy matrix with plain string format instead byte-string.

def res(data):
    M = np.zeros(data.shape).astype(dtype='|S20')
    lines,columns = M.shape
    for l in range(lines):
        M[l][0] = data[l][1]
        M[l][1] = data[l][2]
        M[l][2] = data[l][3]  
    return M
**result python2.7**
[['Ann' '38.72' '-9.133']
 ['John' '55.68' '12.566']
 ['Richard' '52.52' '13.411']
 ['Alex' '40.42' '-3.703']]

**result python3.4**
[[b'Ann' b'38.72' b'-9.133']
 [b'John' b'55.68' b'12.566']
 [b'Richard' b'52.52' b'13.411']
 [b'Alex' b'40.42' b'-3.703']]

In Python3.4 How can i have my Matrix in plain string like in example for python2.7 this is bad because i have functions that expect string values and not byte-strings.

Any help would be great. thanks

1
  • 1
    Generally with a 2d array, it's better index with M[l,0]. As to your problem, the base string type in 3.4 is unicode, while in 2.7 it's ascii. So for some purposes b'abc' is the 3.4 equivalent, but for other just type str. Commented May 20, 2015 at 23:32

1 Answer 1

1

in my case the solution were simply to change dtype('|S20') to dtype(str)..I hope this help.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.