Thursday, 2 July 2015

arcgis desktop - Adding new field in ArcMap from data of existing field?


I want to create a new field with years data.



There is an existing field with the timestamp YYYYMMDDHHMM but I just want the years from that field to the new field.


That way I can display just the years when I create a graph.


Is there anyone that can explain the code to do this?


I am not a programmer.



Answer



Assuming your existing timestamp field is a string:




  1. Add a new field (Open the attribute table of your feature class>Click Upper Left Dropdown>Add Field) as a long integer.





  2. Right click new field and select "Calculate Field".




  3. Change your parser to Python and use the following expression:


    !ExistingTimeStampField![:4]




Note that you need to replace the "ExistingTimeStampField" between the exclamations with the actual name of your field (should be able to auto-populate in the Fields list box). This will trim off the leading 4 characters from your existing field which I presume will always be the 4-digit year.


Edit: If you wish to break out more information you can simply update the indices of your query like such (Note that my original [:4] is the same as saying [0:4]):



!ExistingTimeStampField![4:6]

This would return the MM in YYYYMMDDHHMMSS. [6:8] would return the DD, and so on. See here for a more detailed explanation of substrings: http://docs.python.org/tutorial/introduction.html#strings


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...