from jmespath import search from pyparsing import replace_with def replace(input_str, search_key, replace_str, checkspaces=False): if replace_str is None: input_str = replace(input_str, search_key, "", checkspaces) else: while input_str.find(search_key) != -1: if checkspaces: if input_str[input_str.index(search_key) - 1] != " ": input_str = input_str.replace(search_key, " " + replace_str) else: input_str = input_str.replace(search_key, replace_str) else: input_str = input_str.replace(search, replace_str) return input_str