python from函数用法_Python numpy.core.records.fromstring函数方法的使用

numpy.core.records.fromstring

numpy.core.records.fromstring(datastring, dtype=None, shape=None, offset=0, formats=None, names=None, titles=None, aligned=False, byteorder=None)[source]

根据二进制数据创建记录数组

请注意,尽管此函数具有名称,但它不接受str实例。参数 :datastring :bytes-like

二进制数据缓冲区

dtype :data-type, 可选

shape :int或int的tuple, 可选

每个数组的形状。

offset :int, 可选

在缓冲区中开始读取的位置。

formats, names, titles, aligned, byteorder:

如果dtype为None,

则将这些参数传递给numpy.format_parser以构造dtype。

有关详细文档,请参见该功能。

返回值 :np.recarray

将数组视图记录到数据字符串中的数据中。

如果数据字符串为只读,则它将为只读。

例子>>> a = b'\x01\x02\x03abc'

>>> np.core.records.fromstring(a, dtype='u1,u1,u1,S3')

rec.array([(1, 2, 3, b'abc')],

dtype=[('f0', 'u1'), ('f1', 'u1'), ('f2', 'u1'), ('f3', 'S3')])>>> grades_dtype = [('Name', (np.str_, 10)), ('Marks', np.float64),

... ('GradeLevel', np.int32)]

>>> grades_array = np.array([('Sam', 33.3, 3), ('Mike', 44.4, 5),

... ('Aadi', 66.6, 6)], dtype=grades_dtype)

>>> np.core.records.fromstring(grades_array.tobytes(), dtype=grades_dtype)

rec.array([('Sam', 33.3, 3), ('Mike', 44.4, 5), ('Aadi', 66.6, 6)],

dtype=[('Name', '>> s = '\x01\x02\x03abc'

>>> np.core.records.fromstring(s, dtype='u1,u1,u1,S3')

Traceback (most recent call last)

...

TypeError: a bytes-like object is required, not 'str'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值