Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 整数的数据类型,三个浮点数_Python_Numpy_File Io_String Formatting - Fatal编程技术网

Python 整数的数据类型,三个浮点数

Python 整数的数据类型,三个浮点数,python,numpy,file-io,string-formatting,Python,Numpy,File Io,String Formatting,我有一个文本文件,除其他数据外,还包含表单的数据 215 1 0.0 0.0 0.0 [...] 9 -0.4330127018930699 0.2499999999985268 1.0 10 -0.1366025403783193 -0.03660254037890862 1.0 11 -0.2499999999985268 -0.4330127018930699 1.0 12 0.03660254037890862 -0.1366025403783193 1.0 13 0.433012701

我有一个文本文件,除其他数据外,还包含表单的数据

215
1 0.0 0.0 0.0
[...]
9 -0.4330127018930699 0.2499999999985268 1.0
10 -0.1366025403783193 -0.03660254037890862 1.0
11 -0.2499999999985268 -0.4330127018930699 1.0
12 0.03660254037890862 -0.1366025403783193 1.0
13 0.4330127018930699 -0.2499999999985268 1.0
14 0.1366025403783193 0.03660254037890862 1.0
15 0.2499999999985268 0.4330127018930699 1.0
[...]
215 1.0 1.0 1.0
[...]  # some more data, other format
i、 e

一个整数,指定要到来的数据行数, N行,其中一个整数后跟三个浮点数, 还有一些数据,格式不同。 我想把这些数据转换成一个numpy数组。因为我可以最好地通过一个生成器通过线路访问该文件,所以很方便。但是,我没有正确指定数据类型。这个

使用openfilename作为f: line=islicef,1.next num_nodes=intline 点=numpy.fromiter islicef,num_节点, dtype=['idx',int,1',vals',float,3], count=num_节点 不起作用。有什么提示吗?

此脚本:

import numpy as np

txt = b"""7
9 -0.4330127018930699 0.2499999999985268 1.0
10 -0.1366025403783193 -0.03660254037890862 1.0
11 -0.2499999999985268 -0.4330127018930699 1.0
12 0.03660254037890862 -0.1366025403783193 1.0
13 0.4330127018930699 -0.2499999999985268 1.0
14 0.1366025403783193 0.03660254037890862 1.0
15 0.2499999999985268 0.4330127018930699 1.0
[...]  # some more data, other format
"""
dt = np.dtype([('idx', int, 1), ('vals', float, 3)])
#dt = np.dtype('i,f,f,f')
print(dt)

def gentxt(txt, dt):
    f = txt.splitlines()
    line = f[0]
    num_nodes = int(line)
    aslice = slice(1,num_nodes+1)
    # print(f[aslice])
    points = np.genfromtxt(
        f[aslice],
        dtype=dt)
    return points

M = gentxt(txt,dt)
print(repr(M))
产生

1304:~/mypy$ python3 stack33406545.py 
[('idx', '<i4'), ('vals', '<f8', (3,))]
array([(9, [-0.4330127018930699, 0.2499999999985268, 1.0]),
       (10, [-0.1366025403783193, -0.03660254037890862, 1.0]),
       (11, [-0.2499999999985268, -0.4330127018930699, 1.0]),
       (12, [0.03660254037890862, -0.1366025403783193, 1.0]),
       (13, [0.4330127018930699, -0.2499999999985268, 1.0]),
       (14, [0.1366025403783193, 0.03660254037890862, 1.0]),
       (15, [0.2499999999985268, 0.4330127018930699, 1.0])], 
      dtype=[('idx', '<i4'), ('vals', '<f8', (3,))])
但2d列表列表不会:

In [234]: np.fromiter([['1', '2'],['3', '4']],dtype=int)
....
ValueError: setting an array element with a sequence.
对于复杂的数据类型,我必须为其提供元组:

In [236]: np.fromiter([('1', '2'),('3', '4')],dtype=np.dtype('i,i'))
Out[236]: 
array([(1, 2), (3, 4)], dtype=[('f0', '<i4'), ('f1', '<i4')])

带有多个数字的字符串或字符串元组无效,['12','34'],['12','34',]。genfromtxt可以更好地处理具有csv格式的行和列的文本。

我建议使用loadtxt或genfromtxt。当dtype=None时,他们将为您推导int v浮点值。或者尝试i、f、f、f数据类型。您的数据类型可能也可以工作。我很难使用需要文件句柄的方法,因为该文件包含许多其他格式不同的数据。这就是我使用生成器islice.genfromtxt的原因。它一次只接收一行数据。对于测试,我经常使用字符串列表。发电机应该能正常工作。
In [236]: np.fromiter([('1', '2'),('3', '4')],dtype=np.dtype('i,i'))
Out[236]: 
array([(1, 2), (3, 4)], dtype=[('f0', '<i4'), ('f1', '<i4')])