我想知道如何在 Python 2.7 中编码字典。当我执行以下操作时:
# coding=ISO-8859-1
Programmer = {'v': ["1","2x","3","4","5"]}
Programmer.update({'x': ["1","2x","3","4","5"]})
Programmer.update({'y': ["1","2x","3","4","5"]})
Programmer.update({'z': ["1","2x","3","4","5"]})
Programmer.update({'æ': ["1","2x","3","4","5"]})
Programmer.update({'ø': ["1","2x","3","4","5"]})
Programmer.update({'å': ["1","2x","3","4","5"]})
print Programmer
我得到:
{'v': ['1', '2x', '3', '4', '5'], '\xe5': ['1', '2x', '3', '4', '5'], '\xf8': ['1', '2x', '3', '4', '5'], '\xe6': ['1', '2x', '3', '4', '5'], 'y': ['1', '2x', '3', '4', '5'], 'x': ['1', '2x', '3', '4', '5'], 'z': ['1', '2x', '3', '4', '5']}
我想要的是:
{'v': ['1', '2x', '3', '4', '5'], 'å': ['1', '2x', '3', '4', '5'], 'ø': ['1', '2x', '3', '4', '5'], 'æ': ['1', '2x', '3', '4', '5'], 'y': ['1', '2x', '3', '4', '5'], 'x': ['1', '2x', '3', '4', '5'], 'z': ['1', '2x', '3', '4', '5']}
编辑:
这是我的工作代码:
# -*- coding: cp1252 -*-
from PyQt4 import QtCore, QtGui
import locale
global Programmer
Programmer = {u'v': ["1","2x","3","4","5"]}
Programmer.update({u'x': ["1","2x","3","4","5"]})
Programmer.update({u'y': ["1","2x","3","4","5"]})
Programmer.update({u'z': ["1","2x","3","4","5"]})
Programmer.update({u'æ': ["1","2x","3","4","5"]})
Programmer.update({u'ø': ["1","2x","3","4","5"]})
Programmer.update({u'å': ["1","2x","3","4","5"]})
class Dialog(QtGui.QDialog):
NumGridRows = 3
NumButtons = 3
def __init__(self):
super(Dialog, self).__init__()
styleComboBox = QtGui.QComboBox()
styleComboBox.addItem("UI Item01")
styleLabel = QtGui.QLabel("&UI Text:")
styleLabel.setBuddy(styleComboBox)
opgaveComboBox = QtGui.QComboBox()
opgaveComboBox.addItem(u"v")
opgaveComboBox.addItem(u"x")
opgaveComboBox.addItem(u"å")
opgaveLabel = QtGui.QLabel("&Opgave:")
opgaveLabel.setBuddy(opgaveComboBox)
self.programComboBox = QtGui.QComboBox()
self.programComboBox.addItem("UI item02")
programLabel = QtGui.QLabel("&Program:")
programLabel.setBuddy(self.programComboBox)
opgaveComboBox.activated[str].connect(self.changeOpgave)
topLayout = QtGui.QHBoxLayout()
topLayout.addWidget(styleLabel)
topLayout.addWidget(styleComboBox)
#topLayout.addStretch(1)
topLayoutB = QtGui.QHBoxLayout()
topLayoutB.addWidget(opgaveLabel)
topLayoutB.addWidget(opgaveComboBox)
#topLayoutB.addStretch(1)
topLayoutC = QtGui.QHBoxLayout()
topLayoutC.addWidget(programLabel)
topLayoutC.addWidget(self.programComboBox)
mainLayout = QtGui.QGridLayout()
mainLayout.addLayout(topLayout, 0, 0, 1, 2)
mainLayout.addLayout(topLayoutB, 1, 0, 1, 2)
mainLayout.addLayout(topLayoutC, 2, 0, 1, 2)
mainLayout.setRowStretch(1, 1)
mainLayout.setRowStretch(2, 1)
mainLayout.setColumnStretch(0, 1)
mainLayout.setColumnStretch(1, 1)
self.setLayout(mainLayout)
self.changeStyle('Cleanlooks')
self.setWindowTitle("temp test")
def changeOpgave(self, opgaveName):
self.programComboBox.clear()
print "begin 01"
print Programmer[opgaveName]
if not opgaveName in Programmer:
print "begin 02"
self.programComboBox.addItem("Andet")
else:
print "go"
for item in Programmer[opgaveName]:
self.programComboBox.addItem(item)
def changeStyle(self, styleName):
QtGui.QApplication.setStyle(QtGui.QStyleFactory.create(styleName))
self.changePalette()
def changePalette(self):
QtGui.QApplication.setPalette(QtGui.QApplication.style().standardPalette())
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
dialog = Dialog()
dialog.show()
sys.exit(dialog.exec_())
我明白了
KeyError: PyQt4.QtCore.QString(u'v')
KeyError: PyQt4.QtCore.QString(u'x')
KeyError: "PyQt4.QtCore.QString(u'\xe5')
尝试从“opgave”列表中进行选择时(第 79 行抛出错误)