I have a numpy structured array which contains string and float. I want to save this structured array as it is into a csv file. The simplified version of my procedure is like this.
我有一个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')
I would expect string,0.000000
in foo.csv, but it gives me b'string',0.000000
where does this quotation mark and this b
comes from? How can I get rid of it?
我希望foo.csv中的字符串为0.000000,但是它给了我字符串',0.000000这个引号是什么,这个b来自哪里?我怎么能摆脱它?
I can use readline()
and manually get rid of this but is there any clever way to do this.
我可以使用readline()并手动摆脱这个,但有任何聪明的方法来做到这一点。
Thank you very much.
非常感谢你。
1 个解决方案
#1
1
Line 1087 in savetxt
(...\lib\site-packages\numpy\lib\npio.py) has
savetxt(... \ lib \ site-packages \ numpy \ lib \ npio.py)中的第1087行有
for row in X:
fh.write(asbytes(format % tuple(row) + newline))
Which reveals that columns are converted to bytes before writing (hence the b
prefix. It doesn't appear that this can be changed.
这表明在写入之前将列转换为字节(因此是b前缀。看起来这似乎不会改变。
#1
1
Line 1087 in savetxt
(...\lib\site-packages\numpy\lib\npio.py) has
savetxt(... \ lib \ site-packages \ numpy \ lib \ npio.py)中的第1087行有
for row in X:
fh.write(asbytes(format % tuple(row) + newline))
Which reveals that columns are converted to bytes before writing (hence the b
prefix. It doesn't appear that this can be changed.
这表明在写入之前将列转换为字节(因此是b前缀。看起来这似乎不会改变。