import re
import os
import tkinter
root = tkinter.Tk()
ifile = tkinter.filedialog.askopenfilename(filetypes = [('TXT', 'txt')])
root.destroy()
ofile = ifile + 'out.txt'
if os.path.exists(ofile):
print(ofile + '已存在,请删除后再运行')
exit(0)
pattern = '(\w+) (\w+).*?本月剩余流量([\d.]+)MB'
ans = []
class T:
def __init__(self, num, line):
self.num = num
self.line = line
num = 0.0
line = ''
with open(ifile, 'r') as f:
while True:
line = f.readline()
if len(line) == 0:
break
a = re.match(pattern, line)
ans.append(T(float(a.group(3)), a.group(1) + ' ' + a.group(2) + ' 剩余流量:' + a.group(3) + 'MB'))
ans.sort(key = lambda a: a.num)
ans.reverse()
for i in ans:
print(i.line)
#with open(ofile, 'a') as f:
#for i in ans:
# f.write(i.line + '\n')
print('success')