写了一个很简单的字符串处理程序。因为只是自己用,而且就那几条规则,所以没太考虑性能。
没有注释(我自己写的,懒得注释)
程序本体
from fun import get_data
from fun import get_num
from tkinter import *
import os
def ins():
print("你点击了按钮")
l2.delete(0.0, 'end')
var = l1.get(0.0, 'end')
data = get_data(var)
for i in range(0, len(data)):
get_char = data[i]
if i == (len(data) - 1):
get_char = get_char[0:len(get_char) - 1]
l2.insert('end', get_char)
l2.insert('end', '\n')
data = get_num(var)
for i in range(0, len(data)):
get_char = data[i]
if i == (len(data) - 1):
get_char = get_char[0:len(get_char) - 1]
l2.insert('end', get_char)
l2.insert('end', '\n')
def into_file():
f = open('F:\log.txt', 'a+')
f.seek(0,2)
data1= l2.get(0.0,'end') + "symbol"
f.write(data1)
f.close()
def get_file():
f = open('F:\log.txt', 'a+')
f.seek(0,0)
data=f.read()
data=data.split('symbol')
#获取最后一个字符串,就是最新的日志
data=data[len(data)-2]
l1.delete(0.0, 'end')
l1.insert('end',data)
f.close()
win = Tk()
win.title('文本整理 @青碧凝霜')
Label(win, text='待修改文本').grid(row=0, sticky=W)
l1 = Text(win)
l1.grid(row=0, column=1, sticky=E)
Label(win, text='处理后文本').grid(row=1, sticky=W)
l2 = Text(win)
l2.grid(row=1, column=1, sticky=E)
Button(win, text='更新', width=80, command=ins).grid(row=4, column=1, sticky=W)
Button(win, text='生成更新后日志', width=80, command=into_file).grid(row=2, column=1, sticky=W)
Button(win, text='读取上一次日志', width=80, command=get_file).grid(row=3, column=1, sticky=W)
win.mainloop()
主要的帅选规则的话是在fun.py,我自己写了两个规则。
第一个规则是get_char,第二个规则是get_num。
get_char是筛选一个特定规律的文本并加入冒号逗号
get_num的规则就是筛选小于9位数的字符串并加入冒号逗号。
# 循环获取字符串子程序
#这个数据是list,所有数据格式都有逗号和分号
def get_data(v1):
var=v1
var = var.upper()
var = var + ' '
var=var.replace('——','-')
data_list=[]
find_var=0
end_var=0
for i in range(0, 200):
find_var = var.find('SD')
if find_var == -1:
break
else:
for j in range(find_var, len(var)):
if (var[j] == 'S' or var[j] == 'D' or var[j] == '0' or var[j] == '1' or var[j] == '2' or var[
j] == '3' or var[j] == '4' or var[j] == '5' or var[j] == '6' or var[j] == '7' or var[j] == '8' or
var[j] == '9' or var[j] == '-' or var[j] == ' ' or var[j] == ' ' or var[j] == ' '):
end_var = j
else:
end_var = j
break
if var[j]== 'S' and j>find_var+10:
break
var_=var[find_var:end_var]
var_=var_.replace(' ','')
data_list.append('\'' + var_ + '\',')
var = var[end_var:len(var)]
return data_list
def get_num(var):
var = var.upper()
var=var.replace(' ','')
var = var + ' '
var_ = ''
var__ = []
for i in range(0, len(var)):
if (var[i] == '-' or var[i] == '1' or var[i] == '2' or var[i] == '3' or var[i] == '4' or var[i] == '5' or var[
i] == '6' or var[i] == '7' or var[i] == '8' or var[i] == '9' or var[i] == '0'):
var_ = var_ + var[i]
else:
var_ = var_ + ' '
print(var_)
print(var_.find(" "))
while var_.find(' ') != -1:
var_ = var_.replace(' ', ' ')
var_ = var_.split(' ')
for i in range(0, len(var_)):
if var_[i] != '' and len(var_[i]) > 6:
tmp=var_[i]
get_sym=1
for j in range(0,len(var_[i])):
if tmp[j]=="-" and j>2:
get_sym=0
if get_sym==1:
var__.append('\''+var_[i]+'\',')
print(var__)
return var__
有PYTHON基础的可以在我基础上写一个自己的规则,可以省点心。
就是这样。