Programmatically format numeric fields with arcpy

2306
7
Jump to solution
05-01-2019 10:47 AM
Yuhash
by
Occasional Contributor

***

UPDATE May 10, 2019. Findings so far confirm there is no programmatic access to the Numeric Field Display Properties (e.g. Pad with zeroes, Show thousands separators, etc). There is an open IDEAS topic for this functionality. 

Until implemented, the workaround is to access the property in a saved layer file and applied to a feature layer. An example is provided by Curtis Price below.

***

I know how to manually format numeric fields as described under Formatting numeric fields in tables.

But I would like to access and script the functionality to display numeric fields with thousand separators in a Python Toolbox tool. Is this possible? The number display format properties listed in the screen capture below aren't exposed via Field properties, Table views or any other objects I have researched.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor

This is the workflow I am suggesting. southdakotacities.lyr is a layer file with the comma formatting saved from setting it up in ArcMap.

Of course this requires that the field with commas set (POPULATION in my case) is both in the .lyr file and in the target feature class (in this case wyoming_cities).

from arcpy import env
from arcpy import mapping as AM

# get active data frame
mxd = AM.MapDocument("CURRENT")
df = mxd.activeDataFrame

# get layer definition from layer file, (includes field formatting) 
lyr = AM.Layer("southdakota_cities.lyr")
# update name and source
lyr.name = "wyoming_cities"
lyr.replaceDataSource(env.workspace,"FILEGDB_WORKSPACE", "wyoming_cities")

# add layer to map, this layer has wyoming_cities with commas in attribute table
AM.AddLayer(df, lyr)
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

0 Kudos
7 Replies
DanPatterson_Retired
MVP Emeritus

Sadly, most formatting options of which you seek return strings

"{:,}".format(num)
'123,456,789.01'

f"{num:,}"
'123,456,789.01'

As of recent versions of python (3.6 I think), you can format numbers with the underscore to make it easier to read when you are creating or using them, they don't retain the syntax when exporting unless you format it back as a string.

newnum = 123_456_789.01

newnum
123456789.01‍‍‍‍

f"{newnum:,}"
'123,456,789.01'
curtvprice
MVP Esteemed Contributor

Dan, I think you're missing the question. Yuriko wants to modify the display properties of fields within a layer object in ArcMap.

Unfortunately, the only way I know how to do this in arcpy is to create a layer with the formatting properties set up ahead of time using the ArcMap user interface, save it as a .lyr file. Then at script run time, load the .lyr from disk and point its source property to a dataset. To get in this deep to the ArcMap object model you must delve into .NET and do it in ArcObjects.

DanPatterson_Retired
MVP Emeritus

Ahhh. for some reason, I thought he wanted to calculate a new field with the format desired format.

I just saw Python and thought about the python 3.6 underscore syntax which is great for working with big numbers and a compliment to the f-string formatting 

Yup... can't be done  

Yuhash
by
Occasional Contributor

Many thanks to both of you, Dan and Curtis! It is helpful to know both these things!

0 Kudos
Yuhash
by
Occasional Contributor

Thanks Curtis! As you clearly described, despite setting and saving the Feature Layer field property to display the thousand separators, when I update the Layer's data source the Layer's field properties return to the default without the separators.

0 Kudos
curtvprice
MVP Esteemed Contributor

This is the workflow I am suggesting. southdakotacities.lyr is a layer file with the comma formatting saved from setting it up in ArcMap.

Of course this requires that the field with commas set (POPULATION in my case) is both in the .lyr file and in the target feature class (in this case wyoming_cities).

from arcpy import env
from arcpy import mapping as AM

# get active data frame
mxd = AM.MapDocument("CURRENT")
df = mxd.activeDataFrame

# get layer definition from layer file, (includes field formatting) 
lyr = AM.Layer("southdakota_cities.lyr")
# update name and source
lyr.name = "wyoming_cities"
lyr.replaceDataSource(env.workspace,"FILEGDB_WORKSPACE", "wyoming_cities")

# add layer to map, this layer has wyoming_cities with commas in attribute table
AM.AddLayer(df, lyr)
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
Yuhash
by
Occasional Contributor

Hi again Curtis, thanks!

The tool's workflow copied an MXD, updated data sources of layers and refreshed table values drawn from  data driven feature class attributes. I was initially losing the manually set numeric field formats, but that was due to my order of operations. I'll update the discussion post with the final findings!

0 Kudos