def clean_txt(txt):
 if isbytestring(txt):
  txt = txt.decode('utf-8', 'replace')
 txt = '\n'.join([line.rstrip() for line in txt.splitlines()])
 txt = re.sub('(?m)(?<=^)([ ]{2,}| DCTB +)(?=.)', ('&nbsp;' * 4), txt)
 txt = re.sub('[ ]{2,}', ' ', txt)
 txt = re.sub('^\\s+(?=.)', '', txt)
 txt = re.sub('(?<=.)\\s+$', '', txt)
 txt = re.sub('\n{5,}', '\n\n\n\n', txt)
 txt = clean_ascii_chars(txt)
 return txt

def clean_txt ( txt ) : if isbytestring ( txt ) : txt = txt . decode ( 'utf-8' , 'replace' ) txt = '\n' . join ( [ line . rstrip ( ) for line in txt . splitlines ( ) ] ) txt = re . sub ( '(?m)(?<=^)([ ]{2,}| DCTB +)(?=.)' , ( '&nbsp;' * 4 ) , txt ) txt = re . sub ( '[ ]{2,}' , ' ' , txt ) txt = re . sub ( '^\\s+(?=.)' , '' , txt ) txt = re . sub ( '(?<=.)\\s+$' , '' , txt ) txt = re . sub ( '\n{5,}' , '\n\n\n\n' , txt ) txt = clean_ascii_chars ( txt ) return txt
run transformations on the text to put it into consistent state .