#NEWLINE# def tidy_xml( filename ) : #NEWLINE# #INDENT# '\n    read in file, screen out unsafe unicode characters, write back file in utf-8\n\n    :param filename: str\n    :returns: False if unable to read from file\n    ' #NEWLINE# if ( not os . path . isfile ( filename ) ) : #NEWLINE# #INDENT# raise ValueError ( 'file does not exist' ) #NEWLINE# #UNINDENT# data = None #NEWLINE# for ftype in [ 'utf-8' , 'iso8859-1' ] : #NEWLINE# #INDENT# fhand = None #NEWLINE# try : #NEWLINE# #INDENT# fhand = codecs . open ( filename , 'r' , ftype ) #NEWLINE# data = fhand . read ( ) #NEWLINE# break #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# if ( fhand is not None ) : #NEWLINE# #INDENT# fhand . close ( ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( data is None ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# for match in _SAFE_XML_REGEX . finditer ( data ) : #NEWLINE# #INDENT# data = ( ( data [ : match . start ( ) ] + '?' ) + data [ match . end ( ) : ] ) #NEWLINE# #UNINDENT# with open ( fhand , 'w' ) as fhand : #NEWLINE# #INDENT# fhand . write ( data ) #NEWLINE# #UNINDENT# return True