How to do it...

  1. Open the example_utf8.txt file and display its content:
>>> with open('example_utf8.txt') as file:
... print(file.read())
...
20£
  1. 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
  1. 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£
  1. 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
  1. 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£
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.136.26.152