File record separators.
Larry Bates
larry.bates at websafe.com
Tue May 15 11:29:50 EDT 2007
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
More information about the Python-list
mailing list