compile / exec

eric wilson ewilson at nospam.jamdat.com
Wed Mar 14 20:28:42 EST 2001


thanks ken - that's got it!  :-)


"Ken Seehof" <kens at sightreader.com> wrote in message
news:mailman.984615559.29257.python-list at python.org...
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.
> //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





More information about the Python-list mailing list