pyqt5 读取数据两种方式

第一种借助tableWidget进行显示

主要记录自己做项目的过程

数据为新闻数据

def search_article(self):
‘’‘
此段为根据搜索框内容进行数据筛选
‘’‘

    self.year = self.ui.comboBox_year.currentText()
    self.Pub = self.ui.comboBox_pub.currentText()
    if self.year == 'ALL':
        if self.Pub == 'ALL':
            self.data_new = self.orignal[['NewsSource', 'Year', 'Title', 'Article']]
        else:
            self.data_new = self.orignal.loc[(self.orignal["NewsSource"] == self.Pub),['NewsSource', 'Year', 'Title', 'Article']]
    else:
        if self.Pub == 'ALL':
            self.data_new = self.orignal.loc[(self.orignal["Year"] == int(self.year)),['NewsSource', 'Year', 'Title', 'Article']]
        else:
            self.data_new = self.orignal.loc[(self.orignal["Year"] == int(self.year)) & (self.orignal["NewsSource"] == self.Pub),['NewsSource', 'Year', 'Title', 'Article']]

‘’‘
关键代码如下
‘’‘

    data_rows = self.data_new.shape[0]  # 获取表格行数
    data_colunms = self.data_new.shape[1]  # 获取表格列数
    data_header = self.data_new.columns.values.tolist()  # 获取表头

    self.ui.tableWidget.setColumnCount(data_colunms)
    self.ui.tableWidget.setRowCount(data_rows)
    # 设置行列表头
    self.ui.tableWidget.setHorizontalHeaderLabels(data_header)
    self.ui.tableWidget.horizontalHeader().resizeSection(1, 50)
    self.ui.tableWidget.horizontalHeader().resizeSection(3,330)
    self.ui.tableWidget.verticalHeader().setDefaultSectionSize(300)
    self.ui.tableWidget.verticalHeader().setFixedWidth(20)
    self.ui.tableWidget.setStyleSheet("color: rgb(0,0,0);")

    for i in range(data_rows):  # 行循环
        data_rows_values = self.data_new.iloc[[i]]  # 读入一行数据
        data_rows_values_array = np.array(data_rows_values)  # 将该行数据放入数组中
        data_rows_values_list = data_rows_values_array.tolist()[0]  # 将该数组转换为列表
        for j in range(data_colunms):  # 列循环
            data_items_list = data_rows_values_list[j]  # 行列表中的每个元素放入列列表中
            data_items = str(data_items_list)  # 该数据转换成字符串
            newItem = QTableWidgetItem(data_items)  # 该字符串类型的数据新建为tablewidget元素
            newItem.setTextAlignment(Qt.AlignLeft | Qt.AlignVCenter)  # 显示为左居中、垂直居中
            newItem.setBackground(QColor(255,218,185))
            self.ui.tableWidget.setItem(i, j, newItem)  # 在表格第i行第j列显示newItem元素

 

第二种方法,使用ploty,以html网页形式展示

import plotly.graph_objects as go
import pandas as pd

df = pd.read_csv('。。。',encoding='utf8')#origanl data

fig = go.Figure(data=[go.Table(
    columnorder = [1,2,3,4],
    columnwidth = [60,50,90,400],
    header=dict(values=list(df.columns[2:]),
                fill_color='paleturquoise',
                align=['left', 'center']),
    cells=dict(values=[df.NewsSource,df.Year, df.Title, df.Article],
               fill_color='lavender',
               align=['left', 'center'],
               font_size=12,
               height=30))
])

fig.show()

 

你可能感兴趣的:(qt,python,ui)