This article describes how to read and write unicode files in Python. it involves the reading and encoding operations related to Python and has some reference value, for more information about how to read and write unicode files in Python, see the following example. Share it with you for your reference. The specific implementation method is as follows:
# Coding = UTF-8 import OS import codecs def writefile (fn, v_ls): f = codecs. open (fn, 'wb', 'utf-8') for I in v_ls: f. write (I + OS. linesep) f. close () def readfile (fn): f = codecs. open (fn, 'R', 'utf-8') ls = [line. strip () for line in f] f. close () for I in ls: print I if _ name _ = '_ main _': fn = u'11.txt 'ls = [u'1. python ', U' 2. how to pythonic ', U' 3. python Cook', u'python programmer'] writefile (fn, ls) readfile (fn)
I hope this article will help you with Python programming.