欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

用Python實(shí)現(xiàn)換行符轉(zhuǎn)換的腳本的教程

 更新時(shí)間:2015年04月16日 09:12:15   作者:dbzhang800  
這篇文章主要介紹了用Python實(shí)現(xiàn)換行符轉(zhuǎn)換的腳本的教程,代碼非常簡(jiǎn)單,包括一個(gè)對(duì)操作說(shuō)明的功能的實(shí)現(xiàn),需要的朋友可以參考下

很簡(jiǎn)單的一個(gè)東西,在'\n'、'\r\n'、'\r'3中換行符之間進(jìn)行轉(zhuǎn)換。
用法

復(fù)制代碼 代碼如下:
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()的一個(gè)簡(jiǎn)單的練習(xí)??梢杂?,但還相當(dāng)不完善。

 #!/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 

相關(guān)文章

  • python實(shí)現(xiàn)切割url得到域名、協(xié)議、主機(jī)名等各個(gè)字段的例子

    python實(shí)現(xiàn)切割url得到域名、協(xié)議、主機(jī)名等各個(gè)字段的例子

    今天小編就為大家分享一篇python實(shí)現(xiàn)切割url得到域名、協(xié)議、主機(jī)名等各個(gè)字段的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-07-07
  • matplotlib基本圖形繪制操作實(shí)例

    matplotlib基本圖形繪制操作實(shí)例

    這篇文章主要為大家介紹了matplotlib基本圖形繪制操作實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • python出現(xiàn)RuntimeError錯(cuò)誤問題及解決

    python出現(xiàn)RuntimeError錯(cuò)誤問題及解決

    這篇文章主要介紹了python出現(xiàn)RuntimeError錯(cuò)誤問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Python機(jī)器學(xué)習(xí)庫(kù)sklearn(scikit-learn)的基礎(chǔ)知識(shí)和高級(jí)用法

    Python機(jī)器學(xué)習(xí)庫(kù)sklearn(scikit-learn)的基礎(chǔ)知識(shí)和高級(jí)用法

    Scikit-Learn是 Python 最流行的機(jī)器學(xué)習(xí)庫(kù)之一,它提供了各種工具來(lái)實(shí)現(xiàn)、評(píng)估和探索各種學(xué)習(xí)算法,用于,各種機(jī)器學(xué)習(xí)任務(wù),在本教程中,我們將介紹 Scikit-Learn 的基礎(chǔ)知識(shí)和一些高級(jí)用法,并提供一些實(shí)例代碼來(lái)幫助我們更好地理解
    2023-07-07
  • numpy刪除單行、刪除單列、刪除多列實(shí)現(xiàn)方式

    numpy刪除單行、刪除單列、刪除多列實(shí)現(xiàn)方式

    這篇文章主要介紹了numpy刪除單行、刪除單列、刪除多列實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Python3按一定數(shù)據(jù)位數(shù)格式處理bin文件的方法

    Python3按一定數(shù)據(jù)位數(shù)格式處理bin文件的方法

    今天小編就為大家分享一篇Python3按一定數(shù)據(jù)位數(shù)格式處理bin文件的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • Python導(dǎo)入不同文件夾中文件的方法詳解

    Python導(dǎo)入不同文件夾中文件的方法詳解

    在寫python程序的時(shí)候,經(jīng)常會(huì)用到引入其他文件夾里的py文件,下面這篇文章主要給大家介紹了關(guān)于Python導(dǎo)入不同文件夾中文件的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • python的id()函數(shù)解密過(guò)程

    python的id()函數(shù)解密過(guò)程

    id()函數(shù)在使用過(guò)程中很頻繁,為此本人對(duì)此函數(shù)深入研究下,曬出代碼和大家分享下,希望對(duì)你們有所幫助
    2012-12-12
  • Python中數(shù)組遍歷的方法總結(jié)

    Python中數(shù)組遍歷的方法總結(jié)

    數(shù)組是編程中經(jīng)常使用的數(shù)據(jù)結(jié)構(gòu),用于存儲(chǔ)和操作一組元素,Python提供了多種方法來(lái)遍歷數(shù)組,本文將深入探討這些方法,提供詳細(xì)的示例代碼,希望對(duì)大家有所幫助
    2023-11-11
  • python讀取文本中的坐標(biāo)方法

    python讀取文本中的坐標(biāo)方法

    今天小編就為大家分享一篇python讀取文本中的坐標(biāo)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10

最新評(píng)論