替换表格内容及csv转html及CGI

替换指定内容

现在excel中替换再将替换的内容转换成CSV格式

# -*- coding: utf-8 -*-
"""
Created on Mon Apr 29 10:36:01 2019

@author: 18605
"""

import pandas as pd
df=pd.read_excel("D:张香婷pythonPython1.xlsx",index_col=None,na_values=['dgf'])
line=df.replace("
","")
line=line.replace("优秀","90")
line=line.replace("良好","80")
line=line.replace("合格","60")
line=line.fillna(value=0)
line.to_csv('D:张香婷pythonPython2.csv',encoding="utf_8_sig")
print(line)

运行结果

代码

# -*- coding: utf-8- -*-
"""
Spyder Editor

This is a temporary script file.
"""

#e13.1csvhtml.py
# encoding: utf-8
# -*- coding:utf-8
seg1='''
<!DOCTYPE HTML>
<html>
<body>
<mata charset=gb2312>
<h2 align=center>18信计二班python成绩统计表</h2>
<table border='1' align="center" width=70%>
<tr bgcolor='orange'>
'''
seg2="</tr>
"
seg3="</table>
</body>
</html>"
def fill_data(locls):
# ======================= NO.3 7个就够了 ======================
    seg='<tr><td align="center">{}</td><td align="center">
    {}</td><td align="center">{}</td><td align="center">
    {}</td><td align="center">{}</td><td align="center">
    {}</td><td align="center">{}</td><td align="center">
    '.format(*locls)
    #{}</td><td align="center">{}</td><td align="center">{}</td></tr>

    return seg

# ========================== NO.1 修改encoding ==============================
fr=open("python2.csv","r", encoding="utf-8")
ls=[]
for line in fr:
    line=line.replace("
",""))
    ls.append(line.split(","))
fr.close()
# ========================== NO.2 增加encoding ==============================
fw=open("Python123.html","w", encoding="utf-8")
fw.write(seg1)
# =================== NO.4 修改表格宽度 ========================
fw.write('<th width="10%">{}</th>
<th width="15%">{}</th>
<th width="15%">{}</th>

<th width="15%">{}</th>
<th width="15%">{}</th>

<th width="15%">{}</th>
<th width="15%">{}</th>
'.format(*ls[0]))
fw.write(seg2)
for i in range(len(ls)-1):
    fw.write(fill_data(ls[i+1]))
fw.write(seg3)
fw.close()

运行结果

csv转html

代码

# -*- coding: utf-8- -*-
"""
Spyder Editor

This is a temporary script file.
"""

#e13.1csvhtml.py
# encoding: utf-8
# -*- coding:utf-8
seg1='''
<!DOCTYPE HTML>
<html>
<body>
<mata charset=gb2312>
<h2 align=center>18信计二班python成绩统计表</h2>
<table border='1' align="center" width=70%>
<tr bgcolor='orange'>
'''
seg2="</tr>
"
seg3="</table>
</body>
</html>"
def fill_data(locls):
# ======================= NO.3 7个就够了 ======================
    seg='<tr><td align="center">{}</td><td align="center">
    {}</td><td align="center">{}</td><td align="center">
    {}</td><td align="center">{}</td><td align="center">
    {}</td><td align="center">{}</td><td align="center">
    '.format(*locls)
    #{}</td><td align="center">{}</td><td align="center">{}</td></tr>

    return seg

# ========================== NO.1 修改encoding ==============================
fr=open("python1.csv","r", encoding="utf-8")
ls=[]
for line in fr:
    line=line.replace("
","")
    ls.append(line.split(","))
fr.close()
# ========================== NO.2 增加encoding ==============================
fw=open("Python123.html","w", encoding="utf-8")
fw.write(seg1)
# =================== NO.4 修改表格宽度 ========================
fw.write('<th width="10%">{}</th>
<th width="15%">{}</th>
<th width="15%">{}</th>

<th width="15%">{}</th>
<th width="15%">{}</th>

<th width="15%">{}</th>
<th width="15%">{}</th>
'.format(*ls[0]))
fw.write(seg2)
for i in range(len(ls)-1):
    fw.write(fill_data(ls[i+1]))
fw.write(seg3)
fw.close()

运行结果展示

CGI

CGI(Common Gateway Interface)也叫通用网关接口,它是一个web服务器主机提供信息服务的标准接口,只要遵循这个接口,web服务器就能获取客户端提交的信息,转交给服务端的CGI程序进行处理,然后将处理结果返回给客户端。CGI通讯是由两部分组成的:一部分是用户的浏览器显示的页面,也就是html页面,另一部分则是运行在服务器上的CGI程序。它们之间的通讯方式如下图所示:

CGI其实就是连通HTTP服务器和其他资源(如数据库、文件系统)的一个通道,也是一个接口规范,所以它的学名就叫做通用网关接口。

1. 首先,在你的电脑里找一个目录,新建一个文件夹,叫做“www”,在“www”文件夹下面新建一个目录,叫做“cgi-bin”,我直接建立在了d盘的根目录下;

2. 使用管理员打开cmd命令行工具,进入到你刚才的“www”目录下,注意:是“www”目录;

在浏览器运行结果

网页内容

原文地址:https://www.cnblogs.com/SGzhang/p/10817026.html