[Tutor] Quoting trouble

Marilyn Davis marilyn at deliberate.com
Fri Jan 20 22:50:36 CET 2006


On Fri, 20 Jan 2006, Kent Johnson wrote:

Thank you so much Kent.  This is *very* helpful.

> 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
> ??

Explanation below.

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

>>> exec('h_to = '''%s''' % line ')
...

It seems to want more.

> or
>    exec('h_to = %s' % repr(line))

This one does!

>>> exec('h_to = %s' % repr(line))
>>> h_to
'"ma >> "marilyn at maildance.com"" <marilyn at maildance.com>'

Why I'm using exec:

I'm using exec so that I have a flexible scheme for collecting
headers.  Maybe you have a better suggestion?

I have a dictionary that starts:

            significant_headers = {
                'Cc: ':None,
                'CC: ':None,
                'From: ':None,
                'Return-path: ':None,
                'Sender: ':None,
                'Subject: ':None,
                'To: ':None}
and
            heads_to_find = significant_headers.keys()

So I collect the header texts into the dictionary items:

                for each in heads_to_find:
                    if not significant_headers[each] \
                           and text.startswith(each):
                        significant_headers[each] = text[len(each):].strip().replace('"','\\"').replace('\n',' ')
                        break

And then later, I make variables that match the variables that exim,
my MTA, uses.  For each header in the message exim provides a $h_to,
$h_from, etc.  I like my variable names to match exim's as much as
possible:

        for each in significant_headers.keys():
            this = '''self.h_%s = "%s"''' % \
                   (each[:-2].lower().replace('-','_'),
                    repr(significant_headers[each]))
            exec(this)

If I decide that I need to collect another header, I can just add it
to my dicitonary.

And now, with your help, I don't get any exceptions ... so far.

But, if there's another way to do this, I'd appreciate any improvement.

Thank you so much.

Marilyn




> 
> Kent
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

-- 



More information about the Tutor mailing list