# import pandas
import os
def convert_file(file_dir,new_dir,desc_type,previous_type):
error_list = list()
for root,dirs,files in os.walk(file_dir):
for file in files:
file_path = os.path.join(root,file)
# try:
# df1 = pandas.read_csv(file_path,encoding=previous_type)
# new_path = os.path.join(new_dir,file)
# df1.to_csv(new_path,encoding=desc_type)
# except Exception as e:
# print(e)
# print("file :{} open is error and continue".format(file_path))
# error_list.append(file_path)
# continue
try:
with open(file_path, "rb") as f:
res = f.read().decode(previous_type).encode("utf-8").decode("utf-8-sig") # decode 是将二进制bytes编码转换为unicode,
with open(os.path.join(new_dir,file),"w",encoding=desc_type) as f: # encode 是将unicode编码转换为其他编码
f.write(res)
except Exception as e:
print("file :{} because error : [{}] continue".format(file,e))
error_list.append(file)
continue
if error_list:
with open("./convert_error/error.txt","w",encoding="utf-8") as f:
data = "\r\n".join(error_list)
f.write(data)
# print(error_list)
if __name__ == '__main__':
# 如果想要知道原始文件的格式,使用notepad++打开文件,右下角有文件的编码格式
file_dir = "./csv_data"
new_dir = "./csv_new_data"
desc_type = "utf-8"
previous_type = "utf-16" # UCS-2 Little Endian(即 utf-16)
convert_file(file_dir,new_dir,desc_type,previous_type)
python 转换文件编码,(utf-16进制转换为utf-8)
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 使用reactnative开发蓝牙类App时偶尔需要使用 来源于谷歌转换框架closure-library
- 《六月•乌溪江两岸》 万物葱茏。 捕鱼人撒下网,同时也撒下诱饵 一群鱼游过来,又游过去,较量开启 静水流深,忧伤四...