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$