character encoding - Fix newlines when writing UTF-8 to Text file in python -
i'm @ wits end on one. need write chinese characters text file. following method works newlines stripped resulting file 1 super long string.
i tried inserting every known unicode line break know of , nothing. appreciated. here snippet:
import codecs file_object = codecs.open( 'textfile.txt', "w", "utf-8" ) xmlraw = (data written text file ) newxml = xmlraw.split('\n') n in newxml: file_object.write(n+(u'2424'))# \u2424 unicode line break
if use python 2, use u"\n" append newline, , encode internal unicode format utf when write file: file_object.write((n+u"\n").encode("utf"))
ensure n
of type unicode
inside loop.
Comments
Post a Comment