html表格内引用JavaScript的函数值。相应代码则为:
<script>
var x="这里是要显示的内容"
</script>
<table><tr><td><script>document.write(x)</script></td></tr></table>
现要实现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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)