2008-01-31

python 知识小结

关键字: python
转换字符集 为unicode字符集:unicode(shortname,“原字符集”,'ignore')


#得到文件的字符集
def detectCharset(file):
fileCharSet = "GBK"
detector = UniversalDetector()
for line in open(file):
detector.feed(line)
if detector.done: break
detector.close()
if detector.result.has_key("encoding"):
fileCharSet = detector.result["encoding"]
return str(fileCharSet)
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
得到系统的字符集
def getDefaultEncoding():
if hasattr(sys.stdin, 'encoding') and sys.stdin.encoding:
return sys.stdin.encoding
else :
import locale
locale_name, console_encoding = locale.getdefaultlocale()
return console_encoding
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
得到字符串的字符集
def chardetStr(s):
testlist=chardet.detect(s)
charset = testlist['encoding']
if charset=="GB2312":
charset="GBK"
return charset


python得到wav文件的播放时间
def getWAVDuration(filename):
import wave
w = wave.open(filename, "rb")
nf = w.getnframes()
w.close()
dur = math.ceil((nf*1.0)/44100)
return(int(dur))
评论
发表评论

您还没有登录,请登录后发表评论

88958620
搜索本博客
我的相册
121e6bfe-bb49-46f4-9736-fbe3789ec49e-thumb
DSC01762
共 1 张
存档
最新评论