Python script problem - ListFields / Calculate Field

Discussion created by e.blondel on Jul 13, 2010
Latest reply on Jul 15, 2010 by e.blondel
Hi everybody,

I have some problem with a python script, maybe could you have an idea to help me..
I put the python script as attached file.
I am working with species distribution. In my script:
"Input_Sp" is the species distribution shapefile
"Input_Table'' is the result of IDENTITY between the Input_Sp to a grid shapefile
"PK_REL" is the new field which I want to calculate

I will explain it trying to be clear because it is a little complex!! Also I want to add that I am starting with Python (since a few days) so It is probably that my script seems to be a draft script...

- The name of a species shapefile is for example "d1_alb.shp" and the probability field in the attribute table is [D1_ALB], so, the probability field name is changing according to the species (the 3 alpha code is the species identifier)

In the identity result (Input_Table) I have two area fields: [AREA_K] (area of each polygon) and [AREA_I] (area of each initial gridcell)
and I need to perform this operation: [D1_ALB] * [AREA_K] / [AREA_I]

but the problem is that from one species to another, the 3alpha species code will change so for example if I want to calculate reallocation for an other species (example: HER) the expression will be:
[D1_HER] * [AREA_K] / [AREA_I]

In order to automate this, I have started to write a little script in PYTHON language, that I could integrate in the model. I am starting in Python programming and it is not so easy.
In this script I am using a function (gp.Listfields) that allows me to search and find the three fields in my input_table. For searching the two area fields there is no problem, but for searching the probability field, it is the same problem: I cannot write [D1_ALB] in the script, because the name of this field is changing. I tried to used a "%" character like in SQL (D1_%) to search the field which name starts with "D1_" but it does not run.. there is an error...
so my first question is:
--> With gp.ListFields, Is there a solution to search a field without having the whole field name?

To get around this problem, I had a idea which is consisting in «extracting» the 3alphacode from shapefile name and using it then for searching the [D1_3alphacode] in the Input Table. I lead to do that, but I don't lead to integrate it into the calculation expression (I put "????here there is my doubt????" in my script to indicate to you where is my problem):
I can calculate AREA_K / AREA_I but, though I lead to find the probability field in Input_Table, I don't lead to apply it in the calculation expression...--> this my second problem

please find the attached script. If you have some idea to solve thise problem, it would be very very helpful for me.

Thanks a lot in advance for looking at this issue...