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()
}}}