This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Python ISO-8859-1 encoding problem
Type: Stage: resolved
Components: Unicode Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Hugo.Silva, ezio.melotti, loewis
Priority: normal Keywords:

Created on 2011-11-13 08:26 by Hugo.Silva, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg147552 - (view) Author: Hugo Silva (Hugo.Silva) Date: 2011-11-13 08:26
Hi all,

I'm facing a huge encoding problem in Python when dealing with ISO-8859-1 / Latin-1 character set.

When using os.listdir to get the contents of a folder I'm getting the strings encoded in ISO-8859-1 (ex: ''Ol\xe1 Mundo''), however in the Python interpreter the same string is encoded to a different charset:

In : 'Olá Mundo'.decode('latin-1')
Out: u'Ol\xa0 Mundo'

How can I force Python to decode the string to the same format. I've seen that os.listdir is returning the strings correctly encoded but the interpreter is not ('á' character corresponds to '\xe1' in ISO-8859-1, not to '\xa0'):

http://en.wikipedia.org/wiki/ISO/IEC_8859-1

This is happening 

Any thoughts on how to overcome ?

Regards,
msg147553 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2011-11-13 08:32
This doesn't seem a bug to me, so you should ask for help somewhere else.
You can try to pass a unicode arg to listdir to get unicode back, and double check what the input actually is.
msg147554 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2011-11-13 09:33
Apparently, you are using the interactive shell on Microsoft Windows. This will use the "OEM code page"; which one that is depends on the exact Windows regional version you are using.

You shouldn't decode the string with 'latin-1', but with sys.stdin.encoding. Alternatively, you should use Unicode string literals in Python in the first place.

In any case, Ezio is right: this is not a help forum, but a bug tracker.
History
Date User Action Args
2022-04-11 14:57:23adminsetgithub: 57604
2011-11-13 09:33:28loewissetnosy: + loewis
messages: + msg147554
2011-11-13 08:32:19ezio.melottisetstatus: open -> closed
resolution: not a bug
messages: + msg147553

stage: resolved
2011-11-13 08:26:22Hugo.Silvacreate