加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

使用python的chardet库获得文件编码并修改编码

发布时间:2020-12-16 19:55:11 所属栏目:Python 来源:网络整理
导读:首先需要安装chardet库,有很多方式,我才用的是比较笨的方式:sudo pip install chardet 复制代码 代码如下: #!/usr/bin/env python # coding: UTF-8 import sys import os import chardet def print_usage(): print '''usage: change_charset [file|direct

首先需要安装chardet库,有很多方式,我才用的是比较笨的方式:sudo pip install chardet

复制代码 代码如下:

#!/usr/bin/env python
# coding: UTF-8
import sys
import os
import chardet

 
def print_usage():
  print '''usage:
  change_charset [file|directory] [charset] [output file]n
  for example:
    change 1.txt utf-8 n1.txt
    change 1.txt utf-8
    change . utf-8
    change 1.txt
'''
def get_charset(s):
  return chardet.detect(s)['encoding']

 
def remove(file_name):
  os.remove(file_name)

 
def change_file_charset(file_name,output_file_name,charset):
  f = open(file_name)
  s = f.read()
  f.close()

  if file_name == output_file_name or output_file_name == "":
    remove(file_name)

  old_charset = get_charset(s)
  u = s.decode(old_charset)

  if output_file_name == "":
    output_file_name = file_name
  f = open(output_file_name,'w')
  s = u.encode(charset)
  f.write(s)
  f.close()

 
def do(file_name,charset):
  if os.path.isdir(file_name):
    for item in os.listdir(file_name):
      try:
        if os.path.isdir(file_name+"/"+item):
          do(file_name+"/"+item,"",charset)
        else:
          change_file_charset(file_name+"/"+item,charset)
      except OSError,e:
        print e
  else:
    change_file_charset(file_name,charset)

 
if __name__ == '__main__':
  length = len(sys.argv)

  if length == 1:
    print_usage()
  elif length == 2:
    do(sys.argv[1],"utf-8")
  elif length == 3:
    do(sys.argv[1],sys.argv[2])
  elif length == 4:
    do(sys.argv[1],sys.argv[3],sys.argv[2])
  else:
    print_usage()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读