Python去除每行文字之前的空格

Python去除每行文字之前的空格

PS: 也是从网上各个帖子中学习的Python,因此代码的格式以及内容有粘贴网上其他大神的代码,如有侵权请告知删除

软件制作原因:网上粘贴代码,每行代码前都有序号,例如
1 int a = 0
2 float b = 0
……
代码行数少可以手动,当代码很多,比如1000行时,那手动删除序号就太……

界面示例
Python去除每行文字之前的空格_第1张图片

from tkinter import *
import tkinter.filedialog

root = Tk()
root.title('Delete Head Space')
root.geometry('400x300')
FileName1 = ""
FileName2 = ""

def Button1Anwser():
    global FileName1
    Path = Label(root, text='')
    FileName1 = tkinter.filedialog.askopenfilename()
    Path.config(text=FileName1)
    Path.grid(row=1, column=0, sticky=W)

def Button2Anwser():
    global FileName2
    Path = Label(root, text='')
    FileName2 = tkinter.filedialog.askopenfilename()
    Path.config(text=FileName2)
    Path.grid(row=3, column=0, sticky=W)

def Start():
    OriFile = open(FileName1, 'r')
    NewFile = open(FileName2, 'w')
    TotalLine = OriFile.readlines()

    Text = Label(root, text='')
    Text.config(text='正在处理')
    Text.grid(row=6, column=1)

    for Line in TotalLine:
        NewFile.writelines(Line[int(Num.get()):])
    OriFile.close()
    NewFile.close()

    Text = Label(root, text='')
    Text.config(text='处理完成')
    Text.grid(row=6, column=1)

# -------------------------------------界面---------------------------------
SelectFile1 = Button(root, text="源文件", command=Button1Anwser,width=10, height=1, bg="DarkGray", fg="white")
SelectFile1.grid(row=0, column=0, sticky=W)

SelectFile2 = Button(root, text="目标文件", command=Button2Anwser,width=10, height=1, bg="DarkGray", fg="white")
SelectFile2.grid(row=2, column=0, sticky=W)

Text1 = Label(root, text='每一行首字母的位置')
Text1.grid(row=4, sticky=W)
Num = Entry(root)
Num.grid(row=5, column=0, sticky=W)

ToDeal = Button(root, text="开始处理", command=Start,width=10, height=1, bg="DarkGray", fg="white")
ToDeal.grid(row=6, sticky=W)

Advice1 = Label(root, text='')
Advice1.config(text='网上粘贴代码,每行前面有序号和空格,运行代码前需要去掉此类东西')
Advice1.grid(row=7, column=0, sticky=W)

root.mainloop()

你可能感兴趣的:(Python去除每行文字之前的空格)