我有一个包含字符串和浮点数的 numpy 结构化数组。我想将这个结构化数组按原样保存到 csv 文件中。
我的程序的简化版本是这样的。
structured_array = np.zeros((1,), dtype=[('string','a20'),('float','f8')])
structured_array['string'] = 'string'
structured_array['float'] = 0.0
np.savetxt('foo.csv', structured_array, delimiter=',',fmt='%s,%f')
我希望
string,0.000000
在 foo.csv 中,但它给了我 b'string',0.000000
这个引号和这个在哪里b
来自?我怎样才能摆脱它?我可以用
readline()
并手动摆脱这一点,但有什么聪明的方法可以做到这一点。非常感谢。
最佳答案
1087行在savetxt
(...\lib\site-packages\numpy\lib\npio.py) 有
for row in X:
fh.write(asbytes(format % tuple(row) + newline))
这表明列在写入之前被转换为字节(因此是
b
前缀。这似乎不能改变。
关于arrays - 使用 np.savetxt 保存包含字符串和 float 的结构化 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31342318/