栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

用Python实现换行符转换的脚本的教程

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用Python实现换行符转换的脚本的教程

很简单的一个东西,在'n'、'rn'、'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('rn', 'n') 
      temp = temp.replace('r', 'n') 
    elif mode == 'm':   #mac (before Mac OS 9) 
      temp = temp.replace('rn', 'r') 
      temp = temp.replace('n', 'r') 
    elif mode == 'w':   #windows 
      temp = re.sub("r(?!n)|(?

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/35392.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号