Search code examples
pythonexcelstringrowmultiple-columns

Remove specific text from Excel using Python


I'm doing a project and I need to remove some specific text from a string . For example, from these strings I would like to remove the numbers in the end (_230; _0 ; _240) until the '__' .

'KSS_10292_TRIPLEBAND_230' , 'ATR451606_0', 'K_80010510V01_240'

I'm using python and these strings are in a Excel column named 'AntennaReference'.


Solution

  • You can use rsplit function which returns a list of the words in the string, using sep as the delimiter string.(More here)

    Complete code looks like this:

    test_string = "KSS_10292_TRIPLEBAND_230"
    splited_list = test_string.rsplit("_", 1) # ['KSS_10292_TRIPLEBAND', '230']
    splited_string = splited_list[0] # KSS_10292_TRIPLEBAND
    

    Or in one line:

    "KSS_10292_TRIPLEBAND_230".rsplit("_", 1)[0]