Interpreting \ escape sequences in strings

Paul Watson pwatson at redlinepy.com
Sun Mar 14 22:55:03 CET 2004


"Peter Otten" <__peter__ at web.de> wrote in message
news:c311kc$l1h$07$1 at news.t-online.com...
> Paul Watson wrote:
>
> > I did have not explained it clearly.  I want the user to specify a
string
>
> Seems it was clear enough, you only didn't recognize the answer :-)
>
> > that I will put between words in the output.  The user specified string
> > can
> > have escape sequences.  For example, the user wants to put a binary 1
> > (\001) between each output word.
> >
> > import sys
> > words = ['now', 'is', 'the', 'time']
> > print '\001'.join(words)        #this works
> > print sys.argv[1].join(words)   #this fails
>
> Change the above line to
>
> print sys.argv[1].decode("string_escape")
>
> s.decode("string_escape") returns a new string with all c-style escape
> sequences converted into the corresponding characters. This is an abuse -
> ahem, example of a general mechanism. Look for codecs if you want to learn
> more about it.
>
> Peter

Thank you.  I appreciate your help.  Yes, I missed it.  I will look at the
decode doc.  I expected that this was for converting character encodings
(codepages).  This does work under Python 2.3, and decode was available in
2.2.

However, I am in a Python 2.1 environment.  Do you know of any techniques
that would work under Python 2.1?





More information about the Python-list mailing list