PyQt:我如何知道使用了哪种编码?

2024-04-25 20:48:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我用一个csv文件从一个csv文件中读到文件。如果组合框选项在文件中,它会打印一些内容。在

在脚本的顶部写着:# -*- coding: utf-8 -*-

所以,我得到的错误是:

PyQt4.QtCore.QString(u'choice') is not in list

“选择”当然在列表中。我相信这是一个编码问题,但我只知道这些。在

u'choice'是一个字符串,列表包含字符串。在

以下是我向列表中添加项目的方法:

^{pr2}$

有什么想法吗?谢谢。在


Tags: 文件csv字符串脚本内容列表is选项
1条回答
网友
1楼 · 发布于 2024-04-25 20:48:15

这与编码无关。在

发生错误的原因很简单,因为从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读取器为文件中的每一行返回字符串列表。在

相关问题 更多 >