不能问点有用的问题吗?这种课后自己好好看书,自己动脑啊。
浦北网站制作公司哪家好,找创新互联公司!从网页设计、网站建设、微信开发、APP开发、自适应网站建设等网站项目制作,到程序开发,运营维护。创新互联公司从2013年创立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联公司。
最简单:
print('''+ - - - - + - - - - +
| | |
| | |
+ - - - - + - - - - +
| | |
| | |
+ - - - - + - - - - +''')
想要自己搞的复杂点?
def f(w=5,h=3,width=30,height=50)
def func(x,y):
if x%w==0 and y%h==0:
print('+',end='')
elif x%w==0:
print('|',end='')
elif y%h==0:
print('-',end='')
else:
print(' ',end='')
for y in range(height):
for x in range(width):
func(x,y)
print()
f()
现要实现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
def drawTable(iRow, iColumn, iCellW=5, iCellH=3):
def func(x, y):
if (x % iCellW == 0) and (y % iCellH == 0):
print '+',
elif x % iCellW == 0:
print '|',
elif y % iCellH == 0:
print '-',
else:
print ' ',
for y in range(iColumn * iCellH + 1):
for x in range(iRow * iCellW):
func(x, y)
print '+'
drawTable(5, 3)
上例为画一个五行三列表格的代码示例,在python 2.7版本上测试通过。
行列数目以及单元格宽度都可通过函数参数控制。
一、函数说明
在使用python作图时,应用最广的就是matplotlib包,但我们平时使用matplotlib时主要是画一些简单的图表,很少有涉及分段函数。本次针对数值实验中两个较为复杂的函数,使用其构建分段函数图像。
二、图像代码
2.11、函数公式:
y=4sin(4πt)-sgn(t-0.3)-sgn(0.72-t)
2.12、代码如下:
import numpy as np
import matplotlib.pyplot as plt
def sgn(x):
if x0:
return 1
elif x0:
return -1
else:
return 0
t=np.arange(0,1,0.01)
y=[]
for i in t:
y_1=4*np.sin(4*np.pi*i)-sgn(i-0.3)-sgn(0.72-i)
y.append(y_1)
plt.plot(t,y)
plt.xlabel("t")
plt.ylabel("y")
plt.title("Heavsine")
plt.show()
2.13、运行结果如下:
81036331d721706ae12808beb99b9574.png
2.21、函数公式:
479029.html
2.22、代码如下:
import numpy as np
import matplotlib.pyplot as plt
def g(x):
if x0:
return x
else:
return 0
t=np.arange(0,1,0.01)
y=[]
for i in t:
y_1=g(i*(1-i))*np.sin((2*np.pi*1.05)/(i+0.05))
y.append(y_1)
plt.plot(t,y)
plt.xlabel("t")
plt.ylabel("y")
plt.title("TimeSine")
plt.show()