将QStringList(PyQt)转换为普通的python列表 [英] Converting a QStringList (PyQt) into a normal python list

查看:340
本文介绍了将QStringList(PyQt)转换为普通的python列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取qstringlist中的每个元素,并从列表中获取原始数据,而不是pyqt将其存储为原始数据.

I want to take each element within a qstringlist and get the raw data from the list not whatever pyqt is storing it as.

def find(self):
        self.foundFileList.setRowCount(0)

        fileName = self.inputFileName.currentText()
        path = self.directoryPath.currentText()
        maxSize = Decimal(self.maxFileSize.value())
        i = 0

        self.updateComboBox(self.inputFileName)
        self.updateComboBox(self.directoryPath)
        self.currentDir = QtCore.QDir(path)

        if not fileName:
            fileName = "*"

        allFiles = self.currentDir.entryList([fileName],
            QtCore.QDir.Files | QtCore.QDir.NoSymLinks, QtCore.QDir.Size)

        files = self.currentDir.entryList([fileName],
            QtCore.QDir.Files | QtCore.QDir.NoSymLinks, QtCore.QDir.Size)

        for fn in allFiles:
            file = QtCore.QFile(self.currentDir.absoluteFilePath(fn))
            size = Decimal((QtCore.QFileInfo(file).size() + 1023) / 1024)

            if size > maxSize:
                files.removeAt(i)

            i += 1

        self.showFiles(files)

def showFiles(self, files):
        ##Clean house before new round of files is displayed
        del nameList[0:len(nameList)]
        del fileList[0:len(fileList)]
        i = 0
        for fn in files:
            nameList.append(fn)
            file = QtCore.QFile(self.currentDir.absoluteFilePath(fn))
            fileList.append(file)
            size = QtCore.QFileInfo(file).size()
            ##Some other stuff below here but it's irrelevant

        print nameList
        print "_____________________________"
        print fileList

我从中得到的输出如下:

The output I get from this is as follows:

> [PyQt4.QtCore.QString(u'data - Copy (2).txt'),
> PyQt4.QtCore.QString(u'data - Copy (3).txt'),
> PyQt4.QtCore.QString(u'data - Copy (4).txt'),
> PyQt4.QtCore.QString(u'data - Copy (5).txt'),
> PyQt4.QtCore.QString(u'data - Copy (6).txt'),
> PyQt4.QtCore.QString(u'data - Copy.txt'),
> PyQt4.QtCore.QString(u'data.txt')]
> _____________________________ 
> [<PyQt4.QtCore.QFile object at 0x000000000B28C400>, 
> <PyQt4.QtCore.QFile object at
> 0x000000000B28C598>, <PyQt4.QtCore.QFile object at
> 0x000000000B28C730>, <PyQt4.QtCore.QFile object at
> 0x000000000B28C8C8>, <PyQt4.QtCore.QFile object at
> 0x000000000B28CA60>, <PyQt4.QtCore.QFile object at
> 0x000000000B28CBF8>, <PyQt4.QtCore.QFile object at
> 0x000000000B28CD90>]

如您所见,我只是获取QString值,而似乎是内存位置,我想将实际的字符串本身和实际的目录值作为字符串存储在python列表中.我这样做的主要原因是我已经编写了一个用于matplotlib和scipy的脚本,而我需要的只是这两个列表以使其工作.

As you can see I just get QString values and what appears to be memory locations I want the actual strings by themselves and the actual directory values as strings to store into a python list. The main reason for me doing this is that I have a script for matplotlib and scipy already written and all I need are these two lists to make it work.

推荐答案

怎么样:

print map(str, nameList)
print "_____________________________"
print [str(f.fileName()) for f in fileList]

第一个只是将每个QString转换为一个字符串.

The first one just converts each QString to a string.

第二个获取每个QFile的fileName()值

The second gets the fileName() value of each QFile

您还可以将地图写为列表理解:

You could also write the map as a list comprehension:

print [str(name) for name in nameList]

我还想在此处添加一些内容,以解决可能引起整体混乱的问题.这就是python对象打印的表示与对象的实际 value 之间的区别.

I also wanted to add something here to address an issue that may be causing the overall confusion. And that is the difference between the printed representation of a python object and the actual value of the object.

任何python对象都可以定义 __repr__() 方法返回一个字符串,以提供该对象的可视打印表示.当您打印这些对象时,它与调用print repr(myObject)相同.您打印一个包含QStrings的QStringList. QStrings打印的代表是向您显示模块路径,然后是随附的unicode值.该对象仍然是QString,并具有QString的所有方法.若要查看不同的打印值,必须将其转换为字符串对象.字符串的repr恰好是它自己的原始字符串值.

Any python object can define a __repr__() method which will return a string, to provide a visual printing representation of that object. When you print these objects out, it is the same as calling print repr(myObject). You print a QStringList which contains QStrings. The QStrings printed repr is to show you the module path, and then the enclosed unicode value. The object is still a QString and has all the method of a QString. To see a different printed value, you must convert it to, say, a string object. A string's repr happens to be simply its own raw string value.

我想提及这一点以回应您的评论,询问您是否应该删除每个元素中的PyQt4.QtCore.QString(u位,建议它们现在是带有垃圾数据的字符串对象.同样,多数民众赞成在QString的唯一代表,正在打印.

I wanted to mention this bit in response to your comment, asking if you should go in and delete the PyQt4.QtCore.QString(u bits from each element, suggesting that they were now a string object with junk data. Again, thats only the repr of the QString, being printed.

这篇关于将QStringList(PyQt)转换为普通的python列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆