[Tutor] Escape sequences
David Heiser
David.Heiser at intelliden.com
Thu Jun 22 22:29:33 CEST 2006
That worked just dandy. Thanks.
-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On
Behalf Of Kent Johnson
Sent: Thursday, June 22, 2006 1:03 PM
Cc: tutor at python.org
Subject: Re: [Tutor] Escape sequences
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
_______________________________________________
Tutor maillist - Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list