Converting a QStringList (PyQt) into a normal python list
سؤال
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>]
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.
المحلول
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.
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]
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.
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.
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.
نصائح أخرى
It may help:
pythonList = str(qStringList.join("<join>")).split("<join>")