<p><code>loadtxt</code>文档表明
<code>converters</code>应该包含以下函数<a href="http://docs.scipy.org/doc/numpy-1.5.x/reference/generated/numpy.loadtxt.html" rel="nofollow noreferrer"><em>specifically return floats</em></a>:</p>
<blockquote>
<p><strong>converters</strong> : dict, optional</p>
<blockquote>
<p>A dictionary mapping column number to a function that will convert that column to a float. E.g., if column 0 is a date string: converters = {0: datestr2num}. Converters can also be used to provide a default value for missing data: converters = {3: lambda s: float(s or 0)}. Default: None.</p>
</blockquote>
</blockquote>
<p>如果您需要整数,则需要使用<code>dtype</code>关键字来转换浮点数。</p>
<pre><code>>>> numpy.loadtxt('th.txt', delimiter=',', usecols=(0, 2, 3), converters=dict(zip((0, 2, 3), (float, float, float))), dtype=([('i1', '<i4'), ('i2', '<f4'), ('i3', '<i4')]))
array([(1, 3.2000000476837158, 4), (1, 3.2000000476837158, 4),
(1, 3.2000000476837158, 4), (1, 3.2000000476837158, 4),
(1, 3.2000000476837158, 4), (1, 3.2000000476837158, 4),
(1, 3.2000000476837158, 4), (1, 3.2000000476837158, 4),
(1, 3.2000000476837158, 4)],
dtype=[('i1', '<i4'), ('f1', '<f4'), ('i2', '<i4')])
</code></pre>
<p>当然,在这种情况下实际上不需要<code>converters</code>——这实际上是为了将<code>'True'</code>这样的任意字符串值转换为数值。此外,如果您实际上需要一个简单的二维数组而不是记录数组,则不要传递记录格式:</p>
<pre><code>>>> numpy.loadtxt('th.txt', delimiter=',', usecols=(0, 2, 3), dtype=int)
array([[1, 3, 4],
[1, 3, 4],
[1, 3, 4],
[1, 3, 4],
[1, 3, 4],
[1, 3, 4],
[1, 3, 4],
[1, 3, 4],
[1, 3, 4]])
</code></pre>
<p>但如果这样做,就不能按列指定格式。</p>