August 2018
Intermediate to advanced
366 pages
10h 14m
English
The open function that Python provides accepts an encoding argument that can be used to properly encode/decode the contents of a file:
# Write a file with latin-1 encoding
with open('/tmp/somefile.txt', mode='w', encoding='latin-1') as f:
f.write('This is some latin1 text: "è già ora"')
# Read back file with latin-1 encoding.
with open('/tmp/somefile.txt', encoding='latin-1') as f:
txt = f.read()
print(txt)