1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > python将源代码转换成在html可显示的格式

python将源代码转换成在html可显示的格式

时间:2018-08-05 01:32:27

相关推荐

python将源代码转换成在html可显示的格式

importsys """本python脚本将代码文件转换成可以在html中显示的格式 """ defescape(text): """将text文本中的空格、&、<、>、(")、(")转化成对应的的字符实体,以方便在html上显示 """ text=text.replace("&","&#38;") text=text.replace("","&#160;") text=text.replace("<","&#60;") text=text.replace(">","&#62;") text=text.replace(""","&#34;") text=text.replace("\"","&#39;") returntext defchangetohtml(text): """将text以行为单位加上<li></li>标签 """ lines=text.split("\n") i=0 forlineinlines: lines[i]="<li>"+line+"</li>" i+=1 text="".join(lines) returntext try: filename=input("请输入文件名:") filename=filename.replace("\r","")#在控制台中输入回车后文件名会多一个"\r",需要去掉 f=open(filename,encoding="utf8") t=f.read() f.close() t=escape(t) t=changetohtml(t) f=open(filename+"生成的html代码.txt","wt",encoding="utf8") f.write(t) f.close() except: print("Unexpectederror:",sys.exc_info()[0],sys.exc_info()[1]) input("按回车键退出...")

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。