小编也不知道大家能不能用的到,我只是把我学到的知识分享出来,有需要的可以看一下。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\res
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\res\163
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 + "\n")
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:\python\data
语音控制系统打开或关闭系统应用程序
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()