Skip to main content
 首页 » 编程设计

python之Numpy genfromtxt 遍历列

2024年10月24日11zengkefu

我正在使用 NumPy 的 genfromtext 从 CSV 文件中获取列。

每一列都需要拆分并分配给单独的 SQLAlchemy SystemRecord 与其他一些列和属性结合并添加到数据库中。

迭代列 f1 到 f9 并将它们添加到 session 对象的最佳做法是什么?

到目前为止,我已经使用了以下代码,但我不想为每个 f 列做同样的事情:

t = np.genfromtxt(FILE_NAME,dtype=[(np.str_, 20),(np.str_, 20),(np.str_, 20),(np.str_, 20),(np.str_, 20),(np.str_, 20),(np.str_, 20),(np.str_, 20), (np.str_, 20), (np.str_, 20),(np.str_, 20)]\ 
 ,delimiter=',',filling_values="None", skiprows=0,usecols=(0,1,2,3,4,5,6,7,8,9,10)) 
 
for r in enumerate(t): 
    _acol = r['f1'].split('-') 
    _bcol = r['f2'].split('-') 
    .... 
    arec = t_SystemRecords(first=_acol[0], second=_acol[1], third=_acol[2], ... ) 
    db.session.add(arec) 
    db.session.commit() 

请您参考如下方法:

查看t.dtype。或者 r.dtype。

制作一个示例结构化数组(这是 genfromtxt 返回的内容):

t = np.ones((5,), dtype='i4,i4,f8,S3') 

看起来像:

array([(1, 1, 1.0, b'1'), (1, 1, 1.0, b'1'), (1, 1, 1.0, b'1'), 
       (1, 1, 1.0, b'1'), (1, 1, 1.0, b'1')],  
      dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<f8'), ('f3', 'S3')]) 

dtype 和 dtype.names 是:

In [135]: t.dtype 
Out[135]: dtype([('f0', '<i4'), ('f1', '<i4'), ('f2', '<f8'), ('f3', 'S3')]) 
 
In [138]: t.dtype.names 
Out[138]: ('f0', 'f1', 'f2', 'f3') 

遍历名称以查看各个列:

In [139]: for n in t.dtype.names: 
   .....:     print(t[n]) 
   .....:      
[1 1 1 1 1] 
[1 1 1 1 1] 
[ 1.  1.  1.  1.  1.] 
[b'1' b'1' b'1' b'1' b'1'] 

或者在您的情况下,遍历“行”,然后遍历名称:

In [140]: for i,r in enumerate(t): 
   .....:     print(r) 
   .....:     for n in r.dtype.names: 
   .....:         print(r[n]) 
   .....:          
(1, 1, 1.0, b'1') 
1 
1 
1.0 
b'1' 
(1, 1, 1.0, b'1') 
... 

对于r,它是0d(检查r.shape),你可以通过数字选择项目或迭代

r[1]  # == r[r.dtype.names[1]] 
for i in r: print(r) 

对于 1d 的 t 这不起作用; t[1] 引用了一个项目。

一维结构化数组的行为有点像二维数组,但又不完全是。通常所说的 row 和 column 必须替换为 row(或 item)和 field。


做一个可能更接近你的情况的t

In [175]: txt=[b'one-1, two-23, three-12',b'four-ab, five-ss, six-ss'] 
 
In [176]: t=np.genfromtxt(txt,dtype=[(np.str_,20),(np.str_,20),(np.str_,20)]) 
 
In [177]: t 
Out[177]:  
array([('one-1,', 'two-23,', 'three-12'), 
       ('four-ab,', 'five-ss,', 'six-ss')],  
      dtype=[('f0', '<U20'), ('f1', '<U20'), ('f2', '<U20')]) 

np.char 具有可应用于数组的字符串函数:

In [178]: np.char.split(t['f0'],'-') 
Out[178]: array([['one', '1,'], ['four', 'ab,']], dtype=object) 

它不适用于结构化数组,但适用于各个字段。该输出可以作为列表的列表进行索引(它不是 2d)。