小編也不知道大家能不能用的到,我只是把我學到的知識分享出來,有需要的可以看一下。python本身就是一個不斷更新改進的語言,不存在抄襲,有需要就可以拿過來用,在用的過程中,你發現可以用另外一種方法把它實現,就可以把程式碼做進一步的優化,然後分享出來,這樣python會變的越來越實用。今天心情不好,分享兩個小程式就結束!
郵箱分類儲存
1 #! /usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # __author__ = "Dylan" 4 5 6 ``` 新建一個txt檔案儲存網路爬取的郵箱賬號和密碼,儲存方式如下(具體問題具體分析,這裡只是舉個例子): 7 data: 8 賬號 密碼 9 laphae757878@163.com----198587 10 sgdjhgawue@126.com----198587 11 eiuuweyi34@qq.com----198587 12 res: 13 126.txt: 14 sgdjhgawue 15 163.txt: 16 laphae757878 17 qq.txt: 18 eiuuweyi34 19 ``` 20 21 22 import os 23 import collections 24 25 def work(path): 26 resPath = r"存放取出資料的路徑" # 如c:python es 27 with open(path, "r") as f: 28 while True: 29 lineInfo = f.readline() 30 if len(lineInfo) < 5: 31 break 32 emailStr = lineInfo.split("----")[0] # 郵箱@前的欄位 33 fileType = emailStr.split("@")[1].split(".")[0] # 郵箱型別的目錄 如c:python es163 34 dirStr = os.path.join(resPath, fileType) 35 if not os.path.exists(dirStr): 36 os.mkdir(dirStr) # 不存在,就建立這個目錄 37 filePath = os.path.join(dirStr, fileType + ".txt") # 為每一個郵箱型別建立一個txt檔案,然後把對應的郵箱寫進去 38 with open(filePath, "w") as fw: 39 fw.write(emailStr + " ") 40 41 def getAllDirQueue(path): 42 queue = collections.deque() 43 queue.append(path) 44 while len(queue) != 0: 45 dirPath = queue.popleft() 46 fileList = os.listdir(dirPath) 47 for fileName in fileList: 48 fileAbsPath = os.path.join(dirPath, fileName) 49 if os.path.isdir(fileAbsPath): 50 queue.append(fileAbsPath) 51 else: 52 work(fileAbsPath) # 處理普通檔案 53 54 getAllDirQueue(r"資料路徑") # 如c:pythondata
語音控制系統開啟或關閉系統應用程式
1 from win32com.client import constants 2 import win32com.client 3 import pythoncom 4 import os 5 6 speaker = win32com.client.Dispatch("SAPI.SPVOICE") 7 8 9 class SpeechRecognition: 10 def __init__(self,wordsToAdd): 11 self.speaker=win32com.client.Dispatch("SAPI.SpVoice") 12 self.listener=win32com.client.Dispatch("SAPI.SpSharedRecognizer") 13 self.context=self.listener.CreateRecoContext() 14 self.grammar=self.context.CreateGrammar() 15 self.grammar.DictationSetState(0) 16 self.wordsRule=self.grammar.Rules.Add("wordsRule",constants.SRATopLevel+constants.SRADynamic,0) 17 self.wordsRule.Clear() 18 [self.wordsRule.InitialState.AddWordTransition(None,word)for word in wordsToAdd] 19 self.grammar.Rules.Commit() 20 self.grammar.CmdSetRuleState("wordsRule",1) 21 self.grammar.Rules.Commit() 22 self.eventHandler=ContextEvents(self.context) 23 self.say("Startedsuccessfully") 24 25 def say(self,phrase): 26 self.speaker.Speak(phrase) 27 28 29 class ContextEvents(win32com.client.getevents("SAPI.SpSharedRecoContext")): 30 def OnRecognition(self,StreamNumber,StreamPosition,RecognitionType,Result): 31 newResult=win32com.client.Dispatch(Result) 32 print("說:",newResult.PhraseInfo.GetText()) 33 s = newResult.PhraseInfo.GetText() 34 if s == "記事本": 35 os.system("start notepad") 36 elif s == "畫圖板": 37 os.system("start mspaint") 38 39 if __name__ == "__main__": 40 speaker.Speak("語音識別開啟") 41 wordsToAdd = ["關機", "取消關機", "記事本", "畫圖板", "寫字板", "設定", "關閉記事本"] 42 speechReco = SpeechRecognition(wordsToAdd) 43 while True: 44 pythoncom.PumpWaitingMessages()