推荐学习书目
› 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 · 3194 views
    This topic created in 3198 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
    flyingghost
    Feb 6, 2018
    1
    #!/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
    flyingghost
    Feb 6, 2018
    2
    @flyingghost V2 不给编辑 /删除真是让人羞愤。。。
    flyingghost
    flyingghost
    Feb 6, 2018
    3
    #!/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
    flyingghost
    Feb 6, 2018
    4
    嗯。。。换成全角空格稍微好看一点。
    虽然和-号一样不能复制。
    twor
    twor
    OP
    Feb 6, 2018
    5
    @flyingghost 具体怎么把半角空格怎么替换成全角空格呢?
    我搜了一下 newline += u'\u3000' 好像替换后,贴到这里,是乱的
    8023
    8023
    Feb 6, 2018 via Android
    6
    emmm 为啥不用 gist 呢?
    twor
    twor
    OP
    Feb 6, 2018
    7
    @8023 多一种选择嘛,玩呗
    wwqgtxx
    wwqgtxx
    Feb 7, 2018 via iPhone
    8
    @8023 因为有些时候发帖不想开梯子
    congeec
    congeec
    Feb 7, 2018❤️1
    9
    用 vim 插件 identLine 可破
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1716 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 16:12 · PVG 00:12 · LAX 08:12 · JFK 11:12
    ♥ Do have faith in what you're doing.