numpy.fromstring

本文详细介绍了如何使用numpy库中的fromstring函数从字符串中读取并转换数据到numpy数组,包括参数说明、返回值解释及异常处理,通过实例展示了不同数据类型和分隔符的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

numpy.fromstring

numpy.fromstring(string, dtype=float, count=-1, sep='')

A new 1-D array initialized from text data in a string.
从字符串中的文本数据初始化的新一维数组。

1. Parameters:

string : str
A string containing the data.
包含数据的字符串。

dtype : data-type, optional
The data type of the array; default: float. For binary input data, the data must be in exactly this format.
数组的数据类型。default: float。对于二进制输入数据,数据必须完全采用这种格式。

count : int, optional
Read this number of dtype elements from the data. If this is negative (the default), the count will be determined from the length of the data.
从数据中读取此数量的 dtype 元素。如果为负 (默认值),则将根据数据长度确定计数。

sep : str, optional
The string separating numbers in the data; extra whitespace between elements is also ignored.
数据中分隔数字的字符串,元素之间的多余空格也将被忽略。

2. Returns

arr : ndarray
The constructed array.
构造的数组。

3. Raises

ValueError
If the string is not the correct size to satisfy the requested dtype and count.

4. example

(pt-1.4_py-3.6) yongqiang@yongqiang:~/pytorch_work$ python
Python 3.6.10 |Anaconda, Inc.| (default, May  8 2020, 02:54:21)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>>
>>> np.fromstring('1 2', dtype=int, sep=' ')
array([1, 2])
>>>
>>> np.fromstring('1, 2', dtype=int, sep=',')
array([1, 2])
>>>
>>> np.fromstring('1234', dtype=np.uint8)
array([49, 50, 51, 52], dtype=uint8)
>>>
>>> np.fromstring('1234', np.uint8)
array([49, 50, 51, 52], dtype=uint8)
>>>
>>> exit()
(pt-1.4_py-3.6) yongqiang@yongqiang:~/pytorch_work$

5. little endian (低位字节在前)

Python 的字符串实际上是字节序列,每个字符占一个字节,因此如果从字符串 s 创建一个 8bit 的整数数组的话,所得到的数组正好就是字符串中每个字符的 ASCII 编码。

>>> s = "abcdefgh"
>>> np.fromstring(s, dtype=np.int8)
array([ 97,  98,  99, 100, 101, 102, 103, 104], dtype=int8)
>>>

如果从字符串 s 创建 16bit 的整数数组,那么两个相邻的字节就表示一个整数,把字节 98 和字节 97 当作一个16 位的整数,它的值就是 98 * 256 + 97 = 25185。可以看出内存中是以 little endian (低位字节在前) 方式保存数据的。

>>> s = "abcdefgh"
>>> np.fromstring(s, dtype=np.int8)
array([ 97,  98,  99, 100, 101, 102, 103, 104], dtype=int8)
>>>
>>> np.fromstring(s, dtype=np.int16)
array([25185, 25699, 26213, 26727], dtype=int16)
>>>

如果把整个字符串转换为一个 64 位的双精度浮点数数组,那么它的值是:

>>> np.fromstring(s, dtype=np.float)
array([8.54088322e+194])

显然这个例子没有什么意义,但是可以想象如果我们用 C 语言的二进制方式写了一组 double 类型的数值到某个文件中,那们可以从此文件读取相应的数据,并通过 fromstring 函数将其转换为 float64 类型的数组。

(pt-1.4_py-3.6) yongqiang@yongqiang:~/pytorch_work$ python
Python 3.6.10 |Anaconda, Inc.| (default, May  8 2020, 02:54:21)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>>
>>> np.fromstring('12345678', np.uint8)
array([49, 50, 51, 52, 53, 54, 55, 56], dtype=uint8)
>>>
>>> np.fromstring('12345678', np.int16)
array([12849, 13363, 13877, 14391], dtype=int16)
>>>
50 * 256 + 49 = 12849
52 * 256 + 51 = 13363
54 * 256 + 53 = 13877
56 * 256 + 55 = 14391
little endian (低位字节在前)

>>> np.fromstring('12345678', np.int32)
array([875770417, 943142453], dtype=int32)
>>>
52 * 16777216 + 51 * 65536 + 50 * 256 + 49 = 872415232 + 3342336 + 12800 + 49 = 875770417
56 * 16777216 + 55 * 65536 + 54 * 256 + 53 = 939524096 + 3604480 + 13824 + 53 = 943142453
little endian (低位字节在前)

>>> exit()
(pt-1.4_py-3.6) yongqiang@yongqiang:~/pytorch_work$
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值