[编程相关] python实现文件夹所有文件编码从GBK转为UTF8
作者:精品下载站 日期:2023-10-12 00:08:15 浏览:64 分类:编程开发
前言
最近因为工作的需要,将指定文件夹的源文件从 GBK 转为 UTF-8 编码格式。为了提高工作效率,第一时间就想到了使用 python 实现,为此记录一下,同时也希望这篇文章能帮助到更多的小伙伴。
一、编码格式相互转换
1.GBK 转换为 UTF8
部分代码如下(示例):
class CCopyFile: def __init__(self, src, dst): def ReadFile(filePath, encoding=""): with codecs.open(filePath, "rb", encoding) as f: return f.read() def WriteFile(filePath, contents, encoding=""): with codecs.open(filePath, "wb", encoding) as f: f.write(contents) def UTF8_2_GBK(src, dst): contents = ReadFile(src, encoding="utf-8") WriteFile(dst, contents, encoding="gb18030") def GBK_2_UTF8(src, dst): contents = ReadFile(src, encoding="gb18030") WriteFile(dst, contents, encoding="utf-8") def CopyFile(src, dst): with open(src, ';rb';) as readStream: contents = readStream.read() with open(dst, ';wb';) as writeStream: writeStream.write(contents) ';';'; 匹配后缀,只保存所选的文件格式,并调用 GBK_2_UTF8。 若要保存全部文件,则注释该句直接调用 CopyFile。 注: 1. GBK_2_UTF8 复制文件,并且将编码格式从GBK转为UTF-8 2. CopyFile 直接复制文件,保留源文件的编码格式 ';';'; if src.split(';.';)[-1] in postfix: GBK_2_UTF8(src, dst) else: CopyFile(src, dst)
2.将源文件夹复制至目标文件夹
部分代码如下(示例):
# 将源文件夹整体复制到目标文件夹 def CopyDir(srcPath, targetPath): if os.path.isdir(srcPath) and os.path.isdir(targetPath): filelist_src = os.listdir(srcPath) for file in filelist_src: path = os.path.join(os.path.abspath(srcPath), file) if os.path.isdir(path): path1 = os.path.join(os.path.abspath(targetPath), file) if not os.path.exists(path1): os.mkdir(path1) CopyDir(path, path1) else: path1 = os.path.join(targetPath, file) CCopyFile(path, path1) return True else: return False
3.将源文件夹复制至目标文件夹并且将文件从 GBK 转换为 UTF8 编码
完整代码如下:
import os import codecs # 设置路径 srcPath = r';D:\share\python_study\srcCode';targetPath = r';D:\share\python_study\out';# 设置要保存的文件格式 postfix = set([';h';, ';c';]) class CCopyFile: def __init__(self, src, dst): def ReadFile(filePath, encoding=""): with codecs.open(filePath, "rb", encoding) as f: return f.read() def WriteFile(filePath, contents, encoding=""): with codecs.open(filePath, "wb", encoding) as f: f.write(contents) def UTF8_2_GBK(src, dst): contents = ReadFile(src, encoding="utf-8") WriteFile(dst, contents, encoding="gb18030") def GBK_2_UTF8(src, dst): contents = ReadFile(src, encoding="gb18030") WriteFile(dst, contents, encoding="utf-8") def CopyFile(src, dst): with open(src, ';rb';) as readStream: contents = readStream.read() with open(dst, ';wb';) as writeStream: writeStream.write(contents) ';';'; 匹配后缀,只保存所选的文件格式,并调用 GBK_2_UTF8。 若要保存全部文件,则注释该句直接调用 CopyFile。 注: 1. GBK_2_UTF8 复制文件,并且将编码格式从GBK转为UTF-8 2. CopyFile 直接复制文件,保留源文件的编码格式 ';';'; if src.split(';.';)[-1] in postfix: GBK_2_UTF8(src, dst) else: CopyFile(src, dst)# 将源文件夹整体复制到目标文件夹 def CopyDir(srcPath, targetPath): if os.path.isdir(srcPath) and os.path.isdir(targetPath): filelist_src = os.listdir(srcPath) for file in filelist_src: path = os.path.join(os.path.abspath(srcPath), file) if os.path.isdir(path): path1 = os.path.join(os.path.abspath(targetPath), file) if not os.path.exists(path1): os.mkdir(path1) CopyDir(path, path1) else: path1 = os.path.join(targetPath, file) CCopyFile(path, path1) return True else: return False if __name__ == ';__main__';: nRet = CopyDir(srcPath, targetPath) if nRet: print(';Copy Dir OK!';) else: print(';Copy Dir Failed!';)
总结
以上就是今天要讲的内容,本文仅仅简单介绍了文件从GBK转为UTF-8 编码的使用。
如果对自动检测文件编码并实现目标编码转换,感兴趣的小伙伴,可以点击这里[源码+工具]:python + tkinter 图形化,文件编码格式自动转换工具
工具效果如图所示:
猜你还喜欢
- 03-29 [编程相关] Winform窗体圆角以及描边完美解决方案
- 03-29 [前端问题] has been blocked by CORS policy跨域问题解决
- 03-29 [编程相关] GitHub Actions 入门教程
- 03-29 [编程探讨] CSS Grid 网格布局教程
- 10-12 [编程相关] python实现文件夹所有文件编码从GBK转为UTF8
- 10-11 [编程算法] opencv之霍夫变换:圆
- 10-11 [编程算法] OpenCV Camshift算法+目标跟踪源码
- 10-11 [Python] python 创建 Telnet 客户端
- 10-11 [编程相关] Python 基于 Yolov8 + CPU 实现物体检测
- 03-15 [脚本工具] 使用go语言开发自动化脚本 - 一键定场、抢购、预约、捡漏
- 01-08 [编程技术] 秒杀面试官系列 - Redis zset底层是怎么实现的
- 01-05 [编程技术] 《Redis设计与实现》pdf
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[短剧] 2025年05月31日 精选+付费短剧推荐58部
[软件合集] 25年5月31日 精选软件66个
[电影] 黄沙漫天(2025) 4K.EDRMAX.杜比全景声 / 4K杜比视界/杜比全景声
[风口福利] 短视频红利新风口!炬焰创作者平台重磅激励来袭
[韩剧] 宝物岛/宝藏岛/金银岛(2025)【全16集】【朴炯植/悬疑】
[电影] 愤怒的牦牛 (2025) 国语中字 4k
[短剧合集] 2025年05月30日 精选+付费短剧推荐56部
[软件合集] 25年5月30日 精选软件26个
[软件合集] 25年5月29日 精选软件18个
[短剧合集] 2025年05月28日 精选+付费短剧推荐38部
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[美图] 2W美女个美女小姐姐,饱眼福
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag