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()
...
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.
Most Microsoft Windows text files use "ANSI", "OEM", "Unicode" or "UTF-8" encoding.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With