虎年开始学Python了!(是不是要蛇年开始学比较好?)
初学Python,经常要在网上拷贝别人的代码,但是很多网站的代码前总会有数字行号,一行一行删除实在太麻烦。baidu一下,删除代码前行号的方法也不少了,有用word、utraedit等软件的,也有用javascript写的小工具,还有用python写的命令行工具等。试用了一下,多少都不太符合自己的要求,于是就写了这个小工具。主要还是为了练练手。这是我的第一个python程序(如果hello world不算的话),希望有人能用到。
代码如下,有两个版本,wxPython和PyQt4。现在这个代码比较冗长,如果用正则表达式可能会好一点,不过我还不太会正则,以后再说了。
(2010-2-20 更新:正则表达式版本; 2010-2-21 更新:lambda版本; 2010-2-22更新:Clipboard版本 )
以下是wxPython版本
# -*- coding: utf-8 -*-
"""
去除代码前行号的Python小工具-wxPython版
Develop by Yanxy
"""
import wx
class MainWindow(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id,
u'去除代码前行号的Python小工具 - wxPython版 - Develop by Yanxy')
self.textBox = wx.TextCtrl(self, 1, style=wx.TE_MULTILINE,size=(600,600))
self.butOK = wx.Button(self, label=u"去除行号")
self.butLeft = wx.Button(self, label=u"去除左侧一个字符")
self.Bind(wx.EVT_BUTTON, self.CutLineNum, self.butOK)
self.Bind(wx.EVT_BUTTON, self.CutLeftChar, self.butLeft)
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
self.sizer = wx.BoxSizer(wx.HORIZONTAL)
self.sizer.Add(self.textBox,1,wx.EXPAND)
self.sizer.Add(self.butOK)
self.sizer.Add(self.butLeft)
self.SetSizer(self.sizer)
self.SetAutoLayout(1)
self.sizer.Fit(self)
self.Show(True)
def OnCloseWindow(self, event):
self.Destroy()
def CutLineNum(self, event):
multiStr = unicode(self.textBox.GetValue()).splitlines(1)
outStr = u''
for singleStr in multiStr:
singleStr = singleStr.lstrip()
i=0
for charStr in singleStr:
if charStr.isdigit():
i += 1
elif i>0:
singleStr = singleStr[i:]
break
else:
break
outStr += singleStr
self.textBox.SetValue(outStr)
def CutLeftChar(self, event):
outStr = u''
multiStr = unicode(self.textBox.GetValue()).splitlines(1)
for singleStr in multiStr:
singleStr = singleStr[1:]
outStr += singleStr
self.textBox.SetValue(outStr)
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = MainWindow(parent=None, id=-1)
app.MainLoop()
del app
以下是PyQt4版本
# -*- coding: utf-8 -*-
"""
去除代码前行号的Python小工具-PyQt4版
Develop by Yanxy
"""
import sys
from PyQt4 import QtCore, QtGui
app = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
window.resize(800, 600)
window.setWindowTitle(u'去除代码前行号的Python小工具 - PyQt4版 - Develop by Yanxy')
textBox = QtGui.QTextEdit()
textBox.setAcceptRichText(bool(0))
butOK = QtGui.QPushButton(u"去除行号")
butLeft = QtGui.QPushButton(u"去除左侧一个字符")
layout = QtGui.QHBoxLayout()
layout.addWidget(textBox)
layout.addWidget(butOK,alignment = QtCore.Qt.AlignTop)
layout.addWidget(butLeft,alignment = QtCore.Qt.AlignTop)
window.setLayout(layout)
def CutLineNum():
multiStr = unicode(textBox.toPlainText()).splitlines(1)
outStr = u''
for singleStr in multiStr:
singleStr = singleStr.lstrip()
i=0
for charStr in singleStr:
if charStr.isdigit():
i += 1
elif i>0:
singleStr = singleStr[i:]
break
else:
break
outStr += singleStr
textBox.setText(outStr)
def CutLeftChar():
outStr = u''
multiStr = unicode(textBox.toPlainText()).splitlines(1)
for singleStr in multiStr:
singleStr = singleStr[1:]
outStr += singleStr
textBox.setText(outStr)
QtCore.QObject.connect(butOK,QtCore.SIGNAL("clicked()"),CutLineNum)
QtCore.QObject.connect(butLeft,QtCore.SIGNAL("clicked()"),CutLeftChar)
window.show()
sys.exit(app.exec_())
使用方法很简单:把代码粘贴到文本框中,然后点“去除行号”,再点“去除左侧一个字符”就好了,对于大多数1. 2、3: 等开头的代码就没问题了。个别代码比较BT的,比如(1) [2] #3 等开头的,就先点“去除左侧一个字符”,再重复前面操作就OK了。
附:《从C#到Python》系列连载目录
从C#到Python —— 谈谈我学习Python一周来的体会
从C#到Python —— 0 前言:进入Python的世界
从C#到Python —— 1 变量和数据类型
从C#到Python —— 2 运算符、表达式和流程控制
从C#到Python —— 3 函数及函数编程
从C#到Python —— 4 类及面向对象
从C#到Python —— 5 模块和包
New! 从C#到Python —— PDF整理版(下载地址)