Saving Numeric arrays to file?

D

Duncan Smith

I am having problems saving arrays to file. e.g.
t1 array([30])
t2 array([ 0, 0, 5, 5, 5, 10])
t3 array([20, 5])
t4
array([[ 0, 5, 10, 5, 0, 10],
[ 0, 0, 0, 5, 0, 0]])
f = file('c:\Data\IS_rounded.txt', 'w')
f.write(t1.tostring() + '\n')
f.write(t2.tostring() + '\n')
f.write(t3.tostring() + '\n')
f.write(t4.tostring() + '\n')
f.close()

f = file('c:\Data\IS_rounded.txt', 'r')
Numeric.fromstring(f.readline().strip()) array([30])
Numeric.fromstring(f.readline().strip())
array([0, 0, 5, 5, 5]) # missing
final element
Traceback (most recent call last):
File "<pyshell#181>", line 1, in -toplevel-
Numeric.fromstring(f.readline().strip())
ValueError: string size must be a multiple of element size
Anyone any idea where I'm going wrong? 2.3 on Win2k. Cheers.

Duncan
 
D

Duncan Smith

Of course, the problem is reasonably obvious (doh!). But I still need a
workaround, so I can (ideally) save an array as a single line of text.

Duncan
 
D

Duncan Smith

Emile van Sebille said:
Duncan Smith asks about writing Numeric arrays to disk:
[ 0, 0, 0, 5, 0, 0]])
f = file('c:\Data\IS_rounded.txt', 'w')

Perhaps with 'wb' ?

Emile van Sebille
(e-mail address removed)

It doesn't work for me. Looks like I'll maybe have to avoid \n altogether,
use spaces to separate the strings and read characters individually.
Basically I have a few hundred thousand sets of 4 tables, each associated
with a unique key. The idea was to store them as a text file so that I
could quickly fire them into a dictionary (memory permitting?) when I need
to use them. Maybe I should be using MySQL for persistence? What I *would*
like is a reasonably generic (and not terribly inefficient) solution for
storing strings that contain escape characters in text files. Cheers.

Duncan
 
A

Alex Martelli

Duncan Smith wrote:
...
What I
*would* like is a reasonably generic (and not terribly inefficient)
solution for
storing strings that contain escape characters in text files. Cheers.

I'm not quite sure what you mean by "contain escape characters" in this
context. But maybe the quoted-printable coding is what you're looking
for? See module quopri in the standard Python library.


Alex
 
D

Duncan Smith

Alex Martelli said:
Duncan Smith wrote:
...

I'm not quite sure what you mean by "contain escape characters" in this
context. But maybe the quoted-printable coding is what you're looking
for? See module quopri in the standard Python library.


Alex

Thanks Alex,
I was writing to file strings such as
'\n\x00\x00\x00\x00\x00\x00\x00' and then trying to retrieve them via
readline(). It turns out that querying a MySQL table is easily quick enough
for my purposes (the bottlenecks are elsewhere). But I'll certainly check
out quopri (seeing as I wasn't aware of it). Thanks again.

Duncan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top