Differences between revisions 1 and 2
Revision 1 as of 2005-07-28 13:02:49
Size: 1500
Editor: ZoomQuiet
Comment:
Revision 2 as of 2009-12-25 07:19:07
Size: 1500
Editor: localhost
Comment: converted to 1.6 markup
No differences found!

2005-07-28 Use Python As a tool

{{{Use Python As a tool

By 令狐虫

玩Python的日子也不算短了,正儿八经的东西也没见作出来一个。不过有什么关系呢,脚本语言么,给自己带来方便才是真的。

Python除了作为可以编写程序之外,利用它的强大函数库,用交互模式来快速的完成一些小任务,也是不错的选择。

1、转换big5文件为utf-8

上次下载了一个电影字幕,big5编码的,看着是乱码。所以用Python转了一下:

  • >>> src = open("1.srt").read() >>> dest = unicode(src, "big-5").encode("utf-8") >>> open("2.srt", "w").write(dest)

2、查看quoted编码。

有人问RescoViewer_v4%5b1%5d.30.3%bc%f2%cc%e5%d6%d0%ce%c4%b0%e6这个是什么意思?

用Python看看吧:

  • >>> import urllib >>> print urllib.unquote("RescoViewer_v4%5b1%5d.30.3%bc%f2%cc%e5%d6%d0%ce%c4%b0%e6") RescoViewer_v4[1].30.3简体中文版

3、为文件加上HTML段落标记

FireFox写文章就是要自己一段一段的分段比较不爽(我不爱用FCKEditor)。用Python试试吧:

  • >>> lines = open("test.txt").readlines() >>> fp = open("output.txt", "w") >>> for line in lines:

  • .. fp.write("<p>%s</p>" % line)

  • ..

    >>> fp.close()

4、把CSV格式的文件转换成TAB排版格式

  • >>> lines = open("aaa.csv").readlines() >>> fp = open("output.txt", "w") >>> for line in lines:

  • .. fp.write('\t'.join(line.split(',')))

    >>> fp.close()

}}}

MicroProj/2005-07-28 (last edited 2009-12-25 07:19:07 by localhost)