How to convert "n in" to "n ft" within attribute tables?

1093
8
Jump to solution
05-09-2018 04:54 AM
MatthewForsberg
New Contributor III

I have multiple layers with attribute tables and the values go something like "2 in x 4 in" but I need to make it "n ft x n ft." How could I go about this process in the field calculator, or other ways?

Thanks in advance!

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

You could use a field calculation (Python parser) using the following function:

def ChangeInch2Feet(txt):
    factor = 0.0833333
    decimals = 2
    lst_in = txt.split(' ')
    lst_out = []
    for a in lst_in:
        try:
            inch = float(a)
            feet = inch * factor
            feet_txt = str(round(feet, decimals))
            lst_out.append(feet_txt)
        except:
            if a == 'in':
                lst_out.append('ft')
            else:
                lst_out.append(a)
    return ' '.join(lst_out)

When you execute the calculation:

ChangeInch2Feet(!YourFieldNameWithDataInInches!)

... using the example you provided, this will yield:

0.17 ft x 0.33 ft

There might be exceptions in your data, and this function does not account for any exceptions. 

View solution in original post

8 Replies
XanderBakker
Esri Esteemed Contributor

You could use a field calculation (Python parser) using the following function:

def ChangeInch2Feet(txt):
    factor = 0.0833333
    decimals = 2
    lst_in = txt.split(' ')
    lst_out = []
    for a in lst_in:
        try:
            inch = float(a)
            feet = inch * factor
            feet_txt = str(round(feet, decimals))
            lst_out.append(feet_txt)
        except:
            if a == 'in':
                lst_out.append('ft')
            else:
                lst_out.append(a)
    return ' '.join(lst_out)

When you execute the calculation:

ChangeInch2Feet(!YourFieldNameWithDataInInches!)

... using the example you provided, this will yield:

0.17 ft x 0.33 ft

There might be exceptions in your data, and this function does not account for any exceptions. 

MatthewForsberg
New Contributor III

Is there some sort of website or book on Python? It seems to be very useful..

0 Kudos
DanPatterson_Retired
MVP Emeritus

Python?... Google will give you a few hits.

The main one https://www.python.org/

Any book by O'Reilly ... there are dozens, but a gentle intro is

http://shop.oreilly.com/product/0636920028154.do

Within the Arc* suite 

http://pro.arcgis.com/en/pro-app/arcpy/main/arcgis-pro-arcpy-reference.htm

http://pro.arcgis.com/en/pro-app/help/data/tables/fundamentals-of-field-calculations.htm

docs are for Pro but applicable to *Map

XanderBakker
Esri Esteemed Contributor

Hi Matthew Forsberg , you are right, you can do a lot with Python  in the ArcGIS Platform (and beyond).

There have been numerous questions related to useful resources to learn Python, including a blog posted and actively updated by Dan Patterson : /blogs/dan_patterson/2016/05/09/the-links . You will also find a lot of recommendations in this thread: Can anyone suggest a good learning python website? and you could have a look at this list: https://community.esri.com/groups/technical-support/blog/2014/03/26/7-easy-ways-learning-python-arcp... 

MatthewForsberg
New Contributor III

Hi, I've been busy working on a different project and finally got back to this...I copy and paste the Python calculation above but nothing happens, it comes up with an error. Could you show me in a screenshot how I should be coding this? I feel like I'm not doing it correctly.

0 Kudos
DanPatterson_Retired
MVP Emeritus

It might be easier if you showed your screen shot.

The 'def' goes in the code block section

this line is goes in the expression line ChangeInch2Feet(!YourFieldNameWithDataInInches!)

where you have to change the field name inside the double ! marks.

The parser is Python

The field calculation type is text/string

MatthewForsberg
New Contributor III

Okay, here is a screenshot of what I just tried attempting.

MatthewForsberg
New Contributor III

I just realized that I had to put "Diameter" where you told me to put it in the "YourFieldNameWithDataInInches." Thank you!