# -*- coding: utf-8 -*- """ Created on Tue May 24 15:49:30 2022 @author: Jation """ import pandas as pd import numpy as np import pymysql from HTMLTable import HTMLTable import argparse b = [] a={"1": {"name": "test", "college": "test", "major": "test", "paper": "test"}, "2": {"name": "test", "college": "test", "major": "test", "paper": "test"}, "3": {"name": "test", "college": "test", "major": "test", "paper": "test"}, "4": {"name": "test", "college": "test", "major": "test", "paper": "test"}, "5": {"name": "test", "college": "test", "major": "test", "paper": "test"}, "6": {"name": "test", "college": "test", "major": "test", "paper": "test"}, "type": "response"} for i in a: #print(a[i]) if(i == 'type'): continue d = [] for c in a[i].values(): d.append(c) b.append(d) # d.append(c) #b.append(d) #print(a) #print(b) def shu(a): table = HTMLTable(caption='输出结果') # 表头行 table.append_header_rows(( ('name', 'test', 'college', 'major'), )) # 合并单元格 data_1 = a # 数据行 table.append_data_rows(( data_1 )) # 标题样式 table.caption.set_style({ 'font-size': '30px', 'font-weight': 'bold', 'margin': '10px', }) # 表格样式,即标签样式 table.set_style({ 'border-collapse': 'collapse', 'word-break': 'normal', 'white-space': 'normal', 'font-size': '14px', }) # 统一设置所有单元格样式,
table.set_cell_style({ 'border-color': '#000', 'border-width': '1px', 'border-style': 'solid', 'padding': '5px', }) # 表头样式 table.set_header_row_style({ 'color': '#fff', 'background-color': '#48a6fb', 'font-size': '18px', }) # 覆盖表头单元格字体样式 table.set_header_cell_style({ 'padding': '15px', }) # 调小次表头字体大小 # 遍历数据行,如果增长量为负,标红背景颜色 html = table.to_html() f = open('C:/Users/Jation/Desktop/应用开发/dcs/ui/comment.html','w',encoding = 'utf-8-sig') f.write(html) # 1. 连接数据库, conn = pymysql.connect( host='10.129.16.173', user='root', password='427318Aa', db='test', charset='utf8', # autocommit=True, # 如果插入数据,, 是否自动提交? 和conn.commit()功能一致。 ) # ****python, 必须有一个游标对象, 用来给数据库发送sql语句, 并执行的. # 2. 创建游标对象, cur = conn.cursor() # 3. 对于数据库进行增删改查 parser = argparse.ArgumentParser('Automanager') parser.add_argument('--id', type = str, required = True) # 自动发微博 args = parser.parse_args() id = args.id # 4). **************************数据库查询***************************** sqli = "select name,college,major,paper from "+ id+"_crawl_result;" print(sqli) result = cur.execute(sqli) # 默认不返回查询结果集, 返回数据记录数。 print(result) '''print(cur.fetchone()) # 1). 获取下一个查询结果集; print(cur.fetchone()) print(cur.fetchone()) print(cur.fetchmany(4))''' # 2). 获取制定个数个查询结果集; info = cur.fetchall() # 3). 获取所有的查询结果 print(info) # 5). 移动游标指针 path = "C:/Users/Jation/Desktop/应用开发/dcs/ui/comment.csv" f = open(path,'w') f.truncate() shu(info) # 4. 关闭游标 cur.close() # 5. 关闭连接 conn.close()