Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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    
like image 201
Chris Hall Avatar asked Aug 09 '13 22:08

Chris Hall


1 Answers

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.

like image 125
rtxndr Avatar answered Oct 04 '22 19:10

rtxndr