How can I calculate a text value, the value of which is separated by a special character?

For example: dog | duck, I would like to keep the first value: dog
elephant | cat, I want to keep elephant.

The length of characters are different.


Try using the .split Python function in the field calculator. If, for example, you have dog|cat, the .split("|") function splits the string by | into two parts ['dog', 'cat']. The [0] at the end of the function states that you want the first of the two items in the list. Finally, to be sure there are no remaining spaces in the rows, use .strip()

enter image description here

| improve this answer | |
  • 1
    He should split on " | " (with spaces) instead of "|" right? – Erica Aug 30 '14 at 19:48
  • Good call @Erica, this will be sensitive to spaces if indeed there are any in the field. Otherwise there will be spaces left over. – Aaron Aug 30 '14 at 19:58
  • 1
    it would be safer to remove the spaces after the split. Like this : !field!.split("|")[0].strip() – radouxju Aug 30 '14 at 20:12

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.