如何使用Python批量对文本文件编码互转
作者:若安舟已远
时间:2023-04-26
来源:互联网
浏览:0
在Windows下写C语言默认是GB2312,放到Linux上就会乱码,因为Linux和MacOS默认是UTF-8,因此写了个Python小脚本对指定路径下的文件进行转换。fromsysimportargvimportosfromchardetimportdetectfromcodecsimportlookupCONFIG_FILE='.any2any'DEFAULT_CONFIG='''.c.h.cpp.hpp.hxx.cc.cxx.C.c++.m.cs.rs.j
在Windows下写C语言默认是GB2312,放到Linux上就会乱码,因为Linux和MacOS默认是UTF-8,因此写了个Python小脚本对指定路径下的文件进行转换。
from sys import argv
import os
from chardet import detect
from codecs import lookup
CONFIG_FILE = '.any2any'
DEFAULT_CONFIG = '''
.c
.h
.cpp
.hpp
.hxx
.cc
.cxx
.C
.c++
.m
.cs
.rs
.java
.kt
.php
.pm
.pl
.py
.sh
.go
.xml
.htm
.html
.css
.js
.jsx
.vue
.txt
.csv
'''
if os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE, 'r') as config_file:
file_extension = tuple(config_file.read().split())
else:
file_extension = tuple(DEFAULT_CONFIG.split())
print(f"将转换 {' '.join(list(file_extension))}")
def bytes_encoding(b: bytes, length: int = 1024) -> str:
'''
返回探测到的编码格式
'''
return detect(b[:length])['encoding']
def any2any(b: bytes, encoding: str) -> bytes:
'''
任意编码字节转换为任意编码字节
探测输入的字节编码格式,转换为指定编码,并返回对应字节
'''
file_encoding = bytes_encoding(b)
if file_encoding == encoding:
return b
return lookup(encoding).encode(lookup(file_encoding).decode(b)[0])[0]
def allfileset(path: str = '.', filepathset: set = set()) -> set:
'''
递归路径下所有文件,返回绝对路径集合
'''
if os.path.isdir(path):
for item in os.listdir(path):
filepath = os.path.join(path, item)
if os.path.isfile(filepath):
filepathset.add(os.path.abspath(filepath))
else:
allfileset(filepath, filepathset)
else:
filepathset.add(os.path.abspath(path))
return filepathset
def is_valid_inputs() -> bool:
'''
检查参数是否输入正确
'''
return len(argv) > 1 and all(map(os.path.exists, argv[1:]))
def is_valid_encoding(encoding: str) -> bool:
'''
检查是否存在指定编码
'''
try:
lookup(encoding)
return True
except:
return False
def choice_encoding() -> str:
choice = input('''!!!在转换前注意备份文件!!!
要转换到什么编码?
1. GB18030(Windows下常用,C语言不会乱码)
2. UTF-8(非Windows下通用,例如Linux和macOS)
3. 其他
> ''')
if choice == '1':
return 'GB18030'
elif choice == '2':
return 'UTF-8'
elif choice == '3':
choice = input('输入你想转换到的编码:')
while not is_valid_encoding(choice):
choice = input('不存在该编码,重新输入:')
return choice
else:
print('不做任何操作')
exit()
def main():
if is_valid_inputs():
encoding = choice_encoding()
filepathset = set()
for path in argv[1:]:
filepathset.union(filter(lambda s: s.endswith(file_extension), allfileset(path, filepathset)))
if filepathset:
for path in filepathset:
with open(path, 'rb') as f:
filebytes = any2any(f.read(), encoding)
with open(path, 'wb') as f:
f.write(filebytes)
print(f'{path} 已转换到 {encoding}')
print('转换已完成')
else:
print('没有任何可以转换的文件,请检查程序下是否有.any2any配置文件,用空格或换行间隔要转换的文件类型,例如 .c .cpp .cs')
else:
print("未收到任何要转换的文件或文件夹路径,或参数错误,请把要转换的文件或文件夹拖动到程序上。")
if __name__ == "__main__":
try:
main()
finally:
input('按任意键退出...')可以使用pyinstaller打包成可执行文件,带着走或者分享给其他人用
安装pyinstaller
conda install pyinstaller
打包Python文件,其中-i参数可以给.exe文件加上图标,-F参数指定要打包的脚本
pyinstaller -i icon.ico -F any2any.py
最后打包好的可执行文件在dict路径下。
作者最新文章
Word格式转换成PDF?Word转PDF的具体步骤是什么?
2026-09-02 19:13
长安猎手K50 2026款上市,14.19万元起售
2026-08-25 16:08
价格大跳水,网友惊呼买早了!两大巨头正面交锋,最高直降2500元,几乎所有品类“你降我也降”
2026-08-25 16:02
全新奥迪Q3 L申报信息揭晓,车身尺寸升级,动力配置保持强劲
2026-08-25 15:44
iPhone15ProMax屏幕常亮怎么设置 iPhone15ProMax屏幕常亮设置方法
2026-08-25 15:31
上一篇:
Java线性表的顺序表示及实现方法
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















