如何分析交易記錄?
阿新 • • 發佈:2020-09-22
詞頻統計 SPEC
此作業的要求參見https://edu.cnblogs.com/campus/nenu/2020Fall/homework/11206
該程式託管地址為https://gongwf662.coding.net/public/homework/gongwf/git
功能1
重點:需要通過命令列將資料讀入,然後對單詞切分去重。
難點:對使用pyton進行命令列引數輸入及檔案讀取不熟悉,正則表示式不熟悉。
程式碼片段:
(1)統計輸出單詞
1 def count_word(lines): 2 words_list = re.compile(r'\b[a-zA-Z]+\b', re.I).findall(lines)3 unique_words = list(filter(None, words_list)) 4 text = Counter(unique_words) 5 print('total', len(text)) 6 text = text.most_common() 7 print('\n') 8 formation = "{0:<%ds}{1:<5d}" % 10 9 for i in text: 10 print(formation.format(i[0], i[1]))
(2)檔案讀入
1def read_file(filename): 2 if os.path.isfile(filename): 3 with open(filename, encoding='utf-8') as f: 4 lines = f.read().lower() 5 count_word(lines) 6 else: 7 print("Your input is wrong!")
執行截圖:
功能2
重點:判斷引數個數和型別,處理副檔名。
難點:無。
程式碼片段:
(1)判斷引數型別
1 def get_commend(argv): 2 if len(argv) == 2 and argv[0] == "-s": 3 read_file(argv[1]) 4 elif len(argv) == 1 and (not os.path.isdir(argv[0])): 5 read_file(argv[0] + '.txt') 6 else: 7 print("No files found!")
執行截圖:
功能3
重點:判斷引數型別,將資料夾的txt檔案遍歷讀取。
難點:遍歷資料夾。
程式碼片段:
(1)判斷引數型別
1 def get_commend(argv): 2 if len(argv) == 2 and argv[0] == "-s": 3 read_file(argv[1]) 4 elif len(argv) == 1 and (not os.path.isdir(argv[0])): 5 read_file(argv[0] + '.txt') 6 elif len(argv) == 1 and os.path.isdir(argv[0]): 7 read_dir(argv[0]) 8 else: 9 print("No files found!")
(2)遍歷處理資料夾中的檔案
1 def read_dir(file_dir): 2 dir_list = os.listdir(file_dir) 3 for i in dir_list: 4 with open(file_dir + '/' + i) as f: 5 novel = f.read().lower() 6 print(i) 7 count_word(novel) 8 print('----')
執行截圖:
(1)顯示資料夾中的檔案
(2)遍歷處理結果
功能4
重點:判斷引數型別,重定向輸入
難點:重定向
程式碼片段:
(1)判斷引數型別並讀入資料
1 def get_commend(argv): 2 if len(argv) == 2 and argv[0] == "-s": 3 read_file(argv[1]) 4 elif len(argv) == 1 and argv[0] == "-s": 5 # 重定向 6 novel = sys.stdin.read() 7 count_word(novel.lower()) 8 elif len(argv) == 1 and (not os.path.isdir(argv[0])): 9 read_file(argv[0] + '.txt') 10 elif len(argv) == 1 and os.path.isdir(argv[0]): 11 read_dir(argv[0]) 12 else: 13 print("No files found!")
執行截圖:
PSP
功能 |
預計花費時間(min) |
實際花費時間(min) |
時間差(min) |
原因 |
功能1 |
80 |
103 |
23 |
對python的不熟悉和對題目要求不熟悉。 |
功能2 |
60 |
46 |
-14 |
有第一個功能的經驗後,困難減少。 |
功能3 |
60 |
106 |
46 |
對目錄操作的不熟悉。 |
功能4 |
80 |
123 |
43 |
對重定向的不熟悉。 |
測試 |
30 |
46 |
16 |
需要調參和生成.exe檔案。 |
程式碼及版本控制
該程式託管地址為https://gongwf662.coding.net/public/homework/gongwf/git