numpy.rec.format_parser#

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

将格式、名称、标题描述转换为 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, 可选

如果指定,所有字段将更改为提供的字节序。否则,使用默认字节序。有关所有可用字符串说明符,请参阅 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

转换后的数据类型。