前段时间入手了一个Sony PRS-505的阅读器,不过因为汉化的原因,折腾了很久,终于全部搞定了。麻烦的是505认得最好的编码方式为utf-8,如果是unicode,当文件大于5M时就容易出现问题。所以许多大的txt文档都要转换成utf-8. 手动转换很麻烦,特别是大的文件,打开就要等一段时间,然后还要转码。所以就想到用刚学的Python去做一个小工具,以后方便处理这些书。
该工具带GUI界面,经测试能够成功的把ANSI格式txt文件转换为utf8。因为主要看feiku的电子书,所以代码中带有自动重命名功能,目前不带批处理能力,以后补上
# -*- coding: cp936 -*- from Tkinter import * import tkFileDialog import tkSimpleDialog import os from os import listdir ipth='' rpth='' def inputpath(): global ipth InputFile=tkFileDialog.askopenfilename() print InputFile ipth=InputFile def chresultdir(): global rpth OutputDirect=tkSimpleDialog.askstring('Book storage','Input Path',initialvalue="D:/bookresult5") print OutputDirect rpth=OutputDirect def processfile(): fopen=open(ipth,"r") tempcontent=fopen.read() fopen.close() tounicode=unicode(tempcontent,"gb2312","ignore") toutf8=tounicode.encode("utf8") import os.path if (os.path.exists(rpth)==False): tempresultdir=os.mkdir(rpth) else: pass os.chdir(rpth) tempresult=open("temp.txt",'w') tempresult.write(toutf8) tempresult.close() f1=open(ipth,"r") tempname=f1.readline(10) print tempname newname=tempname.rstrip() print newname f1.close() os.rename("temp.txt",newname+".txt") root=Tk() B1=Button(root,text="input",width=30,height=2,command=inputpath).pack() B2=Button(root,text="OK",width=30,height=2,command=processfile).pack() B3=Button(root,text="Change Result Directory",width=30,height=2,command=chresultdir).pack() root.mainloop()
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有