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
1 Answer 1
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
hpaulj
233k14 gold badges260 silver badges392 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py
bjust means it's bytes type, which is the type you specified. Thebis not actually in the string. Why does it matter? It's the correct way.