ActiveState Code

Recipe 576682: text-to-html


将文本排版转化为html中相同的排版。 为google blog服务

Python
1
2
3
4
5
6
7
8
9
#文本原样到html中
#对换行符的处理
#需求产生于:Google Blog
line_break="\n"

data=file("e://untitled.txt","r").read()
out=data.replace(line_break,"<br/>")

file("c://out.txt","w").write(out)

Comments

  1. 1. At 3:36 a.m. on 6 mar 2009, Ash said:

    Well this is not very helpful, you need to do a lot more for a real HTML conversion. And why would you write out to .txt instead of .html? E.g. converting some Wiki-Markup to HTML converts Lists to <ul> elements and the like would be also necessary for a more interesting use case

  2. 2. At 4:12 p.m. on 6 mar 2009, kib2 said:

    Sure, you're winning a place inside the PyPy team with such awesome scripts... Waiting for html2txt version (a one liner maybe ?).

  3. 3. At 7:27 a.m. on 13 mar 2009, Sandy Norton said:

    Please do not clutter up the recipe space with such trivialities, otherwise you might as well make "print 'hello world'" a recipe...

Sign in to comment