国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

用Python實現換行符轉換的腳本的教程

2020-02-23 00:44:31
字體:
來源:轉載
供稿:網友

很簡單的一個東西,在'/n'、'/r/n'、'/r'3中換行符之間進行轉換。
用法

代碼如下:usage: eol_convert.py [-h] [-r] [-m {u,p,w,m,d}] [-k] [-f]
                      filename [filename ...]

Convert Line Ending

positional arguments:
  filename        file names

optional arguments:
  -h, --help      show this help message and exit
  -r              walk through directory
  -m {u,p,w,m,d}  mode of the line ending
  -k              keep output file date
  -f              force conversion of binary files

源碼

這只能算是argparse模塊和os模塊的utime()、stat()、walk()的一個簡單的練習。可以用,但還相當不完善。

 #!/usr/bin/env python   #2009-2011 dbzhang800   import os   import re   import os.path      def convert_line_endings(temp, mode):     if mode in ['u', 'p']: #unix, posix       temp = temp.replace('/r/n', '/n')       temp = temp.replace('/r', '/n')     elif mode == 'm':   #mac (before Mac OS 9)       temp = temp.replace('/r/n', '/r')       temp = temp.replace('/n', '/r')     elif mode == 'w':   #windows       temp = re.sub("/r(?!/n)|(?<!/r)/n", "/r/n", temp)     return temp      def convert_file(filename, args):     statinfo = None     with file(filename, 'rb+') as f:       data = f.read()       if '/0' in data and not args.force: #skip binary file... ?         print '%s is a binary file?, skip...' % filename         return       newdata = convert_line_endings(data, args.mode)       if (data != newdata):         statinfo = os.stat(filename) if args.keepdate else None         f.seek(0)         f.write(newdata)         f.truncate()     if statinfo:       os.utime(filename, (statinfo.st_atime, statinfo.st_mtime))     print filename      def walk_dir(d, args):     for root, dirs, files in os.walk(d):       for name in files:         convert_file(os.path.join(root, name), args)      if __name__ == '__main__':     import argparse     import sys     parser = argparse.ArgumentParser(description='Convert Line Ending')     parser.add_argument('filename', nargs='+', help='file names')     parser.add_argument('-r', dest='recursive', action='store_true',         help='walk through directory')     parser.add_argument('-m', dest='mode', default='d', choices='upwmd',         help='mode of the line ending')     parser.add_argument('-k', dest='keepdate', action='store_true',         help='keep output file date')     parser.add_argument('-f', dest='force', action='store_true',         help='force conversion of binary files')     args = parser.parse_args()     if args.mode == 'd':       args.mode = 'w' if sys.platform == 'win32' else 'p'        for filename in args.filename:       if os.path.isdir(filename):         if args.recursive:           walk_dir(filename, args)         else:           print '%s is a directory, skip...' % filename       elif os.path.exists(filename):         convert_file(filename, args)       else:         print '%s does not exist' % filename             
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 汉中市| 奉节县| 罗定市| 巴中市| 甘泉县| 武夷山市| 呼伦贝尔市| 景德镇市| 宣武区| 泌阳县| 昌宁县| 虎林市| 谷城县| 从化市| 潼南县| 雷山县| 疏附县| 阿拉善盟| 平和县| 新晃| 什邡市| 蓝山县| 嵊泗县| 额尔古纳市| 塔城市| 根河市| 靖西县| 金湖县| 什邡市| 富顺县| 广汉市| 平武县| 安乡县| 越西县| 乌兰察布市| 永安市| 内乡县| 西丰县| 新化县| 新乐市| 泗水县|