将QStringList (PyQt)转换为正常的python列表。

时间:2021-03-15 23:01:48

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

我想把每个元素都放在一个qstringlist中,从列表中获取原始数据,而不是任何pyqt存储的数据。

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>]

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.

正如您所看到的,我只获得了QString值,以及什么是内存位置,我希望实际的字符串本身和实际的目录值作为字符串存储到python列表中。我这样做的主要原因是,我已经编写了matplotlib和scipy的脚本,我所需要的就是这两个列表来实现它。

2 个解决方案

#1


7  

How about:

如何:

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

The first one just converts each QString to a string.

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

The second gets the fileName() value of each QFile

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

You could also write the map as a list comprehension:

你也可以把这张地图作为一个列表理解:

print [str(name) for name in nameList]

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对象的打印表示和对象的实际值之间的区别。

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.

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

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.

我想在回应你的评论时提及这一点,询问你是否应该删除PyQt4.QtCore。QString(每个元素的u位,表明它们现在是一个带有垃圾数据的字符串对象)。同样,这只是QString的repr,被打印出来。

#2


2  

It may help:

它可以帮助:

pythonList = str(qStringList.join("<join>")).split("<join>")

#1


7  

How about:

如何:

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

The first one just converts each QString to a string.

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

The second gets the fileName() value of each QFile

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

You could also write the map as a list comprehension:

你也可以把这张地图作为一个列表理解:

print [str(name) for name in nameList]

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对象的打印表示和对象的实际值之间的区别。

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.

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

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.

我想在回应你的评论时提及这一点,询问你是否应该删除PyQt4.QtCore。QString(每个元素的u位,表明它们现在是一个带有垃圾数据的字符串对象)。同样,这只是QString的repr,被打印出来。

#2


2  

It may help:

它可以帮助:

pythonList = str(qStringList.join("<join>")).split("<join>")