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

8 个 Python 适用剧本,保藏备用

发布时间:2019-10-10 06:34:07 所属栏目:建站 来源:实验楼
导读:剧本写的好,放工下得早!措施员的一般事变除了编写措施代码,还不行停止地必要处理赏罚相干的测试和验证事变。 譬喻,会见某个网站一向不通,必要确定此地点是否可会见,处事器返回什么,进而确定题目在于什么。完成这个使命,假如一味但愿回收编译型说话来编

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 编写系列适用剧本》,课程对这两个剧本有具体的实现进程讲授,感乐趣的同窗可以直接前去尝试楼举办进修!

(编辑:湖南网)

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

热点阅读