[Tutor] Quoting trouble

Kent Johnson kent37 at tds.net
Fri Jan 20 20:45:11 CET 2006


Marilyn Davis wrote:
> Dear Tutors,
> 
> I'm having a problem and going around in circles.  I'm on Python 2.4.1.
> 
> This odd address line comes in email:  
> 
> 
>>>>line = '''"ma >> \"marilyn at maildance.com\"" <marilyn at maildance.com>'''
> 
> 
> It makes sense to me, and to the MTA.  I want to use it to create a
> variable:
> 
> 
>>>>exec('h_to = %s' % line)
> 
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "<string>", line 1
>     h_to = "ma >> "marilyn at maildance.com"" <marilyn at maildance.com>
>                          ^
> SyntaxError: invalid syntax
> 
> So, how do I keep the \" in there?

Why are you using exec? What's wrong with
   h_to = line
??

If you really need the exec then you need to put some quotes in there 
somewhere, either
   exec('h_to = '''%s''' ' % line)
or
   exec('h_to = %s' % repr(line))

Kent



More information about the Tutor mailing list