问题
My problem is that when I get input with accent then the dictionary stores different keyname, it replace the accented character wit a character code. I'm new here I accept every help. Thank you for your help!
#!/usr/bin/python
# -*- coding: utf-8 -*-
products={}
try:
prodNum = int(raw_input(u"Hány terméket kíván felvenni a listába?\r\n"))
count = 0
while (count < prodNum):
prodName = raw_input(u"Kérem üsse be a %d. termék nevét!\r\n" %(count + 1))
encodedName = prodName.decode('utf8')
print(encodedName)
prodVal = int(raw_input(u"Kérem üsse be a %d. termék darabszámát!\r\n" %(count + 1)))
products[encodedName] = prodVal
count = count + 1
except ValueError:
print (u"Ide egy számot kellett volna írni. :)\r\n")
print(products)
Output:
Hány terméket kíván felvenni a listába?
1
Kérem üsse be a 1. termék nevét!
Qpa Kóla
Qpa Kóla
Kérem üsse be a 1. termék darabszámát!
2
{u'Qpa K\xf3la': 2}
回答1:
Printing a container prints the representation of the contents. There is no bug, merely misplaced expectations.
>>> print u'Qpa K\xf3la'
Qpa Kóla
>>> print repr(u'Qpa K\xf3la')
u'Qpa K\xf3la'
来源:https://stackoverflow.com/questions/26188558/dictionary-corrupt-the-name-of-the-key-python