Open the example_utf8.txt file and display its content:
>>> with open('example_utf8.txt') as file: ... print(file.read()) ... 20£
Try to open the example_iso.txt file, which will raise an exception:
>>> with open('example_iso.txt') as file: ... print(file.read()) ... Traceback (most recent call last): ... UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa3 in position 2: invalid start byte
Open the example_iso.txt file with the proper encoding:
>>> with open('example_iso.txt', encoding='iso-8859-1') as file: ... print(file.read()) ... 20£
Open the utf8 file and save its content in an iso-8859-1 file:
>>> with open('example_utf8.txt') as file: ... content = file.read() >>> with open('example_output_iso.txt', 'w', encoding='iso-8859-1') as file: ... file.write(content) ... 4
Finally, read from the new file in the proper format to ensure it is correctly saved:
>>> with open('example_output_iso.txt', encoding='iso-8859-1') as file: ... print(file.read()) ... 20£