鱼C论坛

 找回密码
 立即注册
查看: 1695|回复: 1

[已解决]将一个二维列表转换为一个html页面

[复制链接]
发表于 2020-12-26 00:32:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 tryhi 于 2020-12-26 01:32 编辑


                               
登录/注册后可看大图

  1. import os
  2. data=[['a','b','c','d'],
  3. ['1','2','3','4'],
  4. ['5','6','7','8'],
  5. ['9','10','11','12']]

  6. #  将一个二维表格转换成网页表格
  7. def tab_html(lst):
  8.     html_tab = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>网页表格</title></head><body><table border="1" cellpadding="10"><tbody>'
  9.     for i in lst:
  10.         html_tab = html_tab + '<tr>'
  11.         for j in i:
  12.             html_tab = html_tab + '<td>' + j + '</td>'
  13.         html_tab = html_tab + '</tr>'
  14.   
  15.     html_tab = html_tab + '</tbody></table></body></html>'
  16.     return html_tab

  17. html = tab_html(data)
  18. with open('aa.html', mode="w",encoding='utf-8') as msg:
  19.     msg.write(html)  # 在账单日前一天写入文件
  20. os.system('aa.html')
复制代码



写了这样一函数用来转换,但是发现不够用了,现在我又想增加一些字体颜色,还有一些表格内的文字想要增加连接进去,感觉不好使,又得重构,有没有什么现成的函数?

刚搜到一个pyecharts模块似乎可以满足需求,不知道有没有使用方法,打个比方,我想将这样一个列表,转换为带字体颜色、背景色以及带连接的html表格,要怎么写呢?

  1. data=[
  2.     [['a','0x0000FF','0x00FF00','http://www.baidu.com'],['b','0x0000FF','0x00FF00','http://www.baidu.com'],['c','0x0000FF','0x00FF00','http://www.baidu.com'],['d','0x0000FF','0x00FF00','http://www.baidu.com']],
  3.     [['1','0x0000FF','0x00FF00','http://www.baidu.com'],['2','0x0000FF','0x00FF00','http://www.baidu.com'],['3','0x0000FF','0x00FF00','http://www.baidu.com'],['4','0x0000FF','0x00FF00','http://www.baidu.com']],
  4.     [['5','0x0000FF','0x00FF00','http://www.baidu.com'],['6','0x0000FF','0x00FF00','http://www.baidu.com'],['7','0x0000FF','0x00FF00','http://www.baidu.com'],['8','0x0000FF','0x00FF00','http://www.baidu.com']],
  5.     [['9','0x0000FF','0x00FF00','http://www.baidu.com'],['10','0x0000FF','0x00FF00','http://www.baidu.com'],['11','0x0000FF','0x00FF00','http://www.baidu.com'],['12','0x0000FF','0x00FF00','http://www.baidu.com']],]
复制代码




突然发现好像不用这么麻烦,反正格式都是字符串,直接传对应的html格式进去就行了
最佳答案
2021-1-2 10:10:16
前端的东西最后由前端生成,
非要用python,那就用flask这种web构架,里面都自带了模板,传输参数就自动生成了。
如果不用模板,就只能手动。
我看你的列表里颜色和背景都是固定的,整体套用即可:

  1. import requests
  2. from lxml import etree


  3. def main():
  4.     data = [
  5.         [['a', '0x0000FF', '0x00FF00', 'http://www.baidu.com'], ['b', '0x0000FF', '0x00FF00', 'http://www.baidu.com'],
  6.          ['c', '0x0000FF', '0x00FF00', 'http://www.baidu.com'], ['d', '0x0000FF', '0x00FF00', 'http://www.baidu.com']],
  7.         [['1', '0x0000FF', '0x00FF00', 'http://www.baidu.com'], ['2', '0x0000FF', '0x00FF00', 'http://www.baidu.com'],
  8.          ['3', '0x0000FF', '0x00FF00', 'http://www.baidu.com'], ['4', '0x0000FF', '0x00FF00', 'http://www.baidu.com']],
  9.         [['5', '0x0000FF', '0x00FF00', 'http://www.baidu.com'], ['6', '0x0000FF', '0x00FF00', 'http://www.baidu.com'],
  10.          ['7', '0x0000FF', '0x00FF00', 'http://www.baidu.com'], ['8', '0x0000FF', '0x00FF00', 'http://www.baidu.com']],
  11.         [['9', '0x0000FF', '0x00FF00', 'http://www.baidu.com'], ['10', '0x0000FF', '0x00FF00', 'http://www.baidu.com'],
  12.          ['11', '0x0000FF', '0x00FF00', 'http://www.baidu.com'],
  13.          ['12', '0x0000FF', '0x00FF00', 'http://www.baidu.com']]]
  14.     print('<table style="color:#0000FF;background-color:#00FF00;">')
  15.     for row_item in data:
  16.         print('<tr>')
  17.         for d_item in row_item:
  18.             print(f'  <td>')
  19.             print(f'    {d_item[0]}')
  20.             print('  </td>')
  21.         print('</tr>')
  22.     print('</table>')


  23. if __name__ == '__main__':
  24.     main()
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-2 10:10:16 | 显示全部楼层    本楼为最佳答案   
前端的东西最后由前端生成,
非要用python,那就用flask这种web构架,里面都自带了模板,传输参数就自动生成了。
如果不用模板,就只能手动。
我看你的列表里颜色和背景都是固定的,整体套用即可:

  1. import requests
  2. from lxml import etree


  3. def main():
  4.     data = [
  5.         [['a', '0x0000FF', '0x00FF00', 'http://www.baidu.com'], ['b', '0x0000FF', '0x00FF00', 'http://www.baidu.com'],
  6.          ['c', '0x0000FF', '0x00FF00', 'http://www.baidu.com'], ['d', '0x0000FF', '0x00FF00', 'http://www.baidu.com']],
  7.         [['1', '0x0000FF', '0x00FF00', 'http://www.baidu.com'], ['2', '0x0000FF', '0x00FF00', 'http://www.baidu.com'],
  8.          ['3', '0x0000FF', '0x00FF00', 'http://www.baidu.com'], ['4', '0x0000FF', '0x00FF00', 'http://www.baidu.com']],
  9.         [['5', '0x0000FF', '0x00FF00', 'http://www.baidu.com'], ['6', '0x0000FF', '0x00FF00', 'http://www.baidu.com'],
  10.          ['7', '0x0000FF', '0x00FF00', 'http://www.baidu.com'], ['8', '0x0000FF', '0x00FF00', 'http://www.baidu.com']],
  11.         [['9', '0x0000FF', '0x00FF00', 'http://www.baidu.com'], ['10', '0x0000FF', '0x00FF00', 'http://www.baidu.com'],
  12.          ['11', '0x0000FF', '0x00FF00', 'http://www.baidu.com'],
  13.          ['12', '0x0000FF', '0x00FF00', 'http://www.baidu.com']]]
  14.     print('<table style="color:#0000FF;background-color:#00FF00;">')
  15.     for row_item in data:
  16.         print('<tr>')
  17.         for d_item in row_item:
  18.             print(f'  <td>')
  19.             print(f'    {d_item[0]}')
  20.             print('  </td>')
  21.         print('</tr>')
  22.     print('</table>')


  23. if __name__ == '__main__':
  24.     main()
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-29 13:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表