[Tutor] Escape sequences
Kent Johnson
kent37 at tds.net
Thu Jun 22 21:02:57 CEST 2006
David Heiser wrote:
> I have code that assigns escape sequences to variables like
> "self.resetString = '\03'". As long as they are hard coded in,
> everything works fine.
>
> But I want to read the variable from a text/config file so my users can
> override the defaults. In the file are a list of "parameter = value"
> pairs like "resetString = \03". As the file is parsed, each pair is
> stored in a dictionary; "parmeterDictionary[parameter] = value".
>
> Then in the code, the value is assigned to the variable with a statement
> like "self.resetString = parmeterDictionary['resetString']".
>
> Simple ASCII strings work fine, but the escape sequences don't work and
> the code fails. "print self.resetString" returns "\\03", instead of a
> nonprintable character.
Use the 'string_escape' codec to decode the escaped strings:
In [1]: s= '\\03'
In [2]: s
Out[2]: '\\03'
In [3]: len(s)
Out[3]: 3
In [4]: s.decode('string_escape')
Out[4]: '\x03'
In [5]: len(_)
Out[5]: 1
Kent
More information about the Tutor
mailing list