未添加着色功能,这个比较麻烦,比如变量是什么色,函数是什么色,需要使用正则。


使用blog里自带的python代码着色,更改如下:print('<pre class="brush:python;toolbar:false">' + all +  '</pre>')


效果及代码如下:

# -*- coding: utf-8 -*-  
codedic={'"':'&quot;',"'":'&#39;','<':'&lt;','>':'&gt;','&':'&amp;'}
codekey=codedic.keys()
filename=input('input file path:')
print(filename)
f=open(filename,'r')
all=''
for line in f:
    ltmp=''
    for c in line:
        if c in codekey:
            ltmp+=codedic[c]
        elif ord(c)==9:
            ltmp+='&nbsp;&nbsp;'
        elif ord(c)==32:
            ltmp+='&nbsp;'
        else:
            ltmp+=c
            
    all=all+'<p style="margin-top: 0px; margin-bottom: 0px;">'+ltmp+'</p>'
f.close()
    
print('<pre class="brush:python;toolbar:false">' + all +  '</pre>')