0
import numpy as np
c=np.dtype([('name','S20'), ('id','f4'), ('Marks','i1')])
d=np.array([('Ganesh',100.2,75) ], dtype = c)
print(d)

But the output I am getting is :

[(b'Ganesh', 100.2, 75)]

and the required output is :

[('Ganesh', 100.2, 75)]

If I am using multiple data in d then the output received is the desired one.

Dhaval Taunk
1,6721 gold badge12 silver badges18 bronze badges
asked Mar 15, 2020 at 5:47
1
  • b just means it's bytes type, which is the type you specified. The b is not actually in the string. Why does it matter? It's the correct way. Commented Mar 15, 2020 at 5:49

1 Answer 1

2
In [114]: c=np.dtype([('name','S20'), ('id','f4'), ('Marks','i1')]) 
In [115]: d=np.array([('Ganesh',100.2,75) ], dtype = c) 
In [116]: d 
Out[116]: 
array([(b'Ganesh', 100.2, 75)],
 dtype=[('name', 'S20'), ('id', '<f4'), ('Marks', 'i1')])

In PY3, the default string type is unicode. `b'foo' is bytestring. Use 'U' instead of 'S' if you want the default:

In [117]: c=np.dtype([('name','U20'), ('id','f4'), ('Marks','i1')]) 
In [118]: d=np.array([('Ganesh',100.2,75) ], dtype = c) 
In [119]: d 
Out[119]: 
array([('Ganesh', 100.2, 75)],
 dtype=[('name', '<U20'), ('id', '<f4'), ('Marks', 'i1')])
answered Mar 15, 2020 at 5:56
Sign up to request clarification or add additional context in comments.

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.