numpy.savetxt保存array时TypeError: must be real number, not str 问题

在用numpy.savetxt() 保存一个字符串array时出现了TypeError: must be real number, not str的报错。在百度搜索了一下,全是乱七八糟的解释,没看到靠谱的。以前在国外用google,任何报错基本很快找到解决办法,百度真是无语了。没办法,用bing搜索了下,在 stack overflow中找到了解决办法:

import numpy as np

# array 为字符换性质
>>> holdon_id 
array(['1000568.3', '1000568.3', '1000568.3', ..., 'AFEU01000007',
       'CP002601', 'AP010906'], dtype='<U12')
# 直接np.savetext 保存
>>> np.savetxt("holdon_id.txt",holdon_id)
Traceback (most recent call last):
  File "/homes/xiaohuizou/anaconda3/lib/python3.7/site-packages/numpy/lib/npyio.py", line 1422, in savetxt
    v = format % tuple(row) + newline
TypeError: must be real number, not str

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/homes/xiaohuizou/anaconda3/lib/python3.7/site-packages/numpy/lib/npyio.py", line 1426, in savetxt
    % (str(X.dtype), format))
TypeError: Mismatch between array dtype ('object') and format specifier ('%.18e')

直接用默认的numpy.savetxt()出现报错,看报错原因TypeError: Mismatch between array dtype ('object') and format specifier ('%.18e')是因为array是‘object’字符型,存储的类型是浮点型'%.18e'所以不匹配。

修改存储类型为字符串解决问题

>>> np.savetxt("holdon_id.txt",holdon_id,fmt="%s")
  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值