❶ python運行出來的結果怎麼顯示到html上
我大概看了你所提問的內容,你的意思應該是想把從資料庫里所讀取到的內容,保存為html格式文件,方便查看。是這樣吧?
這里我簡單寫了這樣的代碼,代碼的思路是:
創建一個html後綴的文件,然後利用文件操作的相關知識寫入文件,完成後,可以直接打開file.html查看。
和你的思路能對上,你可以讀取資料庫內容,然後格式成html規范,寫入file.html文件。完成後,就可以打開file.html查看你的結果。
以下是我寫的代碼,你參考下:(代碼相對來說比較簡單,這只是給你一個思路,具體你可以依據這個方向進行修改完善)
python3.6環境
#該代碼運行於至少python3.6支持
#功能:把內容保存為html格式文件
withopen('file.html','w')asfile:#以w的模式打開file.html文件,不存在就新建
file.write('<html><body><tableborder=1><tr><th>a列表</th><th>b列表</th></tr><indent>輸出結果:')#使用write寫入字元串內容到file.html
foriinrange(10):#執行一個遍歷操作
a=i#i依次賦值給a,i內容為0,1,2,3,4,5,6,7,8,9
b=i**2#把i的值依次進行i的2次冪操作
file.write("<tr><td>"f'{a}'"</td><td>"f'{b}'"</td></tr>")#使用write寫入字元串內容到file.html
file.write('</indent></table></body></html>')#使用write寫入字元串內容到file.html
python3環境
#該代碼運行於python3
#功能:把內容保存為html格式文件
withopen('file.html','w')asfile:#以w的模式打開file.html文件,不存在就新建
file.write('<html><body><tableborder=1><tr><th>a列表</th><th>b列表</th></tr><indent>輸出結果:')#使用write寫入字元串內容到file.html
foriinrange(10):#執行一個遍歷操作
a=i#i依次賦值給a,i內容為0,1,2,3,4,5,6,7,8,9
b=i**2#把i的值依次進行i的2次冪操作
file.write('<tr><td>{}'.format(a)+'</td><td>{}'.format(b)+'</td></tr>')#使用write寫入字元串內容到file.html
file.write('</indent></table></body></html>')#使用write寫入字元串內容到file.html
純手工,如果對你有幫助望採納!
❷ 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
"sss","tandaly"
...tandaly
"lkkkkkkkkkkksdfssssssssssssss",
34
...34
❸ 怎樣用python腳本生成一個html格式的測試報告
比如很簡單的,可以這樣:
# -*- coding:utf-8 -*-
import os,sys
html = open('index.html', 'w')
html.write("""
<html>
<head>
<title>Test</title>
<style>img{float:left;margin:5px;}</style>
</head>
<body>
""")
files = os.listdir('.')
# 首先處理文本
for f in files:
if f.lower().endswith('.txt'):
fp = open(f)
content = fp.read()
fp.close()
html.write("<p>%s</p>" % content)
# 然後處理圖片
for f in files:
if f.lower().endswith('.jpg') or f.lower().endswith('.png'):
html.write("<img src='%s' />" % f)
html.write('</body></html>')
html.close()
把這個python代碼放在有圖片和txt文本的目錄里,運行就可以了。如果不是jpg,修改增加png,gif就行了。