加入收藏 | 设为首页 | 会员中心 | 我要投稿 好传媒网 (https://www.haochuanmei.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > 建站资源 > 优化 > 正文

8 个 Python 实用脚本,收藏备用

发布时间:2019-10-16 04:38:16 所属栏目:优化 来源:实验楼
导读:脚本写的好,下班下得早!程序员的日常工作除了编写程序代码,还不可避免地需要处理相关的测试和验证工作。 例如,访问某个网站一直不通,需要确定此地址是否可访问,服务器返回什么,进而确定问题在于什么。完成这个任务,如果一味希望采用编译型语言来编

8.文本文件编码检测与转换。

  1. import sys 
  2. import os 
  3. import argparse 
  4. from chardet.universaldetector import UniversalDetector 
  5. parser = argparse.ArgumentParser(description = '文本文件编码检测与转换') 
  6. parser.add_argument('filePaths', nargs = '+', 
  7.  help = '检测或转换的文件路径') 
  8. parser.add_argument('-e', '--encoding', nargs = '?', const = 'UTF-8', 
  9.  help = ''' 
  10. 目标编码。支持的编码有: 
  11. ASCII, (Default) UTF-8 (with or without a BOM), UTF-16 (with a BOM), 
  12. UTF-32 (with a BOM), Big5, GB2312/GB18030, EUC-TW, HZ-GB-2312, ISO-2022-CN, EUC-JP, SHIFT_JIS, ISO-2022-JP, 
  13. ISO-2022-KR, KOI8-R, MacCyrillic, IBM855, IBM866, ISO-8859-5, windows-1251, ISO-8859-2, windows-1250, EUC-KR, 
  14. ISO-8859-5, windows-1251, ISO-8859-1, windows-1252, ISO-8859-7, windows-1253, ISO-8859-8, windows-1255, TIS-620 
  15. ''') 
  16. parser.add_argument('-o', '--output', 
  17.  help = '输出目录') 
  18. # 解析参数,得到一个 Namespace 对象 
  19. args = parser.parse_args() 
  20. # 输出目录不为空即视为开启转换, 若未指定转换编码,则默认为 UTF-8 
  21. if args.output != None: 
  22.  if not args.encoding: 
  23.  # 默认使用编码 UTF-8 
  24.  args.encoding = 'UTF-8' 
  25.  # 检测用户提供的输出目录是否有效 
  26.  if not os.path.isdir(args.output): 
  27.  print('Invalid Directory: ' + args.output) 
  28.  sys.exit() 
  29.  else: 
  30.  if args.output[-1] != '/': 
  31.  args.output += '/' 
  32. # 实例化一个通用检测器 
  33. detector = UniversalDetector() 
  34. print() 
  35. print('Encoding (Confidence)',':','File path') 
  36. for filePath in args.filePaths: 
  37.  # 检测文件路径是否有效,无效则跳过 
  38.  if not os.path.isfile(filePath): 
  39.  print('Invalid Path: ' + filePath) 
  40.  continue 
  41.  # 重置检测器 
  42.  detector.reset() 
  43.  # 以二进制模式读取文件 
  44.  for each in open(filePath, 'rb'): 
  45.  # 检测器读取数据 
  46.  detector.feed(each) 
  47.  # 若检测完成则跳出循环 
  48.  if detector.done: 
  49.  break 
  50.  # 关闭检测器 
  51.  detector.close() 
  52.  # 读取结果 
  53.  charEncoding = detector.result['encoding'] 
  54.  confidence = detector.result['confidence'] 
  55.  # 打印信息 
  56.  if charEncoding is None: 
  57.  charEncoding = 'Unknown' 
  58.  confidence = 0.99 
  59.  print('{} {:>12} : {}'.format(charEncoding.rjust(8), 
  60.  '('+str(confidence*100)+'%)', filePath)) 
  61.  if args.encoding and charEncoding != 'Unknown' and confidence > 0.6: 
  62.  # 若未设置输出目录则覆盖源文件 
  63.  outputPath = args.output + os.path.basename(filePath) if args.output else filePath 
  64.  with open(filePath, 'r', encoding = charEncoding, errors = 'replace') as f: 
  65.  temp = f.read() 
  66.  with open(outputPath, 'w', encoding = args.encoding, errors = 'replace') as f: 
  67.  f.write(temp) 

最后两个脚本内容选至实验楼的课程《使用 Python3 编写系列实用脚本》,课程对这两个脚本有详细的实现过程讲解,感兴趣的同学可以直接前往实验楼进行学习!

(编辑:好传媒网)

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

热点阅读