numpy.format_parser

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

类将格式、名称、标题说明转换为数据类型。

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

参数
formatsstr或str列表

格式说明,可以指定为一个字符串,格式说明以逗号分隔。 'f8, i4, a5' 或表单中格式描述字符串的列表 ['f8', 'i4', 'a5'] .

namesstr或str的list/tuple

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

titles序列

标题字符串序列。可以使用空列表删除标题。

aligned可选的布尔

如果为true,则通过填充来对齐字段,就像C编译器那样。默认值为假。

byteorder可选的STR

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

实例

>>> np.format_parser(['<f8', '<i4', '<a5'], ['col1', 'col2', 'col3'],
...                  ['T1', 'T2', 'T3']).dtype
dtype([(('T1', 'col1'), '<f8'), (('T2', 'col2'), '<i4'), (('T3', 'col3'), 'S5')])

names 和/或 titles 可以是空列表。如果 titles 是一个空列表,标题将不会出现。如果 names 为空,将使用默认字段名。

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

转换的数据类型。