Select to view content in your preferred language

Arcpy Calculate Field Stores a String in Scientific Notation

3153
13
12-07-2018 11:44 AM
BrandonZaitz
Occasional Contributor

I have an attribute field that is API well numbers and when I calculate a "TEXT" field using arcpy it converts those API numbers into scientific notation. However, when I calculate the field in Arcmap using the GUI it stores the full API number correctly. Has anyone else had this issue? How might i ensure that this doesn't happen? 

I have tried both:

arcpy.CalculateField_management(shapefile,fieldname,"!{}!".format(inputfieldname),"PYTHON")

and:

arcpy.CalculateField_management(shapefile,fieldname,"str(!{}!)".format(inputfieldname),"PYTHON")

0 Kudos
13 Replies
BrandonZaitz
Occasional Contributor

Below is the entire function:

def CreateAPIText(shapefile,inputfieldname):
     import arcpy
     fieldname = "API_Text"
     #Add field for string API
     arcpy.AddField_management(shapefile,fieldname,"TEXT")
     #Calculate value for API TEXT field
     arcpy.CalculateField_management(shapefile,fieldname,"!{}!".format(inputfieldname),"PYTHON")
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I just tested your code using the interactive Python window in ArcCatalog 10.6.1, and the results are as expected for both a feature class table and DBF table:

0 Kudos
BrandonZaitz
Occasional Contributor

Ok, so i have narrowed it down to .format() causing the problem.

The code below DOES NOT give me scientific notation as a result.

def CreateAPIText(shapefile,inputfieldname):
     import arcpy
     fieldname = "API_Text"
     #Add field for string API
     arcpy.AddField_management(shapefile,fieldname,"TEXT",14)
     #Calculate value for API TEXT field
     arcpy.CalculateField_management(shapefile,fieldname,"!API!","PYTHON", "")

The code below WAS outputing the fields in scientific notation.

def CreateAPIText(shapefile,inputfieldname):
     import arcpy
     fieldname = "API_Text"
     #Add field for string API
     arcpy.AddField_management(shapefile,fieldname,"TEXT",14)
     #Calculate value for API TEXT field
     arcpy.CalculateField_management(shapefile,fieldname,"!{}!".format(inputfieldname),"PYTHON", "")

However, i can not seem to replicate the problem this morning.

If if happens again I may be back but thanks to everyone for the input.

0 Kudos
DanPatterson_Retired
MVP Emeritus

bare formatting can cause some issues depending on what is first passed to it.  You can mitigate a bit by specifying how to treat numerics by prespecifying the size and decimals (even for integers)

a = 30015364700000

"{:<16.0f}".format(a)

'30015364700000  '

with a space to spare

0 Kudos