askopenfilename

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')
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章