Model Builder to python. Error at calculate field function

330
2
Jump to solution
08-20-2020 02:22 PM
helenchu
Occasional Contributor II

All, 

My model works fine but when I export it to python I got syntax error SyntaxError: "EOL while scanning string literal" .  Any idea why?  I've tried many simple calculation functions.  Got same errors for all.  Thank you!

EX:

# Process: CalField_Ac (Calculate Field) (management)
ToAcres = arcpy.management.CalculateField(in_table=FromSqft, field="Acres", expression="round((float(!Sqft!)/43560),4)", expression_type="PYTHON3", code_block="", field_type="TEXT")[0]

or 

# Process: CalField_BlkNum (Calculate Field) (management)
Calculate_BlkNum = arcpy.management.CalculateField(in_table=myTable, field="Blk_Num", expression="fn(!Blk_Num!,!Acct_Num!)", expression_type="PYTHON3", code_block="def fn(Blk_Num,Acct_Num):
 if (Blk_Num) is not None:
 return Blk_Num 
 else:
 return 1
 ", field_type="TEXT")[0]‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

Indentation is crucial for codeblocks.  Triple quotes helps

code_block = """
def fn(Blk_Num, Acct_Num):
    if (Blk_Num) is not None:
        return Blk_Num 
    return 1
""" 

... sort of retired...

View solution in original post

2 Replies
DanPatterson
MVP Esteemed Contributor

Indentation is crucial for codeblocks.  Triple quotes helps

code_block = """
def fn(Blk_Num, Acct_Num):
    if (Blk_Num) is not None:
        return Blk_Num 
    return 1
""" 

... sort of retired...
helenchu
Occasional Contributor II

That's it.  Triple quotes solved my issue.  Thank you so much Dan Patterson.

0 Kudos