Python NumPy 的 loadtxt() 和 genfromtxt 的“dtype”有哪些可用数据类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13997087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:08:16  来源:igfitidea点击:

What are the available datatypes for 'dtype' with NumPy's loadtxt() an genfromtxt?

pythonnumpy

提问by ryanjdillon

What are the available numpy.loadtxtor numpy.genfromtxtfor importing table data with varying datatypes, and what are the available abbreviations for the use (e.g. i32for integer)?

有哪些可用的numpy.loadtxtnumpy.genfromtxt 可用于导入具有不同数据类型的表数据,可用的缩写是什么(例如i32表示整数)?

This postdemonstrates the use of conditions, which I was curious if somebody might elaborate on.

这篇文章演示了条件的使用,我很好奇是否有人可以详细说明。

采纳答案by hpaulj

In addition to np.sctypeDict, there are these variables:

除了 之外np.sctypeDict,还有这些变量:

In [141]: np.typecodes
Out[141]: 
{'All': '?bhilqpBHILQPefdgFDGSUVOMm',
 'AllFloat': 'efdgFDG',
 'AllInteger': 'bBhHiIlLqQpP',
 'Character': 'c',
 'Complex': 'FDG',
 'Datetime': 'Mm',
 'Float': 'efdg',
 'Integer': 'bhilqp',
 'UnsignedInteger': 'BHILQP'}

In [143]: np.sctypes
Out[143]: 
{'complex': [numpy.complex64, numpy.complex128, numpy.complex192],
 'float': [numpy.float16, numpy.float32, numpy.float64, numpy.float96],
 'int': [numpy.int8, numpy.int16, numpy.int32, numpy.int32, numpy.int64],
 'others': [bool, object, str, unicode, numpy.void],
 'uint': [numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint32, numpy.uint64]}

回答by Jon Clements

Generic info about dtypes: http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html

关于的通用信息dtypeshttp: //docs.scipy.org/doc/numpy/reference/arrays.dtypes.html

From http://docs.scipy.org/doc/numpy/reference/arrays.scalars.html#arrays-scalars-built-in

来自http://docs.scipy.org/doc/numpy/reference/arrays.scalars.html#arrays-scalars-built-in

In NumPy, there are 24 new fundamental Python types to describe different types of scalars. These type descriptors are mostly based on the types available in the C language that CPython is written in, with several additional types compatible with Python's types.

在 NumPy 中,有 24 种新的基本 Python 类型来描述不同类型的标量。这些类型描述符主要基于编写 CPython 的 C 语言中可用的类型,还有一些其他类型与 Python 的类型兼容。

And what I didn't realise, is:

而我没有意识到的是:

The C-like names are associated with character codes, which are shown in the table. Use of the character codes, however, is discouraged.

类 C 名称与字符代码相关联,如表中所示。但是,不鼓励使用字符代码。

I doubt the numpycode/doc base is going anyway anytime soon, so that says it all I guess!

我怀疑numpy代码/文档库是否会很快消失,所以我猜这就是全部!

回答by Joel Vroom

for k, v in np.sctypeDict.iteritems(): print '{0:14s} : {1:40s}'.format(str(k), v)

for k, v in np.sctypeDict.iteritems(): print '{0:14s} : {1:40s}'.format(str(k), v)

Q              : <type 'numpy.uint64'>      
U              : <type 'numpy.unicode_'>
a              : <type 'numpy.string_'>

etc.

等等。