File record separators.

H

HMS Surprise

I need to write 2 member lists to a file. For each record the number
of these lists can be different. I think a good way to handle that may
be to make each record a list of lists. I am restricted to using
version 2.2. That being the case what is a good standard record
separator to use to ensure that I read in one record (list of lists)
at a time, '\n'? I want to try to stay with what is considered
standard python idioms.


Thanks,

jvh
 
L

Larry Bates

HMS said:
I need to write 2 member lists to a file. For each record the number
of these lists can be different. I think a good way to handle that may
be to make each record a list of lists. I am restricted to using
version 2.2. That being the case what is a good standard record
separator to use to ensure that I read in one record (list of lists)
at a time, '\n'? I want to try to stay with what is considered
standard python idioms.


Thanks,

jvh
You really haven't given us quite enough info, but here goes:

If I control this file I might just write out a list of lists
on each line and eval it (not tested):

[['a', 'b'], ['c','d']]
[['a', 'b'], ['c','d'], ['e','f']]

fp=open(filename, 'r')
lists=[]
for n, line in enumerate(fp):
try: l=eval(line)
except SyntaxError:
print "SyntaxError on line=%i" % (n+1)

lists.append(l)

If you want something different, I might use a tab between lists
and commas between values.

-Larry
 
G

Grant Edwards

I need to write 2 member lists to a file. For each record the number
of these lists can be different. I think a good way to handle that may
be to make each record a list of lists. I am restricted to using
version 2.2. That being the case what is a good standard record
separator to use to ensure that I read in one record (list of lists)
at a time, '\n'? I want to try to stay with what is considered
standard python idioms.

Your description is a bit vague, but if I'm guessing your task
correctly, the standard idiom is to use the pickle module:

http://www.python.org/doc/2.2.3/lib/module-pickle.html

You can use it to write pretty much any python object to a file.
 
H

HMS Surprise

Thanks folks. Was unaware of enumerate , still have a lot to learn
about python.

Sorry for the poorly phrased request, but you gathered the gist of it.
My wonderment is how to write the following 2 lines and make sure they
are saved as separate records or lines so that I pull in only one at a
time with readline(?).

['a', 'b'], ['c','d']]
[['a', 'b'], ['c','d'], ['e','f']]

Would like to use pickle but it is apparently unavailable in the
package I am using, Jython 2.2.
 
G

Grant Edwards

Would like to use pickle but it is apparently unavailable in the
package I am using, Jython 2.2.

Odd. At least in 2.4, pickle.py seems to be Jython-aware.
 
B

Boris Borcic

HMS said:
Thanks folks. Was unaware of enumerate , still have a lot to learn
about python.

Sorry for the poorly phrased request, but you gathered the gist of it.
My wonderment is how to write the following 2 lines and make sure they
are saved as separate records or lines so that I pull in only one at a
time with readline(?).

['a', 'b'], ['c','d']]
[['a', 'b'], ['c','d'], ['e','f']]

Would like to use pickle but it is apparently unavailable in the
package I am using, Jython 2.2.

I am pretty sure some version of pickle or cPickle is available in Jython 2.1,
though. I'd take a second look, to be sure.
 
H

HMS Surprise

I am pretty sure some version of pickle or cPickle is available in Jython 2.1,
though. I'd take a second look, to be sure.

Many thanks Boris!

import cPickle

caused no errors!
 

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

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top