4

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.

4
  • 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 Commented Jul 10, 2015 at 16:04
  • @DanPatterson I can replicate, numpy 1.9.2, and changing to 'S20' has no effect. Commented Jul 10, 2015 at 17:35
  • @MatthewPlourde my version is 1.7.x could this be a Unicode difference issue? Commented Jul 10, 2015 at 19:46
  • This looks like open issue #4543 Commented Sep 19, 2015 at 3:30

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.