python怎样做html的表格
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python怎样做html的表格相关的知识,希望对你有一定的参考价值。
现要实现python制作html格式的表格,利用Python对字符串str.format()格式化操作进行处理,在日常对CVS格式文件处理过程当中,经常会将CVS格式文件进行转换,在正式场合是程序读取CVS文件进行转换并输出到html格式的文件当中,但现在只是实现一下转换的过程,需要输入以逗号分隔的数据。在设计程式的时候,需要先定义一下整个代码的框架,首先我们要定义一个主函数main(),虽然Python没有规定入口函数,一般在正式的开发中都设计了一个main()函数作为程序的入口函数,或许这是一种规范吧。然后我们在定义一个打印表头的方法print_head(),并在主函数里进行调用。再定义一个打印表尾的方法print_end(),也在主函数中进行调用。定义print_line()为打印表格行,定义extract_field()处理cvs行数据转换为list集合数据。最后再定义一个处理特殊符号的方法escape_html(),因为在html代码中为了避免与它的标签冲突,特要进行特殊符号的转换,如&-->&
还有就是对长度过长的数据要进行处理并用...代替
源代码:
#Author Tandaly
#Date 2013-04-09
#File Csv2html.py
#主函数
def main():
print_head()
maxWidth = 100
count = 0
while True:
try:
line = str(input())
if count == 0:
color = "lightgreen"
elif count%2 == 0:
color = "white"
else:
color = "lightyellow"
print_line(line, color, maxWidth)
count += 1
except EOFError:
break
print_end()
#打印表格头
def print_head():
print("")
#打印表行
def print_line(line, color, maxWidth):
tr = "".format(color)
tds = ""
if line is not None and len(line) > 0:
fields = axtract_fields(line)
for filed in fields:
td = "0".format(filed if (len(str(filed)) <= maxWidth) else
(str(filed)[:100] + "..."))
tds += td
tr += "0
".format(tds)
print(tr)
#打印表格尾
def print_end():
print("")
#抽取行值
def axtract_fields(line):
line = escape_html(line)
fields = []
field = ""
quote = None
for c in line:
if c in "\"":
if quote is None:
quote = c
elif quote == c:
quote = None
continue
if quote is not None:
field += c
continue
if c in ",":
fields.append(field)
field = ""
else:
field += c
if len(field) > 0:
fields.append(field)
return fields
#处理特殊符号
def escape_html(text):
text = text.replace("&", "&")
text = text.replace(">", ">")
text = text.replace("<", "<")
return text
#程序入口
if __name__ == "__main__":
main()
运行结果:
>>>
"nihao","wo"
nihaowo
"lsddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddsfdddddddddddddddddddddddssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssseeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooosdjfsldkfjsladjfsladjflasjdfljslkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkksssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss","tandaly"
lsdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd...tandaly
"ddddddddddddddddsllsflsdjflsdjfljasfjslfjleiwiwooooooooooosdlsdfkckslslllllwllllllllllllllllllllllllllllllllllllllllllllllllllllllgfdjslfjsdlkkkkkkkkkkksdfssssssssssssss",
34
ddddddddddddddddsllsflsdjflsdjfljasfjslfjleiwiwooooooooooosdlsdfkckslslllllwllllllllllllllllllllllll...34 参考技术A 基于文本文档(Markdown) 设想好需要的基本需要的表、字段、类型;
使用 Rails Migration 随着功能的开发逐步创建表;
随着细节功能的开发、需求,逐步增加字段,删除字段,或者调整字段类型;
第一个 Release 的时候清理 Migrations 合并成一个;
随着后期的改动,逐步增加、修改、删除字段或表。
基本上我的所有项目都是这么搞的,这和项目是否复杂无关。
所以我前面为什么说思路需要转变。本回答被提问者和网友采纳
以上是关于python怎样做html的表格的主要内容,如果未能解决你的问题,请参考以下文章