1

I have two fields named as "sample no" and "data_all" trying to find out whether "sample no" exist on "data_all" or NOT. Using field calculator python 9.3. if exist print 1 if not print 0 in check_in field. All field are in string format. For example see attached image in first row "101" found in "data_all" the check_in can print 1 where as in row 2 should be "0". Beginner in python.

enter image description here

1

VBScript Parser: (Python Parser example further down)
In your Field Calculator choose Show Code Block and enter the following in the Pre-Logic Script Code:

d = InStr([data_all], [sampleno])
If d > 0 Then
    e = 1
Else
    e = 0
End If

and in the bottom section enter e

Syntax is InStr(<string to search in>, <string to find>)

Example (Sorry I used slightly different field-names in my screenshots):
enter image description here

Output:
enter image description here

Note:
If you have any shorter values such as 1, 4, 10 these will be found in longer values like 100, 204, 1000.

Python Parser:
In your Field Calculator choose Show Code Block and enter the following in the Pre-Logic Script Code:

def checknum( sampleno, data_all ):
  e = 0
  for da in data_all.split(";"):
    if str(sampleno) == da:
      e = 1
  return e

and in the bottom section enter

checknum( !sampleno!, !data_all! )

Example:
enter image description here

Output:
enter image description here

Unlike with the VBScript Parser, the Python Parser example will do an actual match between two values, so shouldn't get any partial matches.

  • ah sorry just spotted you wanted in Python parser - I couldn't remember whether that was present in 9.3 (I'm using 10.4), so used the older VBScript parser. I'll redo for python. – Midavalo Mar 10 '16 at 7:02
  • I have updated my answer to also include a method using the Python Parser – Midavalo Mar 10 '16 at 7:26
  • thanks for immediate reply, managed to get the VBA code running with the error you already highlighted (short values), however unable to get the same result from python code, I am getting "0" for all records? – user68970 Mar 10 '16 at 12:10
  • Did you get any errors? Remember that Python is very strict on indenting, so make sure all your indents line up with the same indent level above - e.g. e=0, for..., return e should all be at the same indent, the if... one indent further in, and e=1 another again. def... is not indented, and neither is the checknum() in the bottom section. Also make sure the two parameters match your fieldnames in checknum( !sampleno!, !data_all! ) and the fieldnames need to be between ! exclamation marks. – Midavalo Mar 10 '16 at 17:17
  • thanks, no error message coming up, check field get filled with "0" just after the script. It appears that the script is running without any issue but not getting desired result. For indent i have basically copied your code and modified my field name to match the code. assume the code has proper indent? Appreciate your help. – user68970 Mar 11 '16 at 6:07
1

This is 1 line expression, no block required if data are similar to what shown:

(!data.all!find(!sample_no!)>0)*1
  • Getting syntex error on line 1, no result. Thanks – user68970 Mar 11 '16 at 6:09
  • (!data.all!.find(str(!sample_no!))>0)*1 – Midavalo Mar 11 '16 at 6:31
  • thanks, this 1 line expression works but still print "0" for all rows. If I change ">0" to "<0" then print "1" for all rows. Which means it is only executing false not the true. However when I run the expression as "( !data.all! .find('102')>0)*1" then I get "1" as result for first row. Just double checking my ArcGIS is 10.3.0.4284 and python PYTHON 9.3. If this is due to an bug please leave it. I can get around it. Highly appreciate both of you for your great support. – user68970 Mar 11 '16 at 15:29
  • Your 1st column either numeric or contaiins spaces. Rtrim Ltrim it – FelixIP Mar 11 '16 at 19:26
  • 1
    Mark as solved please – FelixIP Mar 15 '16 at 4:14

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.