前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >分享两个小程序

分享两个小程序

作者头像
py3study
发布2020-01-19 15:24:16
5280
发布2020-01-19 15:24:16
举报
文章被收录于专栏:python3python3

  小编也不知道大家能不能用的到,我只是把我学到的知识分享出来,有需要的可以看一下。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()
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-03-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
语音识别
腾讯云语音识别(Automatic Speech Recognition,ASR)是将语音转化成文字的PaaS产品,为企业提供精准而极具性价比的识别服务。被微信、王者荣耀、腾讯视频等大量业务使用,适用于录音质检、会议实时转写、语音输入法等多个场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档