Converting bytes read from a file using numpy fromfile to unicode in Python 3 -
Converting bytes read from a file using numpy fromfile to unicode in Python 3 -
i trying read string of bytes file using numpy fromfile
in python 3. goal convert bytes normal python 3 string. example:
$ echo "1234" > t.txt
now file t.txt
contains 4 bytes of text. then:
import numpy np values=np.fromfile('t.txt',dtype='|s1',count=4) print ("values={}".format(values)) values=np.fromfile('t.txt',dtype='|u1',count=4) print ("values={}".format(values))
gives:
values=[b'1' b'2' b'3' b'4'] traceback (most recent phone call last): file "./t.py", line 12, in <module> print ("values={}".format(values)) file "/home/hakon/.pyenv/versions/3.4.2/lib/python3.4/site-packages/numpy/core/numeric.py", line 1715, in array_str homecoming array2string(a, max_line_width, precision, suppress_small, ' ', "", str) file "/home/hakon/.pyenv/versions/3.4.2/lib/python3.4/site-packages/numpy/core/arrayprint.py", line 454, in array2string separator, prefix, formatter=formatter) file "/home/hakon/.pyenv/versions/3.4.2/lib/python3.4/site-packages/numpy/core/arrayprint.py", line 328, in _array2string _summaryedgeitems, summary_insert)[:-1] file "/home/hakon/.pyenv/versions/3.4.2/lib/python3.4/site-packages/numpy/core/arrayprint.py", line 500, in _formatarray word = format_function(a[-1]) unicodedecodeerror: 'utf-32-le' codec can't decode bytes in position 0-3: codepoint not in range(0x110000)
i obtain normal python 3 string values='1234'
. how can done?
you utilize astype
convert bytes str:
import numpy np values = np.fromfile('t.txt',dtype='|s1',count=4).astype('|u1') print(values) # ['1' '2' '3' '4'] print(values.view('|u4')) # ['1234'] print(values.dtype) # <u1
python python-3.x numpy
Comments
Post a Comment