import re with open('addons/education_lms/security/security.xml', 'r') as f: text = f.read() # Replace Course rule domain with (1, '=', 1) text = re.sub( r'.*?\[.*?\]', """ Student Course Access [(1, '=', 1)]""", text, flags=re.DOTALL ) with open('addons/education_lms/security/security.xml', 'w') as f: f.write(text)