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,選用
如果指定,所有欄位都將變更為提供的位元組順序。否則,將使用預設位元組順序。如需所有可用的字串規範,請參閱 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
轉換後的資料型別。