i've been working on bit of code take bunch of histograms other files , plot them together. in order make sure legend displays correctly i've been trying take titles of these original histograms , cut out bit of information isn't needed more.
the section don't need takes form (a mass=200 gev), i've had no problem removing what's inside parentheses, unfortunately i've tried parentheses either has no effect, negates code removes text, or throws errors.
i've tried using suggestions from; remove parenthesis , text in file using python , how can remove text within parentheses regex?
the error current attempt gives me is
'str' object cannot interpreted integer this section of code:
histo_name = '' # list of things not want show in our legend keys remove_list = ["(a mass = 200 gev)"] # these 2 lines use re module remove things piece of text # specified in remove list remove = '|'.join(remove_list) regex = re.compile(r'\b('+remove+r')\b') # creating correct name stacked histogram histo in histos: if histo == histos[0]: # place_holder contains edited string want set # histogram title place_holder = regex.sub('', str(histo.getname())) histo_name += str(place_holder) histo.settitle(histo_name) else: place_holder = regex.sub(r'\(\w*\)', '', str(histo.getname())) histo_name += ' + ' + str(place_holder) histo.settitle(histo_name) the if/else bit because first histogram pass in isn't getting stacked want keep it's own name, while rest stacked in order hence '+' etc, thought i'd include it.
apologies if i've done obvious wrong, i'm still learning!
from python docs - match literals '(' or ')', use \( or \), or enclose them inside character class: [(] [)].
so use 1 of above patterns instead of plain brackets in regex. e.g.remove_list = ["\(a mass = 200 gev\)"]
edit: issue seems use of \b in regex - according docs linked above matches braces. seemingly-working example is,
import re # test input myteststring = "somemess (a mass = 200 gev) , other mess (remove me if can)" replacewith = "hey there friend" # remove removelist = [r"\(a mass = 200 gev\)", r"\(remove me if can\)"] # build regex remove = r'(' + '|'.join(removelist) + r')' regex = re.compile(remove) # try it! out = regex.sub(replacewith, myteststring) # see if worked print(out)
Comments
Post a Comment