Liam Clarke wrote:
> Hi,
> 
> Thanks Kent, I'll check out the CSV module. I had a go with Pyparsing
> awhile ago, and it's clocking in at the 3 minute mark also.
> 
> Alan - the data is of the form -
> 
> a = {
> b = 1
> c = 2
> d = { e =  { f  = 4 g = "Ultimate Showdown of Ultimate Destiny" } h =
> { i j k } }
> }
> 
> Everything is whitespace delimited. I'd like to turn it into
> 
> ["a", "=", "{", "b", "=", "1",
>   "c", "=", "2", "d", "=", "{",
>  "e", "=", "{", "f", "=", "4", "g", "=",
> "\"Ultimate Showdown of Ultimate Destiny\"",
>  "}", "h", "=", "{", "i", "j", "k", "}", "}"]
> 

This is close - it strips the quotes from the quoted string and doubled 
spaces show up as an empty field. The empty field is easy to strip. You 
could add quotes back to any field that contains spaces.

data = '''a = {
b = 1
c = 2
d = { e =  { f  = 4 g = "Ultimate Showdown of Ultimate Destiny" } h =
{ i j k } }
}'''.splitlines()

import csv

tokens = []
for line in csv.reader(data, delimiter=' '):
     tokens.extend(line)

print tokens


#### prints
['a', '=', '{', 'b', '=', '1', 'c', '=', '2', 'd', '=', '{', 'e', '=', 
'', '{', 'f', '', '=', '4', 'g', '=', 'Ultimate Showdown of Ultimate 
Destiny', '}', 'h', '=', '{', 'i', 'j', 'k', '}', '}', '}']

Kent

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to