我有一个使用Qt设计器的py文件,我有一个组合框,我从csv文件中读取。如果组合框选项在文件中,则打印一些内容。
在脚本的顶部,它说:# -*- coding: utf-8 -*-
PyQt4.QtCore.QString(u'choice') is not in list
"选择"当然在列表中。我相信这是一个编码问题,但我只知道这些。
u'choice'是字符串,列表中包含字符串。
这是我如何添加项目到列表:
import csv
list1=csv.reader(open('file.csv', "rb"))
list2=[]
for i in list1:
list2.append(i)
任何想法?谢谢。
这与编码无关。
出现错误仅仅是因为从csv文件中读取的列表中没有字符串:
>>> import csv
>>> with open('tmp.csv', 'wb') as stream:
... csv.writer(stream).writerow(['choice'])
...
>>> lst = []
>>> with open('tmp.csv', 'rb') as stream:
... for row in csv.reader(stream):
... lst.append(row)
...
>>> from PyQt4.QtCore import QString
>>> s = QString(u'choice')
>>> lst.index(s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: PyQt4.QtCore.QString(u'choice') is not in list
>>> lst
[['choice']]
>>> lst[0].index(s)
0
csv阅读器为文件中的每一行返回字符串的列表。