在Python 3中使用numpy fromfile将文件读取转换为unicode

时间:2014-10-15 13:04:11

标签: python python-3.x numpy

我正在尝试使用Python 3中的NumPy fromfile从文件中读取一串字节。我的目标是将字节转换为普通的Python 3字符串。例如:

$ echo "1234" > t.txt

现在文件t.txt包含4个字节的文本。然后:

import numpy as 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))

给出:

values=[b'1' b'2' b'3' b'4']
Traceback (most recent 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
    return 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)

我想获得像values='1234'这样的普通Python 3字符串。怎么办呢?

2 个答案:

答案 0 :(得分:2)

您可以使用astype将字节转换为str:

import numpy as 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

答案 1 :(得分:1)

我知道问题明确要求np.fromfile,但为什么不直接使用内置文件界面?

f = open('t.txt', 'r')
values = f.read().rstrip('\n')
f.close()

注意:默认情况下,Python 3字符串是Unicode。