Python html脚本csv到html表
我对html一无所知。我是如何获得将csv转换为html的代码的 代码如下:Python html脚本csv到html表,python,html,python-2.7,csv,Python,Html,Python 2.7,Csv,我对html一无所知。我是如何获得将csv转换为html的代码的 代码如下: import sys import csv # Open the CSV file for reading def populate_table(csv_fl): reader = csv.reader(open(csv_fl)) # Create the HTML file for output html_table = '' # initialize rownum variab
import sys
import csv
# Open the CSV file for reading
def populate_table(csv_fl):
reader = csv.reader(open(csv_fl))
# Create the HTML file for output
html_table = ''
# initialize rownum variable
rownum = 0
# write <table> tag
html_table= '<table>\n'
# generate table contents
for row in reader: # Read a single row from the CSV file
# write header row. assumes first row in csv contains header
if rownum == 0:
html_table += '<tr>\n' # write <tr> tag
for column in row:
html_table += '<th>' + column + '</th>\n'
html_table += '</tr>\n'
#write all other rows
else:
html_table += '<tr>\n'
for column in row:
if 'fail' in column or 'Fail' in column:
html_table += "<td style='color:red'>" + column + '</td>\n'
continue
html_table += '<td>' + column + '</td>\n'
html_table += '</tr>\n'
#increment row count
rownum += 1
# write </table> tag
html_table += '</table>\n'
return html_table
导入系统
导入csv
#打开CSV文件进行读取
def填充表格(csv表格):
读卡器=csv。读卡器(打开(csv_fl))
#创建用于输出的HTML文件
html_表=“”
#初始化rownum变量
rownum=0
#写标签
html_表='\n'
#生成表内容
对于读卡器中的行:#从CSV文件中读取一行
#写入标题行。假设csv中的第一行包含标题
如果rownum==0:
html_table+='\n'#写标记
对于行中的列:
html_table+=''+column+'\n'
html_表+='\n'
#写入所有其他行
其他:
html_表+='\n'
对于行中的列:
如果第列为“失败”或第列为“失败”:
html_table+=“”+column+'\n
持续
html_table+=''+column+'\n'
html_表+='\n'
#增量行计数
rownum+=1
#写标签
html_表+='\n'
返回html\u表
上面的代码如果字符串包含Fail或Fail,它将生成红色单元格
我需要帮助在这里使红色(不是单细胞)的完整行
下面是填充html的代码(缩进错误。如果需要正确的缩进代码,我将在链接中共享)
我将执行以下代码,如下所示:
python2.7 fil.py test.csv test.html
import csv2html
import sys
class Sketch:
def __init__(self):
"""
Returns html sketch for a defined scenario
Scenarios asccessible as functions.
supported ones are:
-fail
-pass
-status_update
-final
"""
def _style (self):
body = """
<style>
p {
font-family : Calibri;
font-size: 14px;
font-weight: bolder;
text-align : left;
}
p.fade {
color : #CCCCCC;
font-size: 14px;
}
em {
font-style : italic ;
font-size : 16px;
font-weight: lighter ;
}
em.pass {
font-style : italic ;
font-size : 16px;
color: green ;
}
em.fail {
font-style : italic ;
font-size : 16px;
color: red ;
}
a {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
hr {
align: left ;
margin-left: 0px ;
width: 500px;
height:1px;
}
table {
border-collapse: collapse;
}
tr {
padding: 4px;
text-align: center;
border-right:2px solid #FFFFFF;
}
tr:nth-child(even){background-color: #f2f2f2}
th {
background-color: #cceeff;
color: black;
padding: 4px;
border-right:2px solid #FFFFFF;
}
</style>
"""
return body
def _start(self):
return """
<!DOCTYPE html>
<html>
"""
def _end(self):
body ="""
<hr/>
<p class="fade">Note: Link might be disabled,
please put me in safe sender list, by right
click on message.
This is a system generated mail, please don't
respond to it.</p>
</html>
"""
return body
def _fail (self):
body = """
<p>STATUS :
<em class="fail">failed</em>
</p>
"""
return body
def _critical_fail(self):
str_ = 'Failure is critical, terminating the run.'
body = """
<p>
<em class="fail">%s</em>
</p>
"""%str_
return body
def _pass (self):
body = """
<p>STATUS :
<em class="pass">passed</em>
</p>
"""
return body
def _type (self, title, val):
body = """
<p>%s :
<em>%s</em>
</p>
"""%(title.upper(), val)
return body
def _loglink(self, logs):
body = """ <p> LOGS :</p>
<a href=%s>%s</a>
"""%(logs,logs)
return body
def render (self, test_id, descr, platform=None, pass_=True, \
logs=None, critical=False):
body = self._start() +\
self._style() + \
self._type("test id", test_id) + \
self._type("description", descr) +\
self._type("platform", platform)
if pass_==True:
body += self._pass ()
else:
body += self._fail ()
if critical:
body += self._critical_fail()
body += self._loglink(logs)
body += self._end()
return body
def status_update (self, ):
pass
def final (self, logs):
body += self._end()
return body
def add_html_header (csv_fl, fname):
""" html data returned by sqlite needs to be enclosed in
some of the mandatory tags for the web to parse it
properly. ! """
sketch =Sketch()
content ="""
%s %s
<body>
%s
</body>
</html>
"""%(sketch._start(), sketch._style(), csv2html.populate_table(csv_fl))
open (fname, 'w').write (content)
if len(sys.argv) < 3:
print "Usage: csvToTable.py csv_file html_file"
exit(1)
csv_fl = sys.argv[1]
html_fl = sys.argv[2]
add_html_header(csv_fl, html_fl)
python2.7 fil.py test.csv test.html
导入csv2html
导入系统
课堂素描:
定义初始化(自):
"""
返回已定义场景的html草图
场景可以作为函数访问。
受支持的项目包括:
-失败
-通过
-状态更新
-决赛
"""
def_样式(自):
body=”“”
p{
字体系列:Calibri;
字体大小:14px;
字体大小:粗体;
文本对齐:左对齐;
}
p、 褪色{
颜色:#中交;
字体大小:14px;
}
em{
字体:斜体;
字体大小:16px;
字体重量:较轻;
}
埃姆通行证{
字体:斜体;
字体大小:16px;
颜色:绿色;
}
失败{
字体:斜体;
字体大小:16px;
颜色:红色;
}
a{
文字装饰:无;
}
a:悬停{
文字装饰:下划线;
}
人力资源{
对齐:左;
左边距:0px;
宽度:500px;
高度:1px;
}
桌子{
边界塌陷:塌陷;
}
tr{
填充:4px;
文本对齐:居中;
右边框:2px实心#FFFFFF;
}
tr:n子(偶数){背景色:#f2f2}
th{
背景色:#cceeff;
颜色:黑色;
填充:4px;
右边框:2px实心#FFFFFF;
}
"""
返回体
def_启动(自):
返回“”
"""
def_端(自):
body=”“”
注意:链接可能被禁用,
请把我放在安全寄件人名单上
点击消息。
这是系统生成的邮件,请不要
回应它
"""
返回体
def_故障(自身):
body=”“”
地位:
失败
"""
返回体
def严重故障(自身):
str_389;='故障很严重,正在终止运行。'
body=”“”
%
“”%str_
返回体
def_通行证(自):
body=”“”
地位:
通过
"""
返回体
定义类型(自身、标题、val):
body=”“”
%s:
%
“”%(title.upper(),val)
返回体
def_日志链接(自身,日志):
body=“”日志:
“”“%(日志,日志)
返回体
def呈现(自我、测试id、描述、平台=无、通过=真、\
日志=无,严重=错误):
body=self.\u start()+\
self._style()+\
自身类型(“测试id”,测试id)+\
自身类型(“描述”,描述)+\
自身类型(“平台”,平台)
如果通过=真:
身体+=自身。_通过()
其他:
body+=自我失败()
如果关键:
body+=自我.\u临界\u失败()
body+=自链接(日志)
body+=self.\u end()
返回体
def状态_更新(自我),包括:
通过
def最终版本(自我、日志):
body+=self.\u end()
返回体
def add_html_标题(csv_fl,fname):
“”“sqlite返回的html数据需要包含在
web解析它所必需的一些标记
正确地说
草图=草图()
content=”“”
%s%s
%
“”“%(草图.\u开始(),草图.\u样式(),csv2html.populate\u表格(csv\u fl))
打开(fname,'w')。写入(内容)
如果len(sys.argv)<3:
打印“用法:csvToTable.py csv\u文件html\u文件”
出口(1)
csv_fl=sys.argv[1]
html_fl=sys.argv[2]
添加html\U标题(csv\U fl、html\U fl)
要将整行涂成红色,只需
其中
是要着色的行
p{
字体系列:Calibri;
字体大小:14px;
字体大小:粗体;
文本对齐:左对齐;
}
p、 褪色{
颜色:#中交;
字体大小:14px;
}
em{
字体:斜体;
字体大小:16px;
字体重量:较轻;
}
埃姆通行证{
字体:斜体;
字体大小:16px;
颜色:绿色;
}
失败{
字体:斜体;
字体大小:16px;
颜色:红色;
}
a{
文字装饰:无;
}
a:悬停{
文字装饰:下划线;
}
人力资源{
对齐:左;
左边距:0px;
宽度:500便士