Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to a .txt file (UTF-8), python

Tags:

python

save

I want to save the output (contents) to a file (saving it in UTF-8). The file shouldn't be overwritten, it should be saved as a new file - e.g. file2.txt So, I fists open a file.txt, encode it in UTF-8, do some stuff and then wanna save it to file2.txt in UTF-8. How do I do this?

import codecs
def openfile(filename):
    with codecs.open(filename, encoding="UTF-8") as F:
        contents = F.read()
        ...
like image 877
Gusto Avatar asked Nov 06 '10 11:11

Gusto


People also ask

How do you write to a text file in Python?

Step 1: The user has to open the text file for writing or appending by using the open() function. Step 2: The user can write in the text file by using the write() or writelines() function. Step 3: The user can close the text file by using the close() function.

Are .txt files UTF-8?

Most Microsoft Windows text files use "ANSI", "OEM", "Unicode" or "UTF-8" encoding.


1 Answers

The short way:

file('file2.txt','w').write( file('file.txt').read().encode('utf-8') )

The long way:

data = file('file.txt').read()
... process data ...
data = data.encode('utf-8')
file('file2.txt','w').write( data )

And using 'codecs' explicitly:

codecs.getwriter('utf-8')(file('/tmp/bla3','w')).write(data)
like image 186
adamk Avatar answered Sep 29 '22 11:09

adamk