[Tutor] strings & splitting

Kent Johnson kent37 at tds.net
Thu Jan 26 02:15:08 CET 2006


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



More information about the Tutor mailing list