Home > Back-end >  How to split a string and at a defined and keep the part of that string inclusive of the defined wor
How to split a string and at a defined and keep the part of that string inclusive of the defined wor

Time:10-19

I have a string, part of which I want to keep and use for plot headings, the rest can be discarded. I have a method of doing it (below) but I am sure there is a more pythonic way of doing this rather than the clumsy find method

Code

filename = "Merged_2001_1234567_ZZ00_device_name_Row1_Ch480_1V_3A_4800OHMS_13Oct2022.csv"
filename = os.path.splitext(filename)[0]
b = filename.find("Row1")
print("b : ",b, '\n'*3)
print(filename[b:], '\n'*3)

Returns

b :  37 

Row1_Ch480_1V_3A_4800OHMS_13Oct2022

It is returning what I am after, but I couldn't find a better way to do it without losing the word I was splitting at ("Row1"). Has anyone got a more robust method?

CodePudding user response:

Doing it with string methods and slicing is fine you only have to do a couple. For this you could replace os.path.splitext with the string native method filename = filename.split(".")[0]

If you find yourself doing a lot of these, compiling a regex might be beneficial

import re

filename = "Merged_2001_1234567_ZZ00_device_name_Row1_Ch480_1V_3A_4800OHMS_13Oct2022.csv"
exp = re.compile(r"Row1_. ?(?=\.)")
ret = re.search(exp, filename).group()
print(ret)

>>> Row1_Ch480_1V_3A_4800OHMS_13Oct2022

or plainly if you don't worry about doing the same search repeatedly

ret = re.search(r"Row1_. ?(?=\.)", filename).group()

Some reference: How can I match "anything up until this sequence of characters" in a regular expression?

  • Related