CONTEST - What is the (best) solution?

Fuzzyman fuzzyman at gmail.com
Wed Feb 2 10:42:17 EST 2005


What about the syntax ? Will it have commas in the right place ?
(never, always, or sometimes ? - sometimes is much worse than never or
always).

*damn* - problem is that '{' might appear as one of the values....
*So*... read the file in as a list of lines and strip each line.
Dictionaries will always start where '{' is the first character. Anyway
- you could always explicitly check if each line ends with a ',' and if
it doesn't add one...)
(*warning* UNTESTED)

>handle = open('filename', 'r')
>thefile =  handle.readlines()
>handle.close()
>
>dictindex = []
>i = 0
>while i < len(thefile)-1:
>    if line.startswith('{'):
>        dictindex.append(i)
>        i += 1
>lastdict = ' '.join(thefile[dictindex[-1]:]
>print eval(lastdict)

Checking if each line ends with a ',' (except for the last line) should
be easy !
HTH

Regards,

Fuzzy
http://www.voidspace.org.uk/python/index.shtml




More information about the Python-list mailing list