2

我想知道如何在 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 行抛出错误)

4

2 回答 2

1

组合框回调被传递给一个PyQt4.QtCore.QString()对象,而不是一个 unicode 字符串。另一方面,您的字典应该使用unicode()键(这是正确的做法)。

首先将QString()对象转换为 unicode 值:

def changeOpgave(self, opgaveName):
    opgaveName = unicode(opgaveName)

这是有效的,因为该QString()对象已经保存了 unicode 数据,并为 Python 提供了正确的钩子来进行这种转换,而无需使用显式编解码器重新编码数据。

一旦该opgaveName值是一个 unicode 键,您就可以使用它来将它与您的字典进行比较。

请注意,您可以只定义字典而不.update()调用:

Programmer = {
    u'v': ["1","2x","3","4","5"],
    u'x': ["1","2x","3","4","5"],
    u'y': ["1","2x","3","4","5"],
    u'z': ["1","2x","3","4","5"],
    u'æ': ["1","2x","3","4","5"],
    u'ø': ["1","2x","3","4","5"],
    u'å': ["1","2x","3","4","5"],
}

global关键字在您使用它的地方没有任何意义;该关键字仅在函数内部有意义,在这里是多余的。

于 2013-05-08T11:34:41.723 回答
0

可能是这样的:

print "{",
for key, value in Programmer.items():
    print "'%s'" % key, ':', value, ',',
print "}"

当您打印时,这将打印str(Programmers),但在 dict 内部使用repr(key)and repr(value)。内容不会改变,只是打印方式不同。

于 2013-05-08T09:40:46.333 回答