t.txt -6ren">
gpt4 book ai didi

python - 在Python 3中使用numpy fromfile将从文件读取的字节转换为unicode

转载 作者:行者123 更新时间:2023-11-30 23:18:58 29 4
gpt4 key购买 nike

我正在尝试使用 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)

我想获取一个普通的 Python 3 字符串,例如 values='1234'。如何做到这一点?

最佳答案

您可以使用 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

关于python - 在Python 3中使用numpy fromfile将从文件读取的字节转换为unicode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26383354/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com