compile / exec

Ken Seehof kens at sightreader.com
Wed Mar 14 17:24:19 EST 2001


The first line in the string is indented, which is a syntax error.  The
first line of the string has to be non-indented.

"eric wilson" wrote:
> hi -
> i am new to python and i'm trying to use the compile / exec feature for
> speeding up a loop.  i'm using python v2.0 with pythonwin.  i don't
> understand why this doesn't work....
> thanks in advance for the help.
> file://eric
> 
> the code snippet looks like this:
>     exec_code = compile("""
>     for eachLine in map(strip, allLines):
>         mySequence = split('\s\s+|\t', eachLine)
>         foundFields = str(gm2local(DateTimeFromString(mySequence[0])))[:-3]
> + '\t' + mySequence[1]+ '\t' + mySequence[4]+ '\t' + mySequence[5]+ '\t' +
> mySequence[9]+ '\t' + mySequence[16] + '\n'
>         fw.write(foundFields)
>         count = count + 1
>     """, '', 'exec')
>     exec exec_code
> 
> the error message that i get when this piece of code runs is:
> Traceback (most recent call last):
>   File "C:\Python20\Pythonwin\pywin\framework\scriptutils.py", line 301, in
> RunScript
>     exec codeObject in __main__.__dict__
>   File "E:\W2\pytest\logman.py", line 114, in ?
>     checkArgs()
>   File "E:\W2\pytest\logman.py", line 110, in checkArgs
>     parmFind(sys.argv[1], sys.argv[2])
>   File "E:\W2\pytest\logman.py", line 55, in parmFindRAM
>     exec_code = compile("""
>   File "<string>", line 2
>      for eachLine in map(strip, allLines):
>      ^
>  SyntaxError: invalid syntax

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20010314/0635f39e/attachment.html>


More information about the Python-list mailing list