[Tutor] Syntax Error Messages

Hugo Arts hugo.yoshi at gmail.com
Sat Oct 23 02:35:37 CEST 2010


On Fri, Oct 22, 2010 at 8:49 PM, Terry Green <tmantjg at yahoo.com> wrote:
> Am new to Python, and having difficulty with Error Messages
>
> I ‘m using Python 3.1
>
> And PyScripter as my editor
>
>
>
> I want to process a comma delimited file one line at a time and
> Interact with the fields within each line.
> I found this script when looking at the CVS module and loaded
> It into PyScripter, but get: Syntax Error: Invalid Syntax
> Cannot figure out why and Googleing for help doesn’t help
>
> Any ideas?
>
> import csv, sys
>
> filename = "some.csv"
>
> reader = csv.reader(open(filename, "rb"))
>
> try:
>
>     for row in reader:
>
>         print (row)
>
> except csv.Error, e:
>
>     sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))
>
>
> thanks,
> Terry Green
>

You should have gotten more information than just that error. The
offending line should also be printed, along with an estimate of where
exactly the error occurred. Please copy-paste *all* information the
traceback gives you, unless it is too long (a good rule of thumb is
"would *I* read all this just to help some stranger out?"). The more
information we have to work with, the better.

In any case, that python code was written for python 2.x, not 3.x. If
you change "except csv.Error, e:" to "except csv.Error as e:" it
should work.

Hugo


More information about the Tutor mailing list