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, optional

如果为 True,则按照 C 编译器的方式进行字段对齐。默认为 False。

byteorderstr, optional

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

另请参阅

numpy.dtype, numpy.typename

示例

>>> 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

转换后的数据类型。