【问题标题】:Numpy savetxt Structured Array ValueError: fmt has wrong number of % formats [duplicate]Numpy savetxt 结构化数组 ValueError:fmt 的 % 格式数量错误 [重复]
【发布时间】:2021-04-04 02:48:14
【问题描述】:
import numpy as np

row_a = ['0.01722497', '', '0.09496404', '0.03654174', '0.03624997', '0.01583785', '0.02002064', '0.13934049', '0.0405615', '0.05686177', '', '0.08495372', '0.00619173', '0.00515492', '0.01053369', '0.06576333']
row_b = [0.04871661, 0.1122536, 0.20836956, 0.05473605, 0.02344445, 0.01739371, 0.00524003, 0.0640286, 0.02766152, 0.02442267, 0.04183814, 0.04853815, 0.01682549, 0.00263045, 0.00819199, 0.1631007]
dt = np.dtype([('col_1', 'U32'), ('col_2', float)])
arr = np.empty((2, len(row_a)), dtype=dt)
arr['col_1'] = row_a
arr['col_2'] = row_b
np.savetxt('table.csv', arr, delimiter=',', header='col_1,col_2', fmt='%s %f')

上面的代码(应该从 str 和 int 数组创建结构化数组并将其输出到 csv)给我以下错误,即使我有 2 个长度相同、2 列和 2 格式的数组:

ValueError: fmt has wrong number of % formats:  %s %f

【问题讨论】:

标签: numpy csv structured-array


【解决方案1】:

制作一维结构化数组(根据我的评论):

In [423]: row_a = ['0.01722497', '', '0.09496404', '0.03654174', '0.03624997', '0.01583785', '0
     ...: .02002064', '0.13934049', '0.0405615', '0.05686177', '', '0.08495372', '0.00619173',
     ...: '0.00515492', '0.01053369', '0.06576333']
     ...: row_b = [0.04871661, 0.1122536, 0.20836956, 0.05473605, 0.02344445, 0.01739371, 0.005
     ...: 24003, 0.0640286, 0.02766152, 0.02442267, 0.04183814, 0.04853815, 0.01682549, 0.00263
     ...: 045, 0.00819199, 0.1631007]
     ...: dt = np.dtype([('col_1', 'U32'), ('col_2', float)])
     ...: arr = np.empty(len(row_a), dtype=dt)
     ...: arr['col_1'] = row_a
     ...: arr['col_2'] = row_b
In [424]: 
In [424]: arr
Out[424]: 
array([('0.01722497', 0.04871661), ('', 0.1122536 ),
       ('0.09496404', 0.20836956), ('0.03654174', 0.05473605),
       ('0.03624997', 0.02344445), ('0.01583785', 0.01739371),
       ('0.02002064', 0.00524003), ('0.13934049', 0.0640286 ),
       ('0.0405615', 0.02766152), ('0.05686177', 0.02442267),
       ('', 0.04183814), ('0.08495372', 0.04853815),
       ('0.00619173', 0.01682549), ('0.00515492', 0.00263045),
       ('0.01053369', 0.00819199), ('0.06576333', 0.1631007 )],
      dtype=[('col_1', '<U32'), ('col_2', '<f8')])
In [425]: arr.shape
Out[425]: (16,)

还有保存:

In [426]: np.savetxt('table.csv', arr, delimiter=',', header='col_1,col_2', fmt='%s %f')
In [427]: cat table.csv
# col_1,col_2
0.01722497 0.048717
 0.112254
0.09496404 0.208370
0.03654174 0.054736
...

我之前回答的链接 SO 具有更复杂的 dtype。这是一个简单的 2 字段案例,因此不需要特殊处理。

""" 值可能会在文件加载时出现问题。我建议至少使用, 之类的分隔符,以便加载器可以将其视为缺失值。

In [428]: np.savetxt('table.csv', arr, delimiter=',', header='col_1,col_2', fmt='%s, %f')
In [429]: cat table.csv
# col_1,col_2
0.01722497, 0.048717
, 0.112254
0.09496404, 0.208370
0.03654174, 0.054736
...
In [430]: np.genfromtxt('table.csv', dtype=None, names=True, delimiter=',')
Out[430]: 
array([(0.01722497, 0.048717), (       nan, 0.112254),
       (0.09496404, 0.20837 ), (0.03654174, 0.054736),
       (0.03624997, 0.023444), (0.01583785, 0.017394),

In [431]: np.genfromtxt('table.csv', dtype=arr.dtype, names=True, delimiter=',')
Out[431]: 
array([('0.01722497', 0.048717), ('', 0.112254), ('0.09496404', 0.20837 ),
       ('0.03654174', 0.054736), ('0.03624997', 0.023444),
       ('0.01583785', 0.017394), ('0.02002064', 0.00524 ),

【讨论】:

    猜你喜欢
    • 2017-04-05
    • 2014-02-07
    • 2014-01-27
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多