[GIS] Replacing characters after certain word in Python parser of ArcGIS Field Calculator

arcgis-desktopfield-calculatorpython-parser

I have a table with field which contains path to database. The same thing in this field will be the extension of file.
Here is how it looks like

D:\Проекты\Москва для тестов\Test.gdb\graf_fd

I need to have this

D:\Проекты\Москва для тестов\Test.gdb\

Probably I should replace everything after 'gdb\'. I know that I can type something like delete last 7 charachters, but the name of dataset can be another in other databases as well as the length of string data before 'gdb\'. So are there codes for removing or replacing the characters after certain word?

Best Answer

Provided you have two fields in the feature class, LongPath and ShortPath. Both are of Text type.

To calculate the ShortPath field, right-click the field name in the attribute table and choose Field Calculator, switch to Python parser and run:

'\\'.join(!LongPath!.split('\\')[:-1])

This will construct a string path with all parts except the last one.

LongPath                                        ShortPath
D:\Проекты\Москва для тестов\Test.gdb\graf_fd   D:\Проекты\Москва для тестов\Test.gdb
D:\Проекты\Москва для тестов\Test.gdb\graf_fd   D:\Проекты\Москва для тестов\Test.gdb

More readings on Python used here:

str.split() and str.join()