array.tofile() refuses to write into a StringIO

Hello, as said in the subject, the following code produces an error. Is it normal ? ********************************************************** A = r_[1] file_buffer = StringIO() A.tofile(file_buffer) IOError: first argument must be a string or open file **********************************************************

On 25.09.2011 18:23, OC wrote:
as said in the subject, the following code produces an error. Is it normal ?
********************************************************** A = r_[1] file_buffer = StringIO() A.tofile(file_buffer)
IOError: first argument must be a string or open file **********************************************************
Yes, this is a known shortcoming of .tofile(). -- Pauli Virtanen

On 25.09.2011 18:23, OC wrote:
as said in the subject, the following code produces an error. Is it normal ?
********************************************************** A = r_[1] file_buffer = StringIO() A.tofile(file_buffer)
IOError: first argument must be a string or open file **********************************************************
if you want to write to a string, why not use .tostring()? -CHB -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov

if you want to write to a string, why not use .tostring()?
Because A.tostring() returns the binary data, while I would like the text representation. More precisely, I would like to use A.tofile(sep="\t").
Yes, this is a known shortcoming of .tofile().
Is it worth filing a bug report ? -- O.C.

On 9/27/11 2:14 AM, oc-spam66 wrote:
if you want to write to a string, why not use .tostring()?
Because A.tostring() returns the binary data, while I would like the text representation. More precisely, I would like to use A.tofile(sep="\t").
I see -- I've always thought mingling binary and text reading and writing was an API mistake.
Ah, I found a workaround: savetxt() can work with a StringIO -> savetxt(file_buffer, A)
This is only a workaround. I still think A.tofile() should be capable of writing into a StringIO.
or .tostring() should support text (but that's what savetxt is for) Anyway, the text behavior of fromfile() and tofile() is broken in many ways -- it really need a major refactor,and yes,this is one issue that it would be nice to address. Or just get deprecate the text functionality. -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov

Ah, I found a workaround: savetxt() can work with a StringIO -> savetxt(file_buffer, A) This is only a workaround. I still think A.tofile() should be capable of writing into a StringIO. -- O.C.
participants (4)
-
Chris.Barker
-
OC
-
oc-spam66
-
Pauli Virtanen