numpy.rec.format_parser#

class numpy.rec.format_parser(formats, names, titles, aligned=False, byteorder=None)[源代码]#

将格式,名称,标题描述转换为 dtype 的类.

构造 format_parser 对象后,dtype 属性是转换后的数据类型: dtype = format_parser(formats, names, titles).dtype

参数:
formatsstr 或 str 列表

格式描述,可以指定为以逗号分隔的格式描述的字符串,形式为 'f8, i4, S5' ,也可以指定为格式描述字符串的列表,形式为 ['f8', 'i4', 'S5'] .

namesstr 或 str 的列表/元组

字段名称,可以指定为以逗号分隔的字符串,形式为 'col1, col2, col3' ,也可以指定为字符串的列表或元组,形式为 ['col1', 'col2', 'col3'] . 可以使用空列表,在这种情况下,将使用默认字段名称 (‘f0’, ‘f1’, …).

titles序列

标题字符串序列. 可以使用空列表来省略标题.

alignedbool, 可选

如果为 True,则按照 C 编译器的方式通过填充来对齐字段. 默认为 False.

byteorderstr, optional

如果指定,则所有字段都将更改为提供的字节顺序. 否则,使用默认字节顺序. 有关所有可用的字符串指定符,请参见 dtype.newbyteorder .

示例

>>> import numpy as np
>>> np.rec.format_parser(['<f8', '<i4'], ['col1', 'col2'],
...                      ['T1', 'T2']).dtype
dtype([(('T1', 'col1'), '<f8'), (('T2', 'col2'), '<i4')])

names 和/或 titles 可以是空列表. 如果 titles 是一个空列表,则标题将根本不显示. 如果 names 为空,将使用默认字段名称.

>>> np.rec.format_parser(['f8', 'i4', 'a5'], ['col1', 'col2', 'col3'],
...                      []).dtype
dtype([('col1', '<f8'), ('col2', '<i4'), ('col3', '<S5')])
>>> np.rec.format_parser(['<f8', '<i4', '<a5'], [], []).dtype
dtype([('f0', '<f8'), ('f1', '<i4'), ('f2', 'S5')])
属性:
dtypedtype

转换后的数据类型.