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.

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
or
elephant | cat, I want to keep elephant.

The length of characters are different.

share|improve this question

1 Answer 1

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

share|improve this answer
1  
He should split on " | " (with spaces) instead of "|" right? –  Erica Aug 30 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 at 19:58
1  
it would be safer to remove the spaces after the split. Like this : !field!.split("|")[0].strip() –  radouxju Aug 30 at 20:12
    
Thanks @radouxju, post edited to address your comments. –  Aaron Aug 30 at 20:21

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.