By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,922 Members | 1,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,922 IT Pros & Developers. It's quick & easy.

File record separators.

P: n/a

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

May 15 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
HMS Surprise wrote:
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

May 15 '07 #2

P: n/a
On 2007-05-15, HMS Surprise <jo**@datavoiceint.comwrote:
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.

--
Grant Edwards grante Yow! Nipples, dimples,
at knuckles, NICKLES,
visi.com wrinkles, pimples!!
May 15 '07 #3

P: n/a
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.

May 15 '07 #4

P: n/a
On 2007-05-15, HMS Surprise <jo**@datavoiceint.comwrote:
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.

--
Grant Edwards grante Yow! does your DRESSING
at ROOM have enough ASPARAGUS?
visi.com
May 15 '07 #5

P: n/a
HMS Surprise wrote:
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.

May 15 '07 #6

P: n/a
>
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.
Many thanks Boris!

import cPickle

caused no errors!

May 15 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.