def smart_truncate(line): line = line.lower() for pattern, replacement in trans: line = re.sub(pattern, replacement, line) pipe_parts = line.split('|') if len(pipe_parts) > 1: line = pipe_parts[0] + '|A' for x in line.split('/'): if x in avian: line = re.sub(x, 'avian', line) double_start = line.rfind(line[1:PREFIX_LENGTH], PREFIX_LENGTH) if double_start != -1: line = line[double_start:] return line.strip().title() + '\r\n'