python實(shí)現(xiàn)批量轉(zhuǎn)換文件編碼(批轉(zhuǎn)換編碼示例)
# -*- coding:utf-8 -*-
__author__ = 'walkskyer'
import os
import glob
class Encoding:
def __init__(self):
#文件擴(kuò)展名
self.ext = ".*"
#編碼
self.srcEncoding=None
self.dstEncoding=None
def convertEncoding(self, content, srcEncoding=None, dstEncoding=None):
return content.decode(self.srcEncoding).encode(self.dstEncoding)
def processDirectory(self, args, dirname, filenames):
print 'Directory', dirname
for filename in filenames:
if not os.path.isdir(dirname+'/'+filename):
if filename.endswith(self.ext) or self.ext == ".*":
print ' File', filename
self.f2f(dirname+'/'+filename)
def f2f(self, filepath, srcEncoding=None, dstEncoding=None):
try:
f1 = open(filepath, 'rb')
temp = f1.read()
f1.close()
f2 = open(filepath, 'wb')
f2.write(temp.decode(self.srcEncoding).encode(self.dstEncoding))
f2.close()
print '轉(zhuǎn)碼成功'
except Exception, e:
print e
def colectFileType(self, dirname, fileType):
for filename in glob.glob(r'*.'+fileType):
print filename
def setExt(self, ext):
if not ext.startswith('.'):
ext = "." + ext
self.ext = ext
def setSRC(self, encoding):
self.srcEncoding=encoding
def setDST(self, encoding):
self.dstEncoding=encoding
if __name__ == '__main__':
obj = Encoding()
print u'請輸入文件類型:'
obj.setExt(raw_input())
print u'請輸入文件原始編碼:'
obj.setSRC(raw_input())
print u'請輸入文件目標(biāo)類型:'
obj.setDST(raw_input())
"""obj.setExt('html')
obj.setSRC('gbk')
obj.setDST('utf-8')"""
print u'請輸入文件所在目錄:'
path = raw_input()
os.path.walk(path, obj.processDirectory, None)
相關(guān)文章
詳解python中Numpy的屬性與創(chuàng)建矩陣
這篇文章給大家分享了關(guān)于python中Numpy的屬性與創(chuàng)建矩陣的相關(guān)知識點(diǎn)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。2018-09-09Pandas 合并多個(gè)Dataframe(merge,concat)的方法
今天小編就為大家分享一篇Pandas 合并多個(gè)Dataframe(merge,concat)的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06pyecharts如何旋轉(zhuǎn)折線圖的X軸標(biāo)簽
這篇文章主要介紹了pyecharts如何旋轉(zhuǎn)折線圖的X軸標(biāo)簽,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11基于Django框架的rest_framework的身份驗(yàn)證和權(quán)限解析
Django 是一個(gè)基于 Python 的 Web 框架,可讓您快速創(chuàng)建高效的 Web 應(yīng)用程序,這篇文章主要介紹了基于Django框架的rest_framework的身份驗(yàn)證和權(quán)限解析,需要的朋友可以參考下2023-05-05六個(gè)實(shí)用Pandas數(shù)據(jù)處理代碼
這篇文章主要介紹了六個(gè)實(shí)用Pandas數(shù)據(jù)處理代碼,文章圍繞主題相相關(guān)內(nèi)容,具有一定的參考價(jià)價(jià)值,需要的小伙伴可以參考一下2022-05-05