【发布时间】:2015-12-03 00:53:54
【问题描述】:
我正在创建一个函数,它使用字典替换文本文件中的单词,即如果在文件中遇到第一个单词对,它将被第二个替换。此外,当用户使用 GUI 中的小部件时调用创建的函数。所以创建的函数都存在于一个 ui 类中。 (希望这是有道理的)。我正在使用python
字典看起来像这样(dictlist = {'test':'rest', 'happy':'sad', 'big':'small'})。直接在 python 编辑器中编写时,该函数可以正常工作,但是当我将其复制到翻译后的 .ui 代码时,出现以下错误。任何人都可以就为什么会发生这种情况提供建议。我是新手,所以希望我的解释是有道理的。
line = rpairs(line, dictlist)
line 1486, in rpairs
temp = temp.replace(key, dictlist[key])
TypeError: QString.__getitem__(): arguments did not match any overloaded call:
overload 1: argument 1 has unexpected type 'QString'
overload 2: argument 1 has unexpected type 'QString'
def temp(self):
import string
global filename
global mytemp
dictlist = self.lineEdit_dict.text()
filename2 = self.lineEdit_outputfilename.text()
f1 = open(filename, 'r')
f2 = open('filename2', 'w')
def rpairs (temp, dictlist):
for key in dictlist:
temp = temp.replace(key, dictlist[key])
return temp
for line in f1:
line = rpairs(line, dictlist)
f2.write(line)
f2.close()
return mytemp
【问题讨论】:
-
您是否试图通过阅读
self.lineEdit_dict.text()将dictlist构造为字典?...或者您的程序代码中已经有了它?
标签: python user-interface qstring