python numpy savetxt [英] python numpy savetxt

查看:119
本文介绍了python numpy savetxt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以在这里指出我在做什么错吗?

Can someone indicate what I am doing wrong here?

import numpy as np

a = np.array([1,2,3,4,5],dtype=int)
b = np.array(['a','b','c','d','e'],dtype='|S1')

np.savetxt('test.txt',zip(a,b),fmt="%i %s")

输出为:

Traceback (most recent call last):
  File "loadtxt.py", line 6, in <module>
    np.savetxt('test.txt',zip(a,b),fmt="%i %s")
  File "/Users/tom/Library/Python/2.6/site-packages/numpy/lib/io.py", line 785, in savetxt
    fh.write(format % tuple(row) + '\n')
TypeError: %d format: a number is required, not numpy.string_

推荐答案

您需要以其他方式构造数组:

You need to construct you array differently:

z = np.array(zip([1,2,3,4,5], ['a','b','c','d','e']), dtype=[('int', int), ('str', '|S1')])
np.savetxt('test.txt', z, fmt='%i %s')

在传递序列时,savetext执行

when you're passing a sequence, savetext performs asarray(sequence) call and resulting array is of type |S4, that is all elements are strings! that's why you see this error.

这篇关于python numpy savetxt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆