I\'m having problems reading from a file, processing its string and saving to an UTF-8 File.
Here is the code:
try:
filehandle = open(filename,\"
Process text to and from Unicode at the I/O boundaries of your program using the codecs module:
import codecs
with codecs.open(filename, 'r', encoding='utf8') as f:
text = f.read()
# process Unicode text
with codecs.open(filename, 'w', encoding='utf8') as f:
f.write(text)
Edit: The io module is now recommended instead of codecs and is compatible with Python 3's open syntax, and if using Python 3, you can just use open if you don't require Python 2 compatibility.
import io
with io.open(filename, 'r', encoding='utf8') as f:
text = f.read()
# process Unicode text
with io.open(filename, 'w', encoding='utf8') as f:
f.write(text)