python open gbk_【转】用python将GBK编码文件转为UTF-8编码文件

#!/usr/bin/env python

# -*- coding: utf-8 -*-

__author__ = ''

import logging, os, argparse, textwrap

import time

import chardet

# Default configuration will take effect when corresponding input args are missing.

# Feel free to change this for your convenience.

DEFAULT_CONF = {

# Only those files ending with extensions in this list will be scanned or converted.

'exts' : ['cfg'],

'overwrite' : False,

'add_BOM' : False,

'convert_UTF' : False,

'confi_thres' : 0.8,

}

# We have to set a minimum threshold. Only those target_encoding results returned by chartdet that are above that threshold level would be accepted.

logging.basicConfig(format='%(levelname)s:%(message)s', level=http://logging.INFO)

log = logging.getLogger(__name__)

class Convert2Utf8:

def __init__(self, args):

self.args = args

def walk_dir(self, dirname):

for root, dirs, files in os.walk(dirname):

for name in files:

extension = os.path.splitext(name)[1][1:].strip().lower()

# On linux there is a newline at the end which will cause the match to fail, so we just 'strip()' the '\n'

# Also, add 'lower()' to ensure matching

if (extension in self.args.exts):

fullname = os.path.join(root, name)

try:

self.convert_file(fullname)

except IOError:

log.error("Unable to read or write the file: %s. Please check the file's permission.", fullname)

except KeyboardInterrupt:

log.warning("Interrupted by keyboard (e.g. Ctrl+C)")

exit()

# else:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值