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

2018-02-06 20:08:01 +08:00
 twor
菜鸟习作,仅是可用而已,欢迎改善指正

➜ ~ ./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()
2312 次点击
所在节点    Python
9 条回复
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
2018-02-06 20:34:17 +08:00
@flyingghost V2 不给编辑 /删除真是让人羞愤。。。
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
2018-02-06 20:38:45 +08:00
嗯。。。换成全角空格稍微好看一点。
虽然和-号一样不能复制。
twor
2018-02-06 20:55:27 +08:00
@flyingghost 具体怎么把半角空格怎么替换成全角空格呢?
我搜了一下 newline += u'\u3000' 好像替换后,贴到这里,是乱的
8023
2018-02-06 21:05:33 +08:00
emmm 为啥不用 gist 呢?
twor
2018-02-06 21:09:29 +08:00
@8023 多一种选择嘛,玩呗
wwqgtxx
2018-02-07 07:47:48 +08:00
@8023 因为有些时候发帖不想开梯子
congeec
2018-02-07 15:45:58 +08:00
用 vim 插件 identLine 可破

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/429009

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX