Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Hi I nedd to create a script in Python for use in ArcMap 10. This script must be able to calculate a field from the shp currently selected in ArcMap. Any ideas?, I'm new to Python so any code example will be most welcome. Thank you, I'm confident you will be able to help me

share|improve this question
    
I believe I've tried this before. To my knowledge (limited as it may be), this operation is outside of the scope of pure arcpy and will require use of ArcObjects. –  Nathanus Sep 12 '11 at 15:13

2 Answers 2

Unless I'm missing something from your question, you're just asking how to calculate [field2] from [field1] in the same shapefile, correct? If so, you just need to use the "Calculate Field" tool to do this. And if you just want to run this tool on a selected feature(s), all ArcGIS tools will only work on the selection set if one exists.

ArcGIS Help has python code examples that show exactly how to do it in Python.

Look in the "Code Sample" section about halfway down help. Their [generalized] example is:

import arcpy
from arcpy import env
env.workspace = "C:/data"
arcpy.AddField_management("<in_table>", "<field>", "TEXT", "", "", "20")
arcpy.CalculateField_management("<in_table>", "<field_to_calc>",
                                <field_with_values>, "PYTHON")

If that is all you are after, it's pretty simple.

share|improve this answer

After running a tool in ArcMap you can go to the results window (Geoprocessing > Results) and right click on the finished tool. This will give the option to copy the Python code that you can then paste into your Python shell of choice. This is a great way to begin learning. After working with Python for a couple years now I still find myself doing this just to find out how the GP tool wants the parameters.

share|improve this answer
    
I didn't know that! +1 for fun facts. –  Nathanus Sep 12 '11 at 22:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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