Long Interger value to resemble a integer separated by commas

538
3
01-04-2012 09:23 AM
KaraO_Neill
New Contributor
I have been successful at creating a very basic script when ran to populate labels inside of my layout view in ArcMap. I am no python master and have pieced this together from research. I also have to do this about 40 or so labels so if anyone as a way to write this easier, I am all ears/eyes!

************
import arcpy
from arcpy import env
import string

# Set the workspace

mxd = arcpy.mapping.MapDocument("CURRENT")

# Set the variable for each element

epz_n = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "epz_n")[0]
cur2_n = arcpy.SearchCursor("tbl_10_to_EPZ", "[Compass] = 'N'")
epz_wnw = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "epz_wnw")[0]
cur2_wnw = arcpy.SearchCursor("tbl_10_to_EPZ", "[Compass] = 'WNW'")

for row in cur2_n:
    epz_n.text = row.getValue("POP_RATIO")

for row in cur2_wnw:
    epz_wnw.text = row.getValue("POP_RATIO")   
  
mxd.save()
del mxd

*****************

My question is how do I get that value to resemble a integer separated by commas?

If my value is 15000 I want it to look like 15,000.

Thanks for any suggestions.
Tags (2)
0 Kudos
3 Replies
DanPatterson_Retired
MVP Emeritus
Google search on
Python format number with commas

yields several options, eg.

http://stackoverflow.com/questions/1823058/how-to-print-number-with-commas-as-thousands-separators-i...
0 Kudos
KaraO_Neill
New Contributor
Thanks Dan. I will give this a shot...

Although I am not entirely sure where to insert this code or modify it to make it work for me. Do you have a suggestion?
0 Kudos
KaraO_Neill
New Contributor
This seems to work. Is there an easier way to do this I have a TON of labels that I need to dynamically populate based on the table values? I have too many constraints in order to use a label engine over multiple projects and make them all look the same way. We are trying to automate this process. Thanks!

import arcpy
from arcpy import env
import string

# Set the workspace

mxd = arcpy.mapping.MapDocument("CURRENT")

# Set the variable for each element

epz_n = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "epz_n")[0]
cur2_n = arcpy.SearchCursor("tbl_10_to_EPZ", "[Compass] = 'N'")
epz_wnw = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "epz_wnw")[0]
cur2_wnw = arcpy.SearchCursor("tbl_10_to_EPZ", "[Compass] = 'Wnw'")
  
def intToStringWithCommas(x):
    if type(x) is not int and type(x) is not long:
        raise TypeError("Not an integer!")
    if x < 0:
        return '-' + intToStringWithCommas(-x)
    elif x < 1000:
        return str(x)
    else:
        return intToStringWithCommas(x / 1000) + ',' + '%03d' % (x % 1000)

for row in cur2_n:
    value = row.getValue("POP_RATIO")
    epz_n.text = intToStringWithCommas(value)
  
for row in cur2_wnw:
    value = row.getValue("POP_RATIO")
    epz_wnw.text = intToStringWithCommas(value)

mxd.save()
del mxd
0 Kudos