承载界面:
# coding=utf-8
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import class_datafile QTextCodec.setCodecForCStrings(QTextCodec.codecForName('utf8')) class Form(QMainWindow):
def __init__(self,parent=None):
super(Form,self).__init__(parent)
self.setWindowTitle('DataAnylaze')
self.setWindowIcon(QIcon('image/3.png'))
self.setBaseSize(500,500)
#self.resize(1000,1000)
self.setMinimumSize(600,600)
self.showMaximized() self.table=QTableWidget()
self.setCentralWidget(self.table) self.createAction()
self.createMenu()
self.createToolBar() def createAction(self):
self.openFileAct=QAction(QIcon('image/open.png'),u'打开',self)
self.connect(self.openFileAct,SIGNAL('triggered()'),self.openFile) self.saveFileAct=QAction(QIcon('image/save.png'),u'保存',self)
self.connect(self.saveFileAct,SIGNAL('triggered()'),self.saveFile) self.saveAnotherFileAct=QAction(QIcon('image/saveanother.png'),u'另存为',self)
self.connect(self.saveAnotherFileAct,SIGNAL('triggered()'),self.saveAnotherFile) self.setRightAct=QAction(QIcon('image/r.png'),u'右对齐',self)
self.connect(self.setRightAct,SIGNAL('triggered()'),self.setTableRAlignment) self.setLeftAct=QAction(QIcon('image/l.png'),u'左对齐',self)
self.connect(self.setLeftAct,SIGNAL('triggered()'),self.setTableLAlignment) self.setMiddleAct=QAction(QIcon('image/m.png'),u'居中对齐',self)
self.connect(self.setMiddleAct,SIGNAL('triggered()'),self.setTableMAlignment) def createMenu(self):
fileMenu=self.menuBar().addMenu(u'文件')
fileMenu.addAction(self.openFileAct)
fileMenu.addAction(self.saveFileAct)
fileMenu.addAction(self.saveAnotherFileAct) def createToolBar(self):
fileToolBar=self.addToolBar(u'文件')
fileToolBar.addAction(self.openFileAct)
fileToolBar.addAction(self.saveFileAct)
fileToolBar.addAction(self.saveAnotherFileAct) aliToolBar=self.addToolBar(u'对齐')
aliToolBar.addAction(self.setLeftAct)
aliToolBar.addAction(self.setRightAct)
aliToolBar.addAction(self.setMiddleAct) def openFile(self):
file=QFileDialog.getOpenFileName() dataFile=class_datafile.dataFileClass()
dataFile.setFileDir(file) self.data,self.columnName=dataFile.getAllData() #设置表格行列数量
rowNum=dataFile.getRowLen()
colNum=dataFile.getColumnLen()
self.table.setColumnCount(colNum)
self.table.setRowCount(rowNum) #设置表头
for i in range(len(self.columnName)):
self.table.setHorizontalHeaderItem(i,QTableWidgetItem(self.columnName[i])) #设置表格内容
for j in range(len(self.columnName)):
for k in range(rowNum):
self.table.setItem(k,j,QTableWidgetItem(self.data[self.columnName[j]][k])) mes=QMessageBox()
mes.information(self,u'测试读取数据',u'读了%d行数据' % (rowNum+1),QMessageBox.Ok) def saveFile(self):
pass def saveAnotherFile(self):
pass def setTableRAlignment(self):
for j in range(self.table.columnCount()):
for i in range(self.table.rowCount()):
self.table.item(i,j).setTextAlignment(Qt.AlignRight|Qt.AlignVCenter) def setTableMAlignment(self):
for j in range(self.table.columnCount()):
for i in range(self.table.rowCount()):
self.table.item(i,j).setTextAlignment(Qt.AlignHCenter|Qt.AlignVCenter) def setTableLAlignment(self):
for j in range(self.table.columnCount()):
for i in range(self.table.rowCount()):
self.table.item(i,j).setTextAlignment(Qt.AlignLeft|Qt.AlignVCenter) app=QApplication(sys.argv)
form=Form()
form.show()
app.exec_()
---------------------------------
数据解析:
# coding=utf-8
import sys
import copy
from PyQt4.QtCore import *
from PyQt4.QtGui import * #QTextCodec.setCodecForTr(QTextCodec.codecForName('utf8')) class dataFileClass():
def __init__(self,parent=None):
self.runflag=0
self.data={}
self.columnName=[] def readData(self):
if self.runflag==1:
fileData=open(str(self.file),'r')
flagcol=0
self.rownum=0
for line in fileData:
linelist=line.split(',')
if flagcol==0:
self.columnName=copy.copy(linelist)
for name in self.columnName:
self.data[name]=[]
flagcol+=1
else:
for i in range(len(linelist)):
self.data[self.columnName[i]].append(linelist[i])
self.rownum+=1 fileData.close() def setFileDir(self,dir):
self.file=dir
self.runflag=1
self.readData() def getColumnNames(self):
return self.data.keys() def getColumnLen(self):
return len(self.columnName) def getRowLen(self):
return self.rownum def getValue(self,columnName,row):
if self.data.has_key(columnName):
return self.data[columnName][row] def getAllData(self):
return self.data,self.columnName