V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2018-02-06 20:08:01 +08:00 · 2298 次点击
    这是一个创建于 2262 天前的主题,其中的信息可能已经有所发展或是发生改变。
    菜鸟习作,仅是可用而已,欢迎改善指正

    ➜ ~ ./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 条回复    2018-02-07 15:45:58 +08:00
    flyingghost
        1
    flyingghost  
       2018-02-06 20:33:20 +08:00
    #!/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  
       2018-02-06 20:34:17 +08:00
    @flyingghost V2 不给编辑 /删除真是让人羞愤。。。
    flyingghost
        3
    flyingghost  
       2018-02-06 20:36:51 +08:00
    #!/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  
       2018-02-06 20:38:45 +08:00
    嗯。。。换成全角空格稍微好看一点。
    虽然和-号一样不能复制。
    twor
        5
    twor  
    OP
       2018-02-06 20:55:27 +08:00
    @flyingghost 具体怎么把半角空格怎么替换成全角空格呢?
    我搜了一下 newline += u'\u3000' 好像替换后,贴到这里,是乱的
    8023
        6
    8023  
       2018-02-06 21:05:33 +08:00 via Android
    emmm 为啥不用 gist 呢?
    twor
        7
    twor  
    OP
       2018-02-06 21:09:29 +08:00
    @8023 多一种选择嘛,玩呗
    wwqgtxx
        8
    wwqgtxx  
       2018-02-07 07:47:48 +08:00 via iPhone
    @8023 因为有些时候发帖不想开梯子
    congeec
        9
    congeec  
       2018-02-07 15:45:58 +08:00   ❤️ 1
    用 vim 插件 identLine 可破
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5259 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 07:49 · PVG 15:49 · LAX 00:49 · JFK 03:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.