Skip to content

Instantly share code, notes, and snippets.

@mickhan
Created April 16, 2013 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mickhan/5394008 to your computer and use it in GitHub Desktop.
Save mickhan/5394008 to your computer and use it in GitHub Desktop.
def txt2html(txt):
'''将txt以行为单位加上<li></li>标签'''
def escape(txt):
'''将txt文本中的空格、&、<、>、(")、(')转化成对应的的字符实体,以方便在html上显示'''
txt = txt.replace('&','&#38;')
txt = txt.replace(' ','&#160;')
txt = txt.replace('<','&#60;')
txt = txt.replace('>','&#62;')
txt = txt.replace('"','&#34;')
txt = txt.replace('\'','&#39;')
return txt
txt = escape(txt)
lines = txt.split('\n')
for i, line in enumerate(lines):
lines[i] = '<li>' + line + '</li>'
#lines[i] = '<p>' + line + '</p>'
txt = ''.join(lines)
return txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment