Fix newlines when writing UTF-8 to Text file in python

character-encoding, python

Solution

If you use python 2, then use u"\n" to append newline, and encode internal unicode format to utf when you write it to file: `file_object.write((n+u"\n").encode("utf"))` Ensure `n` is of type `unicode` inside your loop.

Problem

I'm at my wits end on this one. I need to write some Chinese characters to a text file. The following method works however the newlines get stripped so the resulting file is just one super long string. I tried inserting every known unicode line break that I know of and nothing. Any help is greatly appreciated. Here is snippet: ``` import codecs file_object = codecs.open( 'textfile.txt', "w", "utf-8" ) xmlRaw = (data to be written to text file ) newxml = xmlRaw.split('\n') for n in newxml: file_object.write(n+(u'2424'))# where \u2424 is unicode line break ```

Original source

Related problems