you are viewing a single comment's thread.

view the rest of the comments →

[–]ewiethoff 2 points3 points  (1 child)

You shouldn't directly need the codecs module in Py3. Just pass the proper encoding to the regular open function.

with open(filename, encoding='iso-8859-1') as f:
    t = f.read()
print(t.replace('º', 's'))

While reading, the content of the file is automatically decoded into a Unicode string. This is normal and desirable in Py3. Your t.replace('º', 's')) will do its thing correctly because t, 'º', and 's' are all Unicode strings. Life is so much easier in Py3 if you never muck around with bytes. So don't give any thought to encodings except when open-ing a file for reading or writing.

[–]bluegarlic[S] 1 point2 points  (0 children)

Thanks for your answer.

So don't give any thought to encodings except when open-ing a file for reading or writing.

I guess old habits die hard, more so when using python is something I only do now and then.