標籤:next div pos and 個數 enc 檔案中 file 列印
一:迭代器2 & 裝飾器
#codeing:UTF-8#__author__:Duke#date:2018/3/10/010# 今天講迭代器# 產生器都是迭代器,迭代器不一定是產生器l = [1,2,3,4]data = iter(l)print(data) # <list_iterator object at 0x000002ABBF1E9780>#什麼是迭代器? 1 有iter方法 2 有next方法#練習 找出檔案中,最長的一行file = open("data","r",encoding="UTF-8")maxlength_data = ‘‘def max_line(): while True: data_line = file.readline().strip() print(data_line) if not data_line: break global maxlength_data if len (data_line) > len (maxlength_data): maxlength_data = data_line else: pass yieldfor i in max_line(): Noneprint("==========")print(maxlength_data)file.close()
二:time 模組
# time 模組import timeprint(time.time()) #擷取當前的時間戳記 *****print(help(time)) #擷取模組的協助資訊print(time.clock()) #計算CPU的執行時間print(time.sleep(3)) #睡眠時間 *******print(time.gmtime()) # UTC 世界標準時間 #time.struct_time(tm_year=2018, tm_mon=3, tm_mday=10, tm_hour=12, tm_min=6, tm_sec=56, tm_wday=5, tm_yday=69, tm_isdst=0)# UTC 世界標準時間print(time.localtime()) #本地時間struct_time = time.localtime()print(time.strftime("%Y-%m-%d %H:%M:%S",struct_time)) #時間的格式化輸出print(time.strptime(‘2016-09-08 18:48:35‘,‘%Y-%m-%d %H:%M:%S‘)) #將時間賦值到變數#同時也是將時間結構化print(time.ctime()) #不能自訂格式方式的 擷取目前時間的方式print(time.ctime(0)) #列印時間戳記的詳細時間print(time.mktime(time.localtime())) # 結構化時間轉化為時間戳記
三: random 模組
#codeing:UTF-8#__author__:Duke#date:2018/3/14/014 19:29import randomprint(help(random))print(random.random()) #列印一個隨機數 in [0.1)print(random.randint(1,8)) #包括右邊的數print(random.choice(‘hello‘)) #隨機播放字串中的字元print(random.choice([‘123‘,45,‘ok‘])) #隨機播放字串中的字元print(random.choices([‘123‘,45,‘ok‘,3,4,5,7])) #print(random.sample([‘123‘,45,‘ok‘,3,4,5,7],2)) #隨機選多個數print(random.randrange(1,7)) #不包括最後一個數
# random 模組應用 隨機數的產生def v_code(): code = ‘‘ for i in range(6): x = random.choice([1,2,3]) if x == 1: code_num = random.randrange(0,10) code += str(code_num) elif x == 2: code_small_word = random.randrange(65,91) code += chr(code_small_word) else: code_big_word = random.randrange(97,123) code += chr(code_big_word) return codeprint(v_code())
python學習day11