Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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.

    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?

I can use readline() and manually get rid of this but is there any clever way to do this.

Thank you very much.

share|improve this question
    
I can't replicate... I get string,0.000000 have you got some sort of encoding set to your script? try ('string','S20') for the first field – user1121588 Jul 10 '15 at 16:04
    
@DanPatterson I can replicate, numpy 1.9.2, and changing to 'S20' has no effect. – Matthew Plourde Jul 10 '15 at 17:35
    
@MatthewPlourde my version is 1.7.x could this be a Unicode difference issue? – user1121588 Jul 10 '15 at 19:46
    
This looks like open issue #4543 – askewchan Sep 19 '15 at 3:30

Line 1087 in savetxt (...\lib\site-packages\numpy\lib\npio.py) has

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.

share|improve this answer
    
So, nothing can be done? – xiki_tempula Jul 11 '15 at 15:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.