diferent answers with isalpha()

nuno at salgado.ws nuno at salgado.ws
Fri Jul 13 06:38:26 EDT 2007


On Jul 13, 6:07 am, Jyotirmoy Bhattacharya <jmoy.mate... at gmail.com>
wrote:
> On Jul 13, 5:05 am, n... at salgado.ws wrote:
>
> > In Idle when I do print 'á'.isalpha() I get True. When I make and
> > execute a script file with the same code I get False.
>
> > Why do I have diferent answers ?
>
> Non-ASCII characters in ordinary (8-bit) strings have all kinds of
> strangeness. First, the answer of isalpha() and friends depends on the
> current locale. By default, Python uses the "C" locale where the
> alphabetic characters are a-zA-z only. To set the locale to whatever
> is the OS setting for the current user, put this near the beginning of
> your script:
>
> import locale
> locale.setlocale(locale.LC_ALL,'')
>
> Apparently IDLE does this for you. Hence the discrepancy you noted.
>
> Second, there is the matter of encoding. String literals like the one
> you used in your example are stored in whatever encoding your text
> editor chose to store your program in. If it doesn't match the
> encoding using by the current locale, once again the program fails.
>
> As I see it, the only way to properly handle characters outside the
> ASCII set is to use Unicode strings.

Jyotirmoy,

You are right. Thank you for your information.

I will follow your advice but it gets me into another problem with
string.maketrans/translate that I can't solve.




More information about the Python-list mailing list