第12課 習題講解
阿新 • • 發佈:2018-12-03
Python 程式設計作業 02
請實現一個程式,實現如下需求點
1.程式開始的時候提示使用者輸入學生年齡資訊 格式如下:
Jack Green , 21 ; Mike Mos, 9; 我們假設 使用者輸入 上面的資訊,必定會遵守下面的規則: 學生資訊之間用分號隔開(分號前後可能有不定數量的空格), 每個學生資訊裡的 姓名和 年齡之間用 逗號隔開(逗號前後可能有不定數量的空格) 2. 程式隨後將輸入的學生資訊分行顯示,格式如下 Jack Green : 21; Mike Mos : 09; 學生的姓名要求左對齊,寬度為20, 年齡資訊右對齊,寬度為2位,不足前面補零
#--------------------------------------------------------------------------------------------------------------------------------#
需求分析:
1、輸入---input('')、返回的型別是---str
2、要求的輸入格式:Jack Green, 21; Mike Mos,9;
1)可以輸入多個學生資訊,用;隔開
2)單個學生的名字和年齡用逗號(,)隔開
3)逗號和分號的數量是一致的
4)空格的數量是不固定的
3、注意事項:如果使用者不按格式輸入:1、提示,格式錯誤;2、可以再次輸入
4、輸出格式:
Jack Green : 21;
Mike Mos : 09;
思路分解:
1、提示使用者輸入:input()
2、對輸入的內容進行過濾(資料處理),做出相應的提示!
3、獲取name age;
while True: info = input('請輸入學生的資訊:名字, 年齡;') if ',' and ';' in info: if info.count(',') == info.count(';'): temp= info.split(';') for one in temp: if one != '' and ',' in one: name ,age = one.split(',') name = name.strip() # 去空格 age = int(age.strip()) # 去空格 --年齡轉換為int型 print('{:<20}:{:>2}'.format(name, age)) break else: print('您輸入的格式不對:,與;的數量不一致!') else: print('您輸入的格式有誤!') # 執行結果 請輸入學生的資訊:名字, 年齡;tom, 21; tom :21 虛擬碼 while True: 先定義個死迴圈,讓使用者能多次輸入 info = input('請按照格式輸入你的姓名和年齡: 姓名, 年齡;') 按照格式在控制檯輸入姓名和年齡 if ',' and ';' in info: 判斷輸入內容是否合法(是否包含','和';') if info.count(',') == info.count(';') 進一步判斷輸入是否合法,逗號和分號是否成對出現,不然就不合法 temp = info.split(';') 先按照分號進行初步分割 for one in temp: 取列表裡面的元素 if one != '' and ',' in one: 條件判斷,如果列表裡無空值並且有逗號,根據前面的條件,逗號和分號是成對出現,所以滿足one裡面同時包含逗號和分號(為什麼用逗號,因為前面分號已經切割過了) name, age = one.split(',') # 使用逗號分隔 name = one.strip() # 去掉元素兩邊的空格 age = int(one.strip()) # 去掉元素兩邊的空格,並且把年齡轉換為int型 print('{:<20}:{:>02}'.format(name, age)) 打印出結果 break 如果for迴圈執行完畢則跳出迴圈 else: print('你輸入的內容不合法--,和;的數量不一致') 如果逗號和分號的數量不一致,則報錯 else: print('你輸入的內容不合法,請重新輸入!') 如果逗號和分號沒有同時出現,則報錯輸入不合法
請再實現一個程式,實現如下需求點
1.下面的log變數記錄了雲伺服器上 當天上傳的檔案資訊
其中第一列是檔名,第二列是檔案大小
請編寫一個程式,統計出不同型別的 檔案的大小總和
比如:
jpeg 9988999
json 324324
png 2423233
----------------------------------
log = ''' f20180111014341/i_51a7hC3W.jpeg 169472 FrITJxleSP7wUD-MWw-phL_KP6Eu 15156063244230469 image/jpeg 0 f20180111014341/j_R0Hpl4EG.json 1036 ForGzwzV3e-uR3_UzvppJs1VgfQG 15156064773253144 application/json 0 f20180111020739/i_0TDKs0rD.jpeg 169472 FrITJxleSP7wUD-MWw-phL_KP6Eu 15156076847077556 image/jpeg 0 f20180111020739/j_JFO6xiir.json 1040 FmUhTchdLOd7LBoE8OXzPLDKcW60 15156077904192983 application/json 0 f20180111090619/i_1BwNksbL.jpg 49634 FtXBGmipcDha-67WQgGQR5shEBu2 15156329458714950 image/jpeg 0 f20180111090619/i_3BKlsRaZ.jpg 30152 FoWfMSuqz4TEQl5FT-FY5wqu5NGf 15156330575626044 image/jpeg 0 f20180111090619/i_5XboXSKh.jpg 40238 Fl84WaBWThHovIBsQaNFoIaPZcWh 15156329453409855 image/jpeg 0 f20180111090619/i_6DiYSBKp.jpg 74017 FrYG3icChRmFGnWQK6rYxa88KuQI 15156329461803290 image/jpeg 0 f20180111090619/i_76zaF2IM.jpg 38437 Fui8g5OrJh0GQqZzT9wtepfq99lJ 15156334738356648 image/jpeg 0 f20180111090619/i_B6TFYjks.jpg 37953 FleWqlK2W1ZmEgAatAEcm1gpR0kC 15156329464034474 image/jpeg 0 f20180111090619/i_N9eITqj3.jpg 38437 Fui8g5OrJh0GQqZzT9wtepfq99lJ 15156330419595764 image/jpeg 0 f20180111090619/i_QTSNWmA6.jpg 37953 FleWqlK2W1ZmEgAatAEcm1gpR0kC 15156333104224056 image/jpeg 0 f20180111090619/i_XdHcAfh1.jpg 56479 FjLQIQ3GxSEHDfu6tRcMylK1MZ05 15156334227270309 image/jpeg 0 f20180111090619/i_Xyy723MU.jpg 50076 FsfZpQzqu084RUw5NPYW9-Yfam_R 15156334229987458 image/jpeg 0 f20180111090619/i_d8Go0EOv.jpg 30152 FoWfMSuqz4TEQl5FT-FY5wqu5NGf 15156334736228515 image/jpeg 0 f20180111090619/i_diuHmX53.jpg 40591 FuTx1pw4idbKnV5MSvNGxCA5L470 15156333878320713 image/jpeg 0 f20180111090619/i_qQKzheSH.jpg 55858 Fj0A3i8V7fzzOiPQFL79ao15hkN9 15156329456666591 image/jpeg 0 f20180111090619/i_rHL5SYk8.jpg 40238 Fl84WaBWThHovIBsQaNFoIaPZcWh 15156336509742181 image/jpeg 0 f20180111090619/i_xZmQxUbz.jpg 40238 Fl84WaBWThHovIBsQaNFoIaPZcWh 15156333240603466 image/jpeg 0 f20180111090619/i_zBDNgXDv.jpeg 73616 FlgNwq8lypgsxrWs_ksrS_x47SQV 15156334232887875 image/jpeg 0 f20180111090619/j_4mxbEiVh.json 2990 Fpq-3yl3Yr1CadNrJVSDnpeRhQtT 15156331445226898 application/json 0 f20180111090619/j_i1K74768.json 3042 Fl5PpDw1TsZXMuhoq1RUrOeGZ6br 15156335067090003 application/json 0 f20180111095839/i_Q7KMKeda.png 518522 Fl-yB1_ruL2uxZN9k7DjB62h9dYH 15156359599713253 image/png 0 f20180111095839/j_5DpqHolV.json 184 FoYvi7cmSrzuVjUgCRzW5kU95SVo 15156359719719064 application/json 0 f20180111100442/i_No8kToIV.jpg 48975 Fu1cw3f--5Vpz9kLGeJfvljhCtyZ 15156364349642377 image/jpeg 0 f20180111100442/i_P1bkvSeg.jpg 68200 FvYe8vi46TjUKhEy_UwDqLhO6ZsW 15156363800690634 image/jpeg 0 f20180111100442/i_T1AulKcD.jpg 52641 Fj2YzvdC1n_1sF93ZZgrhF3OzOeY 15156364021186365 image/jpeg 0 f20180111100442/i_X8d8BN07.jpg 50770 FivwidMiHbogw77lqgkIKrgmF3eA 15156363969737156 image/jpeg 0 f20180111100442/i_g0wtOsCX.jpg 76656 Fmtixx0mP9CAUTNosjLuYQHL6k0P 15156363448222155 image/jpeg 0 f20180111100442/i_h5OT9324.jpg 72672 FvbIqPLTh2cQHTIBv2akUfahZa_Z 15156364401354652 image/jpeg 0 f20180111100442/i_he8iLYI6.jpg 49399 FjeJvwjwhU-hKZsq66UoBg9_tEJs 15156363907932480 image/jpeg 0 f20180111100442/i_kg29t7Pp.jpg 76293 FuYj__sSeEN7AsXMbxO24Z8Suh8d 15156364156384686 image/jpeg 0 f20180111100442/i_oz1YoBI1.jpg 75620 FkY3xsUMwOI01zgoH1iXXgiQeq6I 15156364089112904 image/jpeg 0 f20180111100442/i_xrOT98on.jpg 50021 Fql7ookM1Rc6V7VairKAfnKe-o9w 15156363856357316 image/jpeg 0 f20180111135114/i_Zqt8Tmoe.png 161629 FlELw59_mV3VqDBLyu1BKN4fIWnx 15156500155209863 image/png 0 f20180111135114/j_uhHoMXKq.json 159 FrypljwAr2LgoLAePBNTUYTUAgDt 15156500200488238 application/json 0 f20180111142119/i_s83iZ2GR.png 92278 Fns8tdh3JCkRmfE_COYEu4o8w03E 15156517082371259 image/png 0 f20180111142119/j_0g45JRth.json 159 Fq1rFwdRguYRXrp61nGZ5TsUG1V- 15156517143375596 application/json 0 f20180111144306/i_yE5TC84E.png 139230 Fjf61ymabEnEvnr5ZMHFjXGCrYlP 15156530038824150 image/png 0 f20180111144306/j_OF4WVtSH.json 159 FqwkKcxfo8jd0jFUyuH4X2CrnE9q 15156530083419530 application/json 0 f20180111150230/i_KtnER4g3.png 120044 FuwOWdrqzcr2-UScem-LzEMgMezs 15156541734892258 image/png 0 f20180111150230/j_xMSUEejY.json 158 FjJr_4deMqFphGaptm-2Pa6wwRP2 15156541771989216 application/json 0 f20180111151741/i_JuSWztB3.jpg 92506 FrIjRevHSi6xv4-NQa2wrHu5a1zQ 15156550875370965 image/jpeg 0 f20180111153550/i_9wWzVenl.gif 769872 FvslKY9JUaCQm-lu02E34tvAP_oG 15156561674621628 image/gif 0 '''
需求分析:
1、處理一個log
2、把相同格式的檔案,統計位元組大小
3、要求的格式:
jpeg 9988999
json 324324
png 2423233
思路分析
1、獲取每一行的日誌資訊的 檔案 + 大小
1)可以使用print檢視是否存在換行---\n
2、對相同格式檔案進行累加
1)size獲取的時候是str型別,累加要用int型別
2)建立一個函式,做累加。
print(log) # 檢視日誌的輸出格式。三引號裡的內容都有換行 # 輸出結果 f20180111014341/i_51a7hC3W.jpeg 169472 FrITJxleSP7wUD-MWw-phL_KP6Eu 15156063244230469 image/jpeg 0 f20180111014341/j_R0Hpl4EG.json 1036 ForGzwzV3e-uR3_UzvppJs1VgfQG 15156064773253144 application/json 0 f20180111020739/i_0TDKs0rD.jpeg 169472 FrITJxleSP7wUD-MWw-phL_KP6Eu 15156076847077556 image/jpeg 0 f20180111020739/j_JFO6xiir.json 1040 FmUhTchdLOd7LBoE8OXzPLDKcW60 15156077904192983 application/json 0 f20180111090619/i_1BwNksbL.jpg 49634 FtXBGmipcDha-67WQgGQR5shEBu2 15156329458714950 image/jpeg 0 f20180111090619/i_3BKlsRaZ.jpg 30152 FoWfMSuqz4TEQl5FT-FY5wqu5NGf 15156330575626044 image/jpeg 0 f20180111090619/i_5XboXSKh.jpg 40238 Fl84WaBWThHovIBsQaNFoIaPZcWh 15156329453409855 image/jpeg 0 f20180111090619/i_6DiYSBKp.jpg 74017 FrYG3icChRmFGnWQK6rYxa88KuQI 15156329461803290 image/jpeg 0 f20180111090619/i_76zaF2IM.jpg 38437 Fui8g5OrJh0GQqZzT9wtepfq99lJ 15156334738356648 image/jpeg 0 f20180111090619/i_B6TFYjks.jpg 37953 FleWqlK2W1ZmEgAatAEcm1gpR0kC 15156329464034474 image/jpeg 0 f20180111090619/i_N9eITqj3.jpg 38437 Fui8g5OrJh0GQqZzT9wtepfq99lJ 15156330419595764 image/jpeg 0 f20180111090619/i_QTSNWmA6.jpg 37953 FleWqlK2W1ZmEgAatAEcm1gpR0kC 15156333104224056 image/jpeg 0 f20180111090619/i_XdHcAfh1.jpg 56479 FjLQIQ3GxSEHDfu6tRcMylK1MZ05 15156334227270309 image/jpeg 0 f20180111090619/i_Xyy723MU.jpg 50076 FsfZpQzqu084RUw5NPYW9-Yfam_R 15156334229987458 image/jpeg 0 f20180111090619/i_d8Go0EOv.jpg 30152 FoWfMSuqz4TEQl5FT-FY5wqu5NGf 15156334736228515 image/jpeg 0 f20180111090619/i_diuHmX53.jpg 40591 FuTx1pw4idbKnV5MSvNGxCA5L470 15156333878320713 image/jpeg 0 f20180111090619/i_qQKzheSH.jpg 55858 Fj0A3i8V7fzzOiPQFL79ao15hkN9 15156329456666591 image/jpeg 0 f20180111090619/i_rHL5SYk8.jpg 40238 Fl84WaBWThHovIBsQaNFoIaPZcWh 15156336509742181 image/jpeg 0 f20180111090619/i_xZmQxUbz.jpg 40238 Fl84WaBWThHovIBsQaNFoIaPZcWh 15156333240603466 image/jpeg 0 f20180111090619/i_zBDNgXDv.jpeg 73616 FlgNwq8lypgsxrWs_ksrS_x47SQV 15156334232887875 image/jpeg 0 f20180111090619/j_4mxbEiVh.json 2990 Fpq-3yl3Yr1CadNrJVSDnpeRhQtT 15156331445226898 application/json 0 f20180111090619/j_i1K74768.json 3042 Fl5PpDw1TsZXMuhoq1RUrOeGZ6br 15156335067090003 application/json 0 f20180111095839/i_Q7KMKeda.png 518522 Fl-yB1_ruL2uxZN9k7DjB62h9dYH 15156359599713253 image/png 0 f20180111095839/j_5DpqHolV.json 184 FoYvi7cmSrzuVjUgCRzW5kU95SVo 15156359719719064 application/json 0 f20180111100442/i_No8kToIV.jpg 48975 Fu1cw3f--5Vpz9kLGeJfvljhCtyZ 15156364349642377 image/jpeg 0 f20180111100442/i_P1bkvSeg.jpg 68200 FvYe8vi46TjUKhEy_UwDqLhO6ZsW 15156363800690634 image/jpeg 0 f20180111100442/i_T1AulKcD.jpg 52641 Fj2YzvdC1n_1sF93ZZgrhF3OzOeY 15156364021186365 image/jpeg 0 f20180111100442/i_X8d8BN07.jpg 50770 FivwidMiHbogw77lqgkIKrgmF3eA 15156363969737156 image/jpeg 0 f20180111100442/i_g0wtOsCX.jpg 76656 Fmtixx0mP9CAUTNosjLuYQHL6k0P 15156363448222155 image/jpeg 0 f20180111100442/i_h5OT9324.jpg 72672 FvbIqPLTh2cQHTIBv2akUfahZa_Z 15156364401354652 image/jpeg 0 f20180111100442/i_he8iLYI6.jpg 49399 FjeJvwjwhU-hKZsq66UoBg9_tEJs 15156363907932480 image/jpeg 0 f20180111100442/i_kg29t7Pp.jpg 76293 FuYj__sSeEN7AsXMbxO24Z8Suh8d 15156364156384686 image/jpeg 0 f20180111100442/i_oz1YoBI1.jpg 75620 FkY3xsUMwOI01zgoH1iXXgiQeq6I 15156364089112904 image/jpeg 0 f20180111100442/i_xrOT98on.jpg 50021 Fql7ookM1Rc6V7VairKAfnKe-o9w 15156363856357316 image/jpeg 0 f20180111135114/i_Zqt8Tmoe.png 161629 FlELw59_mV3VqDBLyu1BKN4fIWnx 15156500155209863 image/png 0 f20180111135114/j_uhHoMXKq.json 159 FrypljwAr2LgoLAePBNTUYTUAgDt 15156500200488238 application/json 0 f20180111142119/i_s83iZ2GR.png 92278 Fns8tdh3JCkRmfE_COYEu4o8w03E 15156517082371259 image/png 0 f20180111142119/j_0g45JRth.json 159 Fq1rFwdRguYRXrp61nGZ5TsUG1V- 15156517143375596 application/json 0 f20180111144306/i_yE5TC84E.png 139230 Fjf61ymabEnEvnr5ZMHFjXGCrYlP 15156530038824150 image/png 0 f20180111144306/j_OF4WVtSH.json 159 FqwkKcxfo8jd0jFUyuH4X2CrnE9q 15156530083419530 application/json 0 f20180111150230/i_KtnER4g3.png 120044 FuwOWdrqzcr2-UScem-LzEMgMezs 15156541734892258 image/png 0 f20180111150230/j_xMSUEejY.json 158 FjJr_4deMqFphGaptm-2Pa6wwRP2 15156541771989216 application/json 0 f20180111151741/i_JuSWztB3.jpg 92506 FrIjRevHSi6xv4-NQa2wrHu5a1zQ 15156550875370965 image/jpeg 0 f20180111153550/i_9wWzVenl.gif 769872 FvslKY9JUaCQm-lu02E34tvAP_oG 15156561674621628 image/gif 0
三引號裡面的內容,用\n來切分,首尾都時空字串
print(log.split('\n')) # 使用\n來切分,發現首尾都是空字串 #輸出結果 ['', 'f20180111014341/i_51a7hC3W.jpeg\t169472\tFrITJxleSP7wUD-MWw-phL_KP6Eu\t15156063244230469\timage/jpeg\t0\t', 'f20180111014341/j_R0Hpl4EG.json\t1036\tForGzwzV3e-uR3_UzvppJs1VgfQG\t15156064773253144\tapplication/json\t0\t', 'f20180111020739/i_0TDKs0rD.jpeg\t169472\tFrITJxleSP7wUD-MWw-phL_KP6Eu\t15156076847077556\timage/jpeg\t0\t', 'f20180111020739/j_JFO6xiir.json\t1040\tFmUhTchdLOd7LBoE8OXzPLDKcW60\t15156077904192983\tapplication/json\t0\t', 'f20180111090619/i_1BwNksbL.jpg\t49634\tFtXBGmipcDha-67WQgGQR5shEBu2\t15156329458714950\timage/jpeg\t0\t', 'f20180111090619/i_3BKlsRaZ.jpg\t30152\tFoWfMSuqz4TEQl5FT-FY5wqu5NGf\t15156330575626044\timage/jpeg\t0\t', 'f20180111090619/i_5XboXSKh.jpg\t40238\tFl84WaBWThHovIBsQaNFoIaPZcWh\t15156329453409855\timage/jpeg\t0\t', 'f20180111090619/i_6DiYSBKp.jpg\t74017\tFrYG3icChRmFGnWQK6rYxa88KuQI\t15156329461803290\timage/jpeg\t0\t', 'f20180111090619/i_76zaF2IM.jpg\t38437\tFui8g5OrJh0GQqZzT9wtepfq99lJ\t15156334738356648\timage/jpeg\t0\t', 'f20180111090619/i_B6TFYjks.jpg\t37953\tFleWqlK2W1ZmEgAatAEcm1gpR0kC\t15156329464034474\timage/jpeg\t0\t', 'f20180111090619/i_N9eITqj3.jpg\t38437\tFui8g5OrJh0GQqZzT9wtepfq99lJ\t15156330419595764\timage/jpeg\t0\t', 'f20180111090619/i_QTSNWmA6.jpg\t37953\tFleWqlK2W1ZmEgAatAEcm1gpR0kC\t15156333104224056\timage/jpeg\t0\t', 'f20180111090619/i_XdHcAfh1.jpg\t56479\tFjLQIQ3GxSEHDfu6tRcMylK1MZ05\t15156334227270309\timage/jpeg\t0\t', 'f20180111090619/i_Xyy723MU.jpg\t50076\tFsfZpQzqu084RUw5NPYW9-Yfam_R\t15156334229987458\timage/jpeg\t0\t', 'f20180111090619/i_d8Go0EOv.jpg\t30152\tFoWfMSuqz4TEQl5FT-FY5wqu5NGf\t15156334736228515\timage/jpeg\t0\t', 'f20180111090619/i_diuHmX53.jpg\t40591\tFuTx1pw4idbKnV5MSvNGxCA5L470\t15156333878320713\timage/jpeg\t0\t', 'f20180111090619/i_qQKzheSH.jpg\t55858\tFj0A3i8V7fzzOiPQFL79ao15hkN9\t15156329456666591\timage/jpeg\t0\t', 'f20180111090619/i_rHL5SYk8.jpg\t40238\tFl84WaBWThHovIBsQaNFoIaPZcWh\t15156336509742181\timage/jpeg\t0\t', 'f20180111090619/i_xZmQxUbz.jpg\t40238\tFl84WaBWThHovIBsQaNFoIaPZcWh\t15156333240603466\timage/jpeg\t0\t', 'f20180111090619/i_zBDNgXDv.jpeg\t73616\tFlgNwq8lypgsxrWs_ksrS_x47SQV\t15156334232887875\timage/jpeg\t0\t', 'f20180111090619/j_4mxbEiVh.json\t2990\tFpq-3yl3Yr1CadNrJVSDnpeRhQtT\t15156331445226898\tapplication/json\t0\t', 'f20180111090619/j_i1K74768.json\t3042\tFl5PpDw1TsZXMuhoq1RUrOeGZ6br\t15156335067090003\tapplication/json\t0\t', 'f20180111095839/i_Q7KMKeda.png\t518522\tFl-yB1_ruL2uxZN9k7DjB62h9dYH\t15156359599713253\timage/png\t0\t', 'f20180111095839/j_5DpqHolV.json\t184\tFoYvi7cmSrzuVjUgCRzW5kU95SVo\t15156359719719064\tapplication/json\t0\t', 'f20180111100442/i_No8kToIV.jpg\t48975\tFu1cw3f--5Vpz9kLGeJfvljhCtyZ\t15156364349642377\timage/jpeg\t0\t', 'f20180111100442/i_P1bkvSeg.jpg\t68200\tFvYe8vi46TjUKhEy_UwDqLhO6ZsW\t15156363800690634\timage/jpeg\t0\t', 'f20180111100442/i_T1AulKcD.jpg\t52641\tFj2YzvdC1n_1sF93ZZgrhF3OzOeY\t15156364021186365\timage/jpeg\t0\t', 'f20180111100442/i_X8d8BN07.jpg\t50770\tFivwidMiHbogw77lqgkIKrgmF3eA\t15156363969737156\timage/jpeg\t0\t', 'f20180111100442/i_g0wtOsCX.jpg\t76656\tFmtixx0mP9CAUTNosjLuYQHL6k0P\t15156363448222155\timage/jpeg\t0\t', 'f20180111100442/i_h5OT9324.jpg\t72672\tFvbIqPLTh2cQHTIBv2akUfahZa_Z\t15156364401354652\timage/jpeg\t0\t', 'f20180111100442/i_he8iLYI6.jpg\t49399\tFjeJvwjwhU-hKZsq66UoBg9_tEJs\t15156363907932480\timage/jpeg\t0\t', 'f20180111100442/i_kg29t7Pp.jpg\t76293\tFuYj__sSeEN7AsXMbxO24Z8Suh8d\t15156364156384686\timage/jpeg\t0\t', 'f20180111100442/i_oz1YoBI1.jpg\t75620\tFkY3xsUMwOI01zgoH1iXXgiQeq6I\t15156364089112904\timage/jpeg\t0\t', 'f20180111100442/i_xrOT98on.jpg\t50021\tFql7ookM1Rc6V7VairKAfnKe-o9w\t15156363856357316\timage/jpeg\t0\t', 'f20180111135114/i_Zqt8Tmoe.png\t161629\tFlELw59_mV3VqDBLyu1BKN4fIWnx\t15156500155209863\timage/png\t0\t', 'f20180111135114/j_uhHoMXKq.json\t159\tFrypljwAr2LgoLAePBNTUYTUAgDt\t15156500200488238\tapplication/json\t0\t', 'f20180111142119/i_s83iZ2GR.png\t92278\tFns8tdh3JCkRmfE_COYEu4o8w03E\t15156517082371259\timage/png\t0\t', 'f20180111142119/j_0g45JRth.json\t159\tFq1rFwdRguYRXrp61nGZ5TsUG1V-\t15156517143375596\tapplication/json\t0\t', 'f20180111144306/i_yE5TC84E.png\t139230\tFjf61ymabEnEvnr5ZMHFjXGCrYlP\t15156530038824150\timage/png\t0\t', 'f20180111144306/j_OF4WVtSH.json\t159\tFqwkKcxfo8jd0jFUyuH4X2CrnE9q\t15156530083419530\tapplication/json\t0\t', 'f20180111150230/i_KtnER4g3.png\t120044\tFuwOWdrqzcr2-UScem-LzEMgMezs\t15156541734892258\timage/png\t0\t', 'f20180111150230/j_xMSUEejY.json\t158\tFjJr_4deMqFphGaptm-2Pa6wwRP2\t15156541771989216\tapplication/json\t0\t', 'f20180111151741/i_JuSWztB3.jpg\t92506\tFrIjRevHSi6xv4-NQa2wrHu5a1zQ\t15156550875370965\timage/jpeg\t0\t', 'f20180111153550/i_9wWzVenl.gif\t769872\tFvslKY9JUaCQm-lu02E34tvAP_oG\t15156561674621628\timage/gif\t0\t', '']
程式碼如下
outList = [] def sizeAdd(fileType, fileSize): # 形參 for one in outList: if fileType == one[0]: # 說明有這個格式的檔案 one[1] += fileSize return outList.append([fileType,fileSize]) for line in log.split('\n'): if line != '': temp = line.split('\t') fileName = temp[0].strip().split('.')[1].strip() size = int(temp[1].strip()) # 檔案大小 sizeAdd(fileName, size) print(outList) # 執行結果 [['jpeg', 412560], ['json', 8927], ['jpg', 1374206], ['png', 1031703], ['gif', 769872]]
虛擬碼
outList = [] # 虛擬碼 結果儲存為這樣的格式:outList = [[gif, 20000], [png, 10000]] def sizeAdd(fileType, fileSize): # 形參 for one in outList: if fileType == one[0]: # 如果列表中存在此檔案格式 one[1] += fileSize # 則在檔案大小在原來的基礎上累加 return # 結束函式,為什麼不用break。因為break是跳出本次迴圈;而return是結束函式執行 outList.append([fileType, fileSize]) # outList,append()放在開頭位置,意思是for迴圈裡面如果成立,則不執行append(), 反之則執行 # print(log.split('\n')) for line in log.split('\n'): # 按照換行符來切割,得到整個每句內容 if line != '': # 假設條件,子句內容不為空 temp = line.split('\t') # 定義temp, 按照製表符'\t'來切割 fileName = temp[0].split('.')[1].strip() # 定義檔名 fileName,按照.來切割,去掉兩邊空格 size = int(temp[1].strip()) # 檔案大小,去掉空格 #print(fileName, size)
Python 作業 03
請定義一個函式 mySort,引數為一個列表,引數列表中的元素都是整數.
mySort 函式需要將引數列表中的元素按從小到大排序,最終返回一個新的list。
請按下面演算法的思路實現函式:
1. 建立一個新的列表newList
2. 先找出所有元素中最小的,append在newList裡面
3. 再找出剩餘的所有元素中最小的,append在newList裡面
4. 依次類推,直到所有的元素都放到newList裡面
需求分析:
1、mySort 函式需要將列表中的元素按照從小到大排序,最終返回一個新的list。
1)def
2)return
newList = [] # 存結果 def mySort(inList): while len(inList) > 0: minData = inList[0] # 假設法 minIdx = 0 indx = 0 for one in inList: if minData > one: minData = one minIdx = indx indx += 1 inList.pop(minIdx) # pop()函式,括號裡面填的是元素的下標 newList.append(minData) return newList print(mySort([16, 3, 12, 18, 5, 19])) # 執行結果 [3, 5, 12, 16, 18, 19]
Python 作業 04
現有檔案1(如下,請儲存到檔案file1.txt中), 記錄了公司員工的薪資,其內容格式如下
name: Jack ; salary: 12000
name :Mike ; salary: 12300
name: Luk ; salary: 10030
name :Tim ; salary: 9000
name: John ; salary: 12000 name: Lisa ; salary: 11000 每個員工一行,記錄了員工的姓名和薪資, 每行記錄 原始檔案中並不對齊,中間有或多或少的空格 現要求實現一個python程式,計算出所有員工的稅後工資(薪資的90%)和扣稅明細, 以如下格式存入新的檔案 file2.txt中,如下所示 name: Jack ; salary: 12000 ; tax: 1200 ; income: 10800 name: Mike ; salary: 12300 ; tax: 1230 ; income: 11070 name: Luk ; salary: 10030 ; tax: 1003 ; income: 9027 name: Tim ; salary: 9000 ; tax: 900 ; income: 8100 name: John ; salary: 12000 ; tax: 1200 ; income: 10800 name: Lisa ; salary: 11000 ; tax: 1100 ; income: 9900 要求像上面一樣的對齊 tax 表示扣稅金額和 income表示實際收入。注意扣稅金額和 實際收入要取整數
思路分析:
1、讀取檔案內容
2、獲取name,salary
1)讀取每一行資料:fo.read().splitlines()---去掉換行符
inFileDir = 'D://software/HelloWorld/songqin/python/lesson13-14 習題講解/file1.txt' outFileDir = 'D://software/HelloWorld/songqin/python/lesson13-14 習題講解/file2.txt' with open(inFileDir) as rFile, open(outFileDir, 'w') as wFile: for line in rFile.read().splitlines(): if ';' in line: info = line.split(';') if ':'in info[0] and ':'in info[1]: # 這裡的條件之前寫成了if ':' and ':' in info:---這樣的話所有條件都不成立 name = info[0].split(':')[1].strip() salary = int(info[1].split(':')[1].strip()) # 遇到的錯誤,這裡不定義int型,下一步會報錯 out = 'name:{:<8} ; salary:{:>8} ; tax:{:>6} ; income:{:>8}\n'.format(name, salary, int(salary * 0.1), int(salary * 0.9)) # 這裡要加上\n換行 wFile.write(out) else: print('this line is error!\n') 執行結果 name:Jack ; salary: 12000 ; tax: 1200 ; income: 10800 name:Mike ; salary: 12800 ; tax: 1280 ; income: 11520 name:Luk ; salary: 10030 ; tax: 1003 ; income: 9027 name:Tim ; salary: 9000 ; tax: 900 ; income: 8100 name:John ; salary: 12000 ; tax: 1200 ; income: 10800 name:Lisa ; salary: 11000 ; tax: 1100 ; income: 9900
虛擬碼
虛擬碼: 1--定義兩個檔名及檔案路徑 inFileDir = '檔案路徑' outFileDie = '檔案路徑' 2-- 使用with open 可以省略close() with open(inFileDir, 'r'---預設為只讀,可以省略) as rFile, open(outFileDir, 'w') as wFile: 3--讀取每行資料,使用splitlines()可以去掉換行符 for line in rFile.read().splitlines(): 4--按照(; )進行切割,切割之前先進行條件判斷,看line中是否有分號。如果有則執行下一步操作,否則提示錯誤 if ';' in line: info = line.split(';') 5--再按照(:)進行切割,切割之前先進行條件判斷。 salary要轉成int型 if ':' in info[0] and ':' in info[1]: name = info[0].split(':')[1].strip() salary = int(info[1].split(':')[1].strip()) 6--格式化,按照一定格式輸出結果 out = ‘name: {:>6}; salary: {:>8}; tax:{:>8}; income:{:>8}’.format(name, salary, int(salary * 0.1), int(salary * 0.9)) 7--寫入檔案 wflie.write(out)