import constants import os def remove_tricky_unicode(x): try: x = x.decode('utf-8') x = x.replace(u"\u2018", "'").replace(u"\u2019", "'") x = x.replace(u"\u201c", '"').replace(u"\u201d", '"') x = x.replace(u"\u2e3a", "-").replace(u"\u2014", "-") except: pass return x class file_writer: def __init__(self, filename): self.full_filename = os.path.join(constants.pages_dir, filename) self.f = open(self.full_filename, 'w') self.xforms = [ remove_tricky_unicode ] def add_xform(self, xform): self.xforms.append(xform) def write(self, data): for xform in self.xforms: data = xform(data) self.f.write(data.encode('utf-8')) def done(self): self.f.close() def close(self): self.done() # Test #def toupper(x): # return x.upper() # #fw = file_writer("test") #fw.add_xform(toupper) #fw.write(u"This is a \u201ctest\u201d. \n") #fw.done()