2

I am trying to do a simple concatenation using the ArcGIS 10.2 field calculator with Python code. I have a field of census tracts numbers (tract). I want to concatenate this with the numeric codes for the state and county so that I can join this table to the spatial geometry. The new field is defined as 'text'.

My code is "06085" + "!tract!" This yields the correct concatenation PLUS .0 on each record. So 0608512345.0

I have also tried str(06085)+str(!tract!) which yields the same results. What am I missing?

4
  • + str(int(!tract!))
    – FelixIP
    Feb 29, 2016 at 19:01
  • @FelixIP, she would lose the leading zeros....I think there can be leading zeros on the tract number itself.
    – Tom
    Feb 29, 2016 at 19:02
  • @Tom I see no mentioning of leading zeros, and if there are leading zeros on a tract, it makes it either non-numeric or decimal <1
    – FelixIP
    Feb 29, 2016 at 19:04
  • @FelixIP, she didn't explicitly mention it, but the state FIPS code in her example has it, which suggests that the tract numbers might as well--and the fact that she says it's for census tracts, which can have leading zeros: census.gov/geo/reference/gtc/gtc_ct.html. However, you're right, that her field type must already be numeric...which could point to a other problems.
    – Tom
    Feb 29, 2016 at 19:07

2 Answers 2

3

So, you don't want the .0?

Then, you should use:

"06085{}".format( !Tract! ).split('.')[0]
3
  • Brilliant! This worked :-) Thank you! The US Census state FIPS codes do indeed have a leading zero which makes it much more difficult. Feb 29, 2016 at 20:28
  • Can you help me understand the Python: why do the string commands format() or str() add a .0 on the end of numeric text? Is there a way to avoid this so I don't have to use the .split() ? Feb 29, 2016 at 20:29
  • @AllisonMeezan, it must be that the "Tract" field is set to store floats or doubles. Because of that, Python is reading them with a decimal. But this also means that a simpler version should work: '06085{}'.format(str(!Tract!))
    – Tom
    Feb 29, 2016 at 20:45
2

Try "06085{}.0".format( !Tract! )

enter image description here

This yields the desired text output:

enter image description here

2
  • I don't think that yields the desired output. As far as I can tell, the whole point of the question was to get rid of the decimal--not to explicitly add it. Though the original question wasn't totally clear.
    – Tom
    Mar 1, 2016 at 22:46
  • Yes, I realised that might be the case after I read your answer. But I agree the question isn't clear
    – Midavalo
    Mar 1, 2016 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.