EOL while scanning string literal

7807
7
Jump to solution
05-08-2015 06:02 AM
MeredithGreen
New Contributor III

I'm in the process of creating a tool using a script.  I keep getting the error ERROR 000989: Python syntax error: Parsing error <type 'exceptions.SyntaxError'>: EOL while scanning string literal (line 83) Failed to execute (CalculateField).

I have been staring at my script at line 83 trying to figure out what's wrong but I just can't see the problem.  I wanted to know if someone can take a look at this section of my script and see if they spot the problem.  Line 83 is in bold and colored red.  Any help would be greatly appreciated.

codeblock3 = """def Reclass(ProjectLd):

      if ProjectLd == "0401H - HARN STATE PLANE LAMBERT CONFORMAL CONIC, CALIFORNIA I ZONE":

                return '0401H'

      if ProjectLd == "0402H - HARN STATE PLANE LAMBERT CONFORMAL CONIC, CALIFORNIA II ZONE":

                return '0402H'

      if ProjectLd == "0403H - HARN STATE PLANE LAMBERT CONFORMAL CONIC, CALIFORNIA III ZONE":

                return '0403H'

      if ProjectLd == "0404H - HARN STATE PLANE LAMBERT CONFORMAL CONIC, CALIFORNIA IV ZONE":

                return '0404H'

      if ProjectLd == "0405H - HARN STATE PLANE LAMBERT CONFORMAL CONIC, CALIFORNIA V ZONE":

                return '0405H'

      if ProjectLd == "0406H - HARN STATE PLANE LAMBERT CONFORMAL CONIC, CALIFORNIA VI ZONE":

                return '0406H'

      if ProjectLd == "0501H - HARN STATE PLANE LAMBERT CONFORMAL CONIC, COLORADO CENTRAL ZONE":

                return '0501H'

      if ProjectLd == "0502H - HARN STATE PLANE LAMBERT CONFORMAL CONIC, COLORADO NORTH ZONE":

                return '0502H'

      if ProjectLd == "0503H - HARN STATE PLANE LAMBERT CONFORMAL CONIC, COLORADO SOUTH ZONE":

                return '0503H'

      if ProjectLd == "0600H - HARN STATE PLANE LAMBERT CONFORMAL CONIC, CONNECTICUT ZONE":

                return '0600H'

      if ProjectLd == "0903H - HARN STATE PLANE LAMBERT CONFORMAL CONIC, FLORIDA NORTH ZONE":

                return '0903H'

      if ProjectLd == "1501H - HARN STATE PLANE LAMBERT CONFORMAL CONIC, KANSAS NORTH ZONE":

                return '1501H'

      if ProjectLd == "1502H - HARN STATE PLANE LAMBERT CONFORMAL CONIC, KANSAS SOUTH ZONE":

0 Kudos
1 Solution

Accepted Solutions
ToddBlanchette
Occasional Contributor II

You could also get rid of your entire list of IF statements by just returning the first 5 characters of your string variable, which looks like what you're trying to achieve here.  Something like:

def Reclass(ProjectLd):

     return ProjectLd[:5]      # will send back a string containing the first 5 characters of ProjectLd

which doesn't even really need its own method... you could just parse the string at the point you're calling it (unless I'm missing something else this method does, then just insert it underneath).

View solution in original post

7 Replies
JamesCrandall
MVP Frequent Contributor

I suspect it has something to do with the triple quotes in the very first line:

codeblock3 = """def Reclass(ProjectLd):

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

The error alone makes me think it is your data, not your code.  You are using the field calculator I assume?  That error message is common with the field calculator when there is a newline character of some form in a text field in a table being worked on.

If you Google "CalculateField parsing error EOL" or something similar you will find plenty of discussion about how to deal with it.  In the past, some folks would say use the VB parser instead of Python, but I consider that poor advice today since VB's sunset is pretty far along.  Some folks add additional code to find and replace new line characters, while other folks use an UpdateCursor instead of field calculator.

There are lots of suggestions, not sure any one of them is a silver bullet.

0 Kudos
Zeke
by
Regular Contributor III

Wouldn't it be simpler just to return the first element of a split ProjectLD? Gets rid of your if statements. Or skip the function and just set codeblock3 to the first element.

return ProjectLD.split()[0]

or

codeblock3 = ProjectLD.split()[0]

0 Kudos
ToddBlanchette
Occasional Contributor II

You could also get rid of your entire list of IF statements by just returning the first 5 characters of your string variable, which looks like what you're trying to achieve here.  Something like:

def Reclass(ProjectLd):

     return ProjectLd[:5]      # will send back a string containing the first 5 characters of ProjectLd

which doesn't even really need its own method... you could just parse the string at the point you're calling it (unless I'm missing something else this method does, then just insert it underneath).

curtvprice
MVP Esteemed Contributor

arcpy.CalculateField(tbl, field, "!ProjectLd![:5]", "PYTHON")

0 Kudos
MeredithGreen
New Contributor III

This fixed the issue!  Thank you so much for the tip.

0 Kudos
BlakeTerhune
MVP Regular Contributor
0 Kudos