Python批量轉(zhuǎn)換文件編碼格式
更新時(shí)間:2015年05月17日 15:16:29 投稿:hebedich
需要將工作目錄下的文件進(jìn)行轉(zhuǎn)碼,開(kāi)始的編碼是GBK的,需要將其轉(zhuǎn)換為utf-8的。文件較多,手動(dòng)轉(zhuǎn)換肯定不行,用Python寫(xiě)個(gè)腳本來(lái)實(shí)現(xiàn)。
自己寫(xiě)的方法,適用于linux,
#!/usr/bin/python
#coding=utf-8
import sys
import os, os.path
import dircache
import commands
def add(x,y):
return x*y
def trans(dirname):
lis = dircache.opendir(dirname)
for a in lis:
af=dirname+os.sep+a
## print af
if os.path.isdir(af):
## print af
trans(af)
else:
## print af+"encoding="+fi.name
ft = commands.getoutput('file -i '+af)
## print ft
if a.find('.htm')==-1 and a.find('.xml')==-1 and ft.find('text/')!=-1 and ft.find('iso-8859')!=-1:
print 'gbk'+ft+">"+af
commands.getoutput('iconv -ficonv -f gbk -t utf-8 -c -o'+""+af+""+af)
trans(os.getcwd())
py2.6以下版本可用代碼
import os,sys
def convert( filename, in_enc = "GBK", out_enc="UTF8" ):
try:
print "convert " + filename,
content = open(filename).read()
new_content = content.decode(in_enc).encode(out_enc)
open(filename, 'w').write(new_content)
print " done"
except:
print " error"
def explore(dir):
for root, dirs, files in os.walk(dir):
for file in files:
path = os.path.join(root, file)
convert(path)
def main():
for path in sys.argv[1:]:
if os.path.isfile(path):
convert(path)
elif os.path.isdir(path):
explore(path)
if __name__ == "__main__":
main()
支持py3.1的版本
import os
import sys
import codecs
#該程序用于將目錄下的文件從指定格式轉(zhuǎn)換到指定格式,默認(rèn)的是GBK轉(zhuǎn)到utf-8
def convert(file,in_enc="GBK",out_enc="UTF-8"):
try:
print ("convert " +file)
f=codecs.open(file,'r',in_enc)
new_content=f.read()
codecs.open(file,'w',out_enc).write(new_content)
#print (f.read())
except IOError as err:
print ("I/O error: {0}".format(err))
def explore(dir):
for root,dirs,files in os.walk(dir):
for file in files:
path=os.path.join(root,file)
convert(path)
def main():
for path in sys.argv[1:]:
if(os.path.isfile(path)):
convert(path)
elif os.path.isdir(path):
explore(path)
if __name__=="__main__":
main()
以上所述就是本文 的全部?jī)?nèi)容了,希望大家能夠喜歡。
相關(guān)文章
pip安裝提示Twisted錯(cuò)誤問(wèn)題(Python3.6.4安裝Twisted錯(cuò)誤)
這篇文章主要介紹了pip安裝提示Twisted錯(cuò)誤問(wèn)題(Python3.6.4安裝Twisted錯(cuò)誤),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
Python和Java進(jìn)行DES加密和解密的實(shí)例
下面小編就為大家分享一篇Python和Java進(jìn)行DES加密和解密的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
python:解析requests返回的response(json格式)說(shuō)明
這篇文章主要介紹了python:解析requests返回的response(json格式)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
python爬取網(wǎng)易云音樂(lè)熱歌榜實(shí)例代碼
在本篇文章里小編給大家整理的是關(guān)于python爬取網(wǎng)易云音樂(lè)熱歌榜實(shí)例代碼,需要的朋友們可以學(xué)習(xí)下。2020-08-08
Python使用lxml庫(kù)實(shí)現(xiàn)高效處理XML和HTML
lxml?是一個(gè)功能強(qiáng)大且高效的庫(kù),它基于?libxml2?和?libxslt?庫(kù),提供了簡(jiǎn)潔易用的?API?來(lái)處理?XML?和?HTML?文檔,下面小編就來(lái)和大家詳細(xì)講講它的具體使用吧2025-02-02
python 實(shí)現(xiàn)mysql增刪查改示例代碼
python中可以通過(guò)pymysql實(shí)現(xiàn)MySQL數(shù)據(jù)庫(kù)的連接,并實(shí)現(xiàn)數(shù)據(jù)庫(kù)的各種操作,這篇文章主要給大家介紹了關(guān)于pymsql實(shí)現(xiàn)增刪改查的示例代碼,需要的朋友可以參考下2021-11-11

