I have this:

dic = {'ál':1, 'él':2}
string = "ÉL"
number=dic[string.lower()]

and it's giving me a KeyError.

I realize then that 'ÉL'.lower() is giving me 'Él', i.e. it is not affecting the É at all because some weird Python 2.x encoding issue. Can somebody help me please? I can't tell users to write always in lowercase.

Use u'à1' and u'é1' or even better

from __future__ import (absolute_import, division,
                        print_function, unicode_literals)

Currently, I even use

from __future__ import (absolute_import, division,
                        print_function, unicode_literals)
from future import standard_library
from future.builtins import *

in python 2.7, where future is this module.

Edited 2 Years Ago by Gribouillis

Yes, but you will write in python 3 in the future if you don't do it yet, so why not write (almost) compatible code right now ? There are good reasons for this

  • Python 3 is better than python 2 (more consistent and homogeneous)
  • It is not more difficult to write python 3 code
  • Your code may run out of the box the day you decide it is time to use python 3, so less work for tomorrow

I do, actually. I started with Python 3; it was my first language and my first contact with computer science and programming in general. The thing is that, in my current job, they use an NLP tolkit that relies on Python 2.x so...

So, by impoting all that I'll be able to get my Python 3 programs to run in Python 2.7+?

And thank you for your great reply!!

This question has already been answered. Start a new discussion instead.