推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
twor
V2EX  ›  Python

Python 代码在 V2EX 回贴丢失缩进的改善小代码

  •  
  •   twor · Feb 6, 2018 · 3069 views
    This topic created in 3155 days ago, the information mentioned may be changed or developed.
    菜鸟习作,仅是可用而已,欢迎改善指正

    ➜ ~ ./replaceblank.py replaceblank.py
    #!/usr/bin/python
    # -*- coding: UTF-8 -*-

    import sys

    if sys.argv[1]:
    ----path = sys.argv[1]
    ----myfile = open(path)
    ----try:
    --------for line in myfile:
    ------------newline = ''
    ------------for c in line:
    ----------------if c == ' ':
    --------------------newline += '-'
    ----------------else:
    --------------------break
    ------------newline += line.strip()
    ------------print(newline)
    ----finally:
    --------myfile.close()
    9 replies  •  2018-02-07 15:45:58 +08:00
    flyingghost
        1
    flyingghost  
       Feb 6, 2018
    #!/usr/bin/python
    #  *  coding: UTF 8  * 

    import sys

    if sys.argv[1]:
        path = sys.argv[1]
        myfile = open(path)
        try:
            for line in myfile:
                newline = ''
                for c in line:
                    if c == ' ':
                        newline += '-'
                    else:
                        break
                newline += line.strip()
                print(newline)
        finally:
            myfile.close()
    flyingghost
        2
    flyingghost  
       Feb 6, 2018
    @flyingghost V2 不给编辑 /删除真是让人羞愤。。。
    flyingghost
        3
    flyingghost  
       Feb 6, 2018
    #!/usr/bin/python
    # -*- coding: UTF-8 -*-

    import sys

    if sys.argv[1]:
       path = sys.argv[1]
       myfile = open(path)
       try:
         for line in myfile:
           newline = ''
           for c in line:
             if c == ' ':
               newline += '-'
             else:
               break
           newline += line.strip()
           print(newline)
       finally:
         myfile.close()
    flyingghost
        4
    flyingghost  
       Feb 6, 2018
    嗯。。。换成全角空格稍微好看一点。
    虽然和-号一样不能复制。
    twor
        5
    twor  
    OP
       Feb 6, 2018
    @flyingghost 具体怎么把半角空格怎么替换成全角空格呢?
    我搜了一下 newline += u'\u3000' 好像替换后,贴到这里,是乱的
    8023
        6
    8023  
       Feb 6, 2018 via Android
    emmm 为啥不用 gist 呢?
    twor
        7
    twor  
    OP
       Feb 6, 2018
    @8023 多一种选择嘛,玩呗
    wwqgtxx
        8
    wwqgtxx  
       Feb 7, 2018 via iPhone
    @8023 因为有些时候发帖不想开梯子
    congeec
        9
    congeec  
       Feb 7, 2018   ❤️ 1
    用 vim 插件 identLine 可破
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   863 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 18:57 · PVG 02:57 · LAX 11:57 · JFK 14:57
    ♥ Do have faith in what you're doing.