<p>使用<code>delimiter</code>的字段宽度版本,我可以使用<code>genfromtxt</code>加载前两组数字(<code>txt</code>是文件示例的多行粘贴):</p>
<pre><code>In [221]: dels = [2,3,3, 3,3,3]; cols=[0,1,2,3,4,5]
In [222]: np.genfromtxt(txt.splitlines(), delimiter=dels, usecols=cols, dtype=float)
Out[222]:
array([[ 24., 238., 8., 27., 237., 8.],
[ 39., 79., 6., 39., 80., 6.],
[ 39., 88., 8., 39., 89., 8.],
[ 39., 97., 10., 39., 98., 10.],
[ 39., 104., 12., 39., 105., 12.],
[ 40., 80., 9., 40., 81., 10.],
[ 40., 107., 9., 40., 108., 9.],
[ 41., 79., 8., 41., 80., 8.]])
</code></pre>
<p>因为'8'字段,我不得不使用<code>dtype</code>float。我想我可以缩短字段,跳过“.”。你知道吗</p>
<p>或者,如果我指定一个<code>None</code>类型,它将生成一个混合了整数和浮点数据类型字段的结构化数组。你知道吗</p>
<pre><code>In [223]: np.genfromtxt(txt.splitlines(), delimiter=dels, usecols=cols, dtype=None)
Out[223]:
array([(24, 238, 8., 27, 237, 8.), (39, 79, 6., 39, 80, 6.),
(39, 88, 8., 39, 89, 8.), (39, 97, 10., 39, 98, 10.),
(39, 104, 12., 39, 105, 12.), (40, 80, 9., 40, 81, 10.),
(40, 107, 9., 40, 108, 9.), (41, 79, 8., 41, 80, 8.)],
dtype=[('f0', '<i8'), ('f1', '<i8'), ('f2', '<f8'), ('f3', '<i8'), ('f4', '<i8'), ('f5', '<f8')])
</code></pre>
<p><code>dels</code>和<code>cols</code>可以扩展以处理所有7个组。你知道吗</p>
<p>一旦你有了这样的数字,应该很容易把它们映射到你的最终坐标数组。但我想你已经解决了。你知道吗</p>
<p>===</p>
<p>将“.”拆分为自己的未使用列:</p>
<pre><code>In [226]: dels = [2,3,2,1, 3,3,2,1]; cols=[0,1,2, 4,5,6]
In [227]: np.genfromtxt(txt.splitlines(), delimiter=dels, usecols=cols, dtype=int)
Out[227]:
array([[ 24, 238, 8, 27, 237, 8],
[ 39, 79, 6, 39, 80, 6],
[ 39, 88, 8, 39, 89, 8],
[ 39, 97, 10, 39, 98, 10],
[ 39, 104, 12, 39, 105, 12],
[ 40, 80, 9, 40, 81, 10],
[ 40, 107, 9, 40, 108, 9],
[ 41, 79, 8, 41, 80, 8]])
</code></pre>