string / split method on ASCII code?

Tim Chase python.list at tim.thechases.com
Wed Mar 12 16:55:56 EDT 2008


> I have these annoying textilfes that are delimited by the ASCII char for <<
> (only its a single character) and >> (again a single character)
> 
> Their codes are 174 and 175, respectively.
> 
> My datafiles are in the moronic form
> 
> X<<Y>>Z
> 
> I need to split on those freaking characters.  Any tips on how to make split
> work with these things?

If it were a single character, you could just use

   s.split(chr(174))

However, since you need to split on multiple characters, you can use

   import re
   splitter_re = re.compile(chr(174) + '|' + chr(175))
   for line in file(FILENAME):
     parts = splitter_re.split(line)
     do_something(parts)

and then go find a large blunt object with which to bludgeon the 
creator of the file... :)

They aren't exactly ascii characters (0-127), but the above 
should at least do the trick.

-tkc





More information about the Python-list mailing list