<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<atom:link href="http://itecfun.com/extern.php?action=feed&amp;fid=18&amp;type=rss" rel="self" type="application/rss+xml" />
		<title><![CDATA[个人知识管理站 / Python]]></title>
		<link>http://www.itecfun.com/index.php</link>
		<description><![CDATA[个人知识管理站 最近发表的主题。]]></description>
		<lastBuildDate>Tue, 23 Jul 2019 06:02:36 +0000</lastBuildDate>
		<generator>FluxBB</generator>
		<item>
			<title><![CDATA[python 库安装与使用【收集】]]></title>
			<link>http://www.itecfun.com/viewtopic.php?id=3410&amp;action=new</link>
			<description><![CDATA[<p><strong> <a href="https://mp.weixin.qq.com/s?__biz=MzIxODM4MjA5MA==&amp;mid=2247490018&amp;idx=2&amp;sn=2019be108df0805a6e3dcb4e36852848&amp;chksm=97ea3387a09dba91bf6d42ca1eb7b37564f2fc8548c62b424f9635a0dcb389f4147055cf2811&amp;scene=21#wechat_redirect" rel="nofollow">1、pynput 【Python技能】如丝滑般控制键盘鼠标</a></strong></p>]]></description>
			<author><![CDATA[dummy@example.com (xuyg)]]></author>
			<pubDate>Tue, 23 Jul 2019 06:02:36 +0000</pubDate>
			<guid>http://www.itecfun.com/viewtopic.php?id=3410&amp;action=new</guid>
		</item>
		<item>
			<title><![CDATA[正则表达式]]></title>
			<link>http://www.itecfun.com/viewtopic.php?id=3409&amp;action=new</link>
			<description><![CDATA[<p>正则表达式的使用</p><br /><p><strong>函数&#160; &#160;&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; 说明</strong><br />sub(pattern,repl,string)&#160; &#160; 把字符串中的所有匹配表达式pattern中的地方替换成repl<br />[^**]&#160; &#160;&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; 表示不匹配此字符集中的任何一个字符<br />\u4e00-\u9fa5&#160; &#160;&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; 汉字的unicode范围<br />\u0030-\u0039&#160; &#160;&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; 数字的unicode范围<br />\u0041-\u005a&#160; &#160;&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; 大写字母unicode范围<br />\u0061-\u007a&#160; &#160;&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;小写字母unicode范围<br />\uAC00-\uD7AF&#160; &#160; &#160; &#160;&#160; &#160; &#160; 韩文的unicode范围<br />\u3040-\u31FF&#160; &#160;&#160; &#160; &#160; &#160; &#160; &#160; &#160;日文的unicode范围</p>]]></description>
			<author><![CDATA[dummy@example.com (xuyg)]]></author>
			<pubDate>Wed, 17 Jul 2019 02:07:27 +0000</pubDate>
			<guid>http://www.itecfun.com/viewtopic.php?id=3409&amp;action=new</guid>
		</item>
		<item>
			<title><![CDATA[python3.6+pytesseract+PIL 实现图片批量文字识别代码（原创）]]></title>
			<link>http://www.itecfun.com/viewtopic.php?id=3406&amp;action=new</link>
			<description><![CDATA[<div class="codebox"><pre class="vscroll"><code>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2019-06-03 10:12:06
# @Author  : XuYG (xiaoxu04@foxmail.com)
# @Link    : http://www.itecfun.com
# @Version : $V1.0$

import os
from PIL import Image
import pytesseract

class xygORC(object):
	def __init__(self,dir_path,lang,file_name,write_mode):
		&#039;&#039;&#039;
		初始化相关参数
		
		Arguments:
			dir_path {[str]} -- [要识别的图片所在目录]
			lang {[str]} -- [tessdata训练数据包的名称，默认采用简体中文识别 chi_sim]
			file_name {[str]} -- [识别的文本保存的文件名称，可自由命名]
			write_mode {[str]} -- [default a+]
		&#039;&#039;&#039;
		self.dir_path = dir_path
		self.lang = lang
		self.file_name = file_name
		self.write_mode = write_mode
		self.content = &quot;&quot;

	def recognize(self,image_path,lang):
		&#039;&#039;&#039;
		单张图片识别	
		Arguments:
			image_path {[str]} -- [待识别图片路径]
			lang {[str]} -- [tessdata 训练数据包的名称，简体中文用chi_sim进行识别]
		Returns:
			[str] -- [返回识别的本张图片的文字内容]
		&#039;&#039;&#039;
		image = Image.open(image_path)
		#使用tessdata chi_sim简体中文训练数据进行识别
		content = pytesseract.image_to_string(image, lang=lang)
		return content
	def save(self,file_name,content,write_mode):
		&#039;&#039;&#039;
		保存识别的内容
		Arguments:
			file_name {[str]} -- [保存的文件路径]
			content {[str]} -- [文本内容]
			write_mode {[str]} -- [a,b,r,a+,b+...]
		Returns:
			bool -- [返回保存状态，True or False]
		&#039;&#039;&#039;
		#结果保存
		with open(file_name,write_mode) as f:
			f.write(content.replace(&#039; &#039;,&#039;&#039;))  #识别后每个字之间有空格，需要去掉空格
			return True
		return False

	def recognize_all(self,dir_path,lang):
		&#039;&#039;&#039;
		识别制定目录下所有图片中的文字
		
		Arguments:
			dir_path {[str]} -- [目录路径]
			lang {[str]} -- [tessdata 训练数据包的名称，简体中文用chi_sim进行识别]
		
		Returns:
			[str] -- [目录下所有图片识别后的文字内容]
		&#039;&#039;&#039;
		img_list = self.get_file_list(dir_path, [])
		img_list.sort(key=lambda x: os.path.getmtime(x))
		content = &quot;&quot;
		for img in img_list:
			text = self.recognize(img, lang)
			content +=text +&quot;\n &quot;
			print(&quot;&#039;{0}&#039;已识别完成！&quot;.format(img))
		return content

	def get_file_list(self,dir, file_list):
		&#039;&#039;&#039;
		获取指定目录下所有待识别的jpg、png图片列表
		并按时间排序后返回
		
		Arguments:
			dir {[str]} -- [图片存放目录]
			file_list {[list]} -- [图片列表]
		
		Returns:
			[list] -- [目录下所有图片列表]
		&#039;&#039;&#039;
		newDir = dir
		if os.path.isfile(dir):
			ext = os.path.splitext(dir)[-1]
			if ext == &quot;.jpg&quot; or ext ==&quot;.png&quot;:
				file_list.append(dir)
		elif os.path.isdir(dir):
			for file in os.listdir(dir):
				newDir = os.path.join(dir, file)
				self.get_file_list(newDir, file_list)
		return file_list

	def main(self):
		&#039;&#039;&#039;
		主函数入口
		&#039;&#039;&#039;
		self.content = self.recognize_all(self.dir_path, self.lang)
		self.save(self.file_name, self.content, write_mode)


if __name__ == &#039;__main__&#039;:
	dir_path =&#039;E:\\国家政策\\政策学习01：国家大数据战略与社会治理革命&#039; 
	lang  =&#039;chi_sim&#039;  #tessdata chi_sim训练数据包的名称
	file_name = &quot;政策学习.txt&quot; #要保存的识别文字的文件名称，可自由命名
	write_mode =&quot;a+&quot; #以文件末尾追加的形式进行写入
	os.chdir(dir_path) ##将工作目录切换到图片所在目录，主要目的是将文件保存在图片所在目录，方便查阅

	myorc = xygORC(dir_path,lang,file_name,write_mode)

	myorc.main()</code></pre></div>]]></description>
			<author><![CDATA[dummy@example.com (xuyg)]]></author>
			<pubDate>Mon, 03 Jun 2019 04:22:36 +0000</pubDate>
			<guid>http://www.itecfun.com/viewtopic.php?id=3406&amp;action=new</guid>
		</item>
		<item>
			<title><![CDATA[Python 3.6 安装BeautifulSoup4]]></title>
			<link>http://www.itecfun.com/viewtopic.php?id=3351&amp;action=new</link>
			<description><![CDATA[<p>在python3.6环境下通过 </p><p>pip install beaufifulsoup4</p><p>安装BeautifulSoup4 失败，主要是bs4是用python2.7编写的，需使用自带工具2to3.py工具转变下代码即可。</p><p>首先，下载<a href="https://www.crummy.com/software/BeautifulSoup/bs4/download/" rel="nofollow">BeautifulSoup</a>对相应的版本。</p><p>将beaufifulsoup4-4.6.0.tar.gz解压缩，拷贝目录下的bs4文件到 python安装目录的Lib文件夹下</p><p>cmd进入 python安装目录的Tools\scripts中 </p><p>在命令行中输入：python 2to3.py&#160; -w &quot;D:\Program Files\Python\Python36\Lib\bs4&quot;</p><p>进入python，输入 import bs4看下是否正常~。</p>]]></description>
			<author><![CDATA[dummy@example.com (xuyg)]]></author>
			<pubDate>Tue, 29 May 2018 05:53:56 +0000</pubDate>
			<guid>http://www.itecfun.com/viewtopic.php?id=3351&amp;action=new</guid>
		</item>
		<item>
			<title><![CDATA[python gui之tkinter事件处理]]></title>
			<link>http://www.itecfun.com/viewtopic.php?id=3349&amp;action=new</link>
			<description><![CDATA[<p>python gui之tkinter事件处理<br />事件一览表<br />事件&#160; &#160;&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; 代码&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; 备注<br />鼠标左键单击按下&#160; &#160; 1/Button-1/ButtonPress-1&#160; &#160;&#160; <br />鼠标左键单击松开&#160; &#160; ButtonRelease-1&#160; &#160;&#160; <br />鼠标右键单击&#160; &#160; 3&#160; &#160;&#160; <br />鼠标左键双击&#160; &#160; Double-1/Double-Button-1&#160; &#160;&#160; <br />鼠标右键双击&#160; &#160; Double-3&#160; &#160;&#160; <br />鼠标滚轮单击&#160; &#160; 2&#160; &#160;&#160; <br />鼠标滚轮双击&#160; &#160; Double-2&#160; &#160;&#160; <br />鼠标移动&#160; &#160;&#160; &#160; &#160; &#160; &#160;B1-Motion&#160; &#160;&#160; <br />鼠标移动到区域&#160; &#160; Enter&#160; &#160;&#160; <br />鼠标离开区域&#160; &#160; Leave&#160; &#160;&#160; <br />获得键盘焦点&#160; &#160; FocusIn&#160; &#160;&#160; <br />失去键盘焦点&#160; &#160; FocusOut&#160; &#160;&#160; <br />键盘事件&#160; &#160; &#160; &#160; &#160; Key&#160; &#160;&#160; <br />回车键&#160; &#160;&#160; &#160; &#160; &#160; &#160;Return&#160; &#160;&#160; <br />控件尺寸变&#160; &#160;&#160; &#160; Configure&#160; &#160; </p><p>响应时间<br />提前响应<br />ttk treeview的TreeviewSelect事件是提前的，即你选中了某行，通过treeview.selection()[0]得到的就是这一样。 </p><p>延后相应<br />比如ttk的treeview是的单击的情况，单击的行被选中了，但是通过 treeview.selection()[0]得到的却不是选中的行！而是之前选中的行。可以参考下这里。</p><p>响应函数<br />event_handler(event,*args)</p><p>event参数<br />event 参数有以下属性：</p><p>[&#039;__doc__&#039;, &#039;__module__&#039;, &#039;char&#039;, &#039;delta&#039;, &#039;height&#039;, &#039;keycode&#039;, &#039;keysym&#039;, &#039;keysym_num&#039;, &#039;num&#039;, &#039;send_event&#039;, &#039;serial&#039;, &#039;state&#039;, &#039;time&#039;, &#039;type&#039;, &#039;widget&#039;, &#039;width&#039;, &#039;x&#039;, &#039;x_root&#039;, &#039;y&#039;, &#039;y_root&#039;]</p><p>Event Attributes<br />widget<br />The widget which generated this event. This is a valid Tkinter widget instance, not a name. This attribute is set for all events.</p><p>x, y<br />鼠标当前的相对位置，以像素为单位。</p><p>比如，ttk treeview 有个通过y坐标定位行的方法：identify_row(self, y)</p><br /><p>x_root, y_root<br />鼠标当前的绝对位置（相对于设备的左上角）。以像素为单位。</p><p>char<br />字符（键盘事件中才有）， 类型是字符串。</p><p>keysym<br />The key symbol (keyboard events only).</p><p>键符（键盘事件中才有）</p><p>keycode<br />键码 (键盘事件中才有).</p><p>num<br />按钮号码（鼠标事件中才有）1-左键/2-中/3-右</p><p>width, height<br />widget的新尺寸，以像素为单位(Configure events only).</p><p>type<br />&#160; &#160; &#160; &#160; 事件类型<br />&#160; &#160; &#160; &#160; 1---<br />&#160; &#160; &#160; &#160; 2---<br />&#160; &#160; &#160; &#160; 3---<br />&#160; &#160; &#160; &#160; 4---鼠标<br />更多资料参考这里。<br />绑定事件<br />控件.bind(&#039;&lt;事件代码&gt;&#039;,event_handler)<br />适用于大多数控件。此外还有bind_all方法。</p><p>控件.protocal(&#039;事件代码&#039;, event_handler)<br />这种情况的控件，必需是顶层窗口或者root容器。</p>]]></description>
			<author><![CDATA[dummy@example.com (xuyg)]]></author>
			<pubDate>Sun, 27 May 2018 03:38:57 +0000</pubDate>
			<guid>http://www.itecfun.com/viewtopic.php?id=3349&amp;action=new</guid>
		</item>
		<item>
			<title><![CDATA[Python入门：Numpy 和 Pandas 基本语法]]></title>
			<link>http://www.itecfun.com/viewtopic.php?id=3343&amp;action=new</link>
			<description><![CDATA[<p>df = pd.read_clipboard()</p><p>df.to_excel(&#039;D:\\dataframe_excel.xls&#039;,index=True)&#160; #export to excel file</p><p>#写入excel文件需要安装 xlwt 模块 pip install xlwt<br /> import xlwt<br />ModuleNotFoundError: No module named &#039;xlwt&#039;</p><p>dfexcel = pd.read_excel(&#039;D:\\dataframe_excel.xls&#039;)</p><p>#读取excel文件需要安装 xlrd 模块 pip install xlrd<br />import xlrd<br />ModuleNotFoundError: No module named &#039;xlrd&#039;</p>]]></description>
			<author><![CDATA[dummy@example.com (xuyg)]]></author>
			<pubDate>Thu, 10 May 2018 08:58:58 +0000</pubDate>
			<guid>http://www.itecfun.com/viewtopic.php?id=3343&amp;action=new</guid>
		</item>
		<item>
			<title><![CDATA[Python 开发 pycharm和Spyder哪个好？]]></title>
			<link>http://www.itecfun.com/viewtopic.php?id=3333&amp;action=new</link>
			<description><![CDATA[<p>1. 主要做QT相关的开发的话用Eric；</p><p>2. 主要开发Web相关的的话可以用PyCharm或者Aptana Studio 3；</p><p>3. 作为适应了matlab用户界面的我来说我还是使用了PyScripter，一个主动性高的IDE和一个开放性高的python相遇就是一件美好的事情。</p><p>单从界面来说其实我们都可以看见许多经典的操作界面的影子，比如visual studio，matlab, wordpad等等。</p>]]></description>
			<author><![CDATA[dummy@example.com (xuyg)]]></author>
			<pubDate>Sat, 28 Apr 2018 07:46:38 +0000</pubDate>
			<guid>http://www.itecfun.com/viewtopic.php?id=3333&amp;action=new</guid>
		</item>
		<item>
			<title><![CDATA[[人工智能]机器学习的框架偏向于Python原因]]></title>
			<link>http://www.itecfun.com/viewtopic.php?id=3312&amp;action=new</link>
			<description><![CDATA[<p>前言</p><p>主要有以下原因:</p><p>1. Python是解释语言，程序写起来非常方便</p><p>写程序方便对做机器学习的人很重要。 <br />因为经常需要对模型进行各种各样的修改，这在编译语言里很可能是牵一发而动全身的事情，Python里通常可以用很少的时间实现。</p><p>举例来说，在C等编译语言里写一个矩阵乘法，需要自己分配操作数（矩阵）的内存、分配结果的内存、手动对BLAS接口调用gemm、最后如果没用smart pointer还得手动回收内存空间。Python几乎就是import numpy; numpy.dot两句话的事。</p><p>当然现在很多面向C/C++库已经支持托管的内存管理了，这也让开发过程容易了很多，但解释语言仍然有天生的优势——不需要编译时间。这对机器学习这种需要大量prototyping和迭代的研究方向是非常有益工作效率的。<br />2. Python的开发生态成熟，有很多有用的库可以用</p><p>除了上面说到的NumPy，还有SciPy、NLTK、os（自带）等等不一而足。Python灵活的语法还使得包括文本操作、list/dict comprehension等非常实用的功能非常容易高效实现（编写和运行效率都高），配合lambda等使用更是方便。这也是Python良性生态背后的一大原因。相比而言，Lua虽然也是解释语言，甚至有LuaJIT这种神器加持，但其本身很难做到Python这样，一是因为有Python这个前辈占领着市场份额，另一个也因为它本身种种反常识的设计（比如全局变量）。不过借着Lua-Python bridge和Torch的东风，Lua似乎也在寄生兴起。<br />3. Python的效率很高。</p><p>解释语言的发展已经大大超过许多人的想象。很多比如list comprehension的语法糖都是贴近内核实现的。除了JIT[1]之外，还有Cython可以大幅增加运行效率。最后，得益于Python对C的接口，很多像gnumpy, theano这样高效、Python接口友好的库可以加速程序的运行，在强大团队的支撑下，这些库的效率可能比一个不熟练的程序员用C写一个月调优的效率还要高。<br />4.数据存储方便</p><p>有sql,hadoop,mangodb,redis,spark等<br />5.数据获取方便</p><p>有Scrapy,beautifulsoup,requests,paramiko等<br />6.数据运算方便</p><p>有pandas，Numpy，scipy等<br />7.输出结果方便</p><p>有matplotlib,VisPy等<br />8.和其他语言交互方便</p><p>有ctypes,rpy2,Cython,SWIG,PyQt,boost.python<br />9.加速方便</p><p>有pypy,Cython,PyCUDA</p><p>10.图形图像方便</p><p>有PyOpenGL,PyOpenCV,mayavi2<br />11.信号处理方便</p><p>PyWavelets，scipy.signal<br />12.云系统支持方便</p><p>github,sourceforge,EC2,BAT,HPC<br />13.python开源</p><p>python支持的平台多，包括windows,linux,unix,macos。而matlab太贵，只能调用其api，用python省钱，省钱就是赚钱。<br />python 和 c++ 做个比较。</p><p>c++ 的cpu效率是远远高于 python 的.不过 python 是一门胶水语言，它可以和任何语言结合，基于这个优点，很多数据处理的python 库底层都是 c++ 实现的，意思就是说：你用python写code，但效率是c++的。只有那些for 循环，还是用python的效率高。</p><p>近年来机器学习最要是深度学习，而深度学习使用cuda gpu加速远比cpu要快，而cuda 是c++写的。<br />所以现在TensorLayer、theano 等深度学习库都是 python 编程、底层c++。</p><p><a href="http://blog.csdn.net/BaiHuaXiu123/article/details/54349037" rel="nofollow">版权声明：本文为博主原创文章，未经博主允许不得转载。</a></p>]]></description>
			<author><![CDATA[dummy@example.com (xuyg)]]></author>
			<pubDate>Mon, 11 Dec 2017 07:03:53 +0000</pubDate>
			<guid>http://www.itecfun.com/viewtopic.php?id=3312&amp;action=new</guid>
		</item>
		<item>
			<title><![CDATA[重拾Python  Django web]]></title>
			<link>http://www.itecfun.com/viewtopic.php?id=3240&amp;action=new</link>
			<description><![CDATA[<p>Django 部署 <br /><a href="http://blog.csdn.net/yingmutongxue/article/details/43985559#" rel="nofollow">参考1</a><br /><a href="http://www.cnblogs.com/fnng/p/4119712.html" rel="nofollow">参考2</a></p><p>注意个软件版本需要相适应</p><p>已配置成功的软件环境如下：<br />Apache:httpd-2.4.20-x86-r2-VC9<br />Python:2.7.9 win32<br />Django:1.9.6<br />mod_wsgi:mod_wsgi-4.4.23+ap24vc9-cp27-cp27m-win32</p>]]></description>
			<author><![CDATA[dummy@example.com (xuyg)]]></author>
			<pubDate>Fri, 17 Jun 2016 05:13:56 +0000</pubDate>
			<guid>http://www.itecfun.com/viewtopic.php?id=3240&amp;action=new</guid>
		</item>
		<item>
			<title><![CDATA[Python 字符串操作]]></title>
			<link>http://www.itecfun.com/viewtopic.php?id=3185&amp;action=new</link>
			<description><![CDATA[<p><strong>介绍字符串相关的：比较,截取,替换,长度,连接,反转,编码,格式化,查找,复制,大小写,分割等操作</strong></p><p>字符串或串(String)是由数字、字母、下划线组成的一串字符。一般记为 s=“a1a2···an”(n&gt;=0)。它是编程语言中表示文本的数据类型。 通常以串的整体作为操作对象，如：在串中查找某个子串、求取一个子串、在串的某个位置上插入一个子串以及删除一个子串等。两个字符串相等的充要条件是：长度相等，并且各个对应位置上的字符都相等。<br />python 字符串相关特性<br />1 属于python基本数据类型和结构的一种。2 本身是不可变的数据类型。 3 有很多内置的方法<br /><strong>字符串连接</strong><br />方法1： 用字符串的join方法<br />a = [&#039;a&#039;,&#039;b&#039;,&#039;c&#039;,&#039;d&#039;]<br />content = &#039;&#039;<br />content = &#039;&#039;.join(a)<br />print content<br />方法2： 用字符串的替换占位符替换<br />a = [&#039;a&#039;,&#039;b&#039;,&#039;c&#039;,&#039;d&#039;]<br />content = &#039;&#039;<br />content = &#039;%s%s%s%s&#039; % tuple(a)<br />print content<br />想要了解更多,请看python字符串连接<br /><strong>字符串截取</strong><br />我们可以通过索引来提取想要获取的字符，可以把python的字符串也做为字符串的列表就更好理解</p><p>python的字串列表有2种取值顺序<br />1是从左到右索引默认0开始的，最大范围是字符串长度少1<br />s = &#039;ilovepython&#039;<br />s[0]的结果是i</p><p>2是从右到左索引默认-1开始的，最大范围是字符串开头<br />s = &#039;ilovepython&#039;<br />s[-1]的结果是n</p><p>上面这个是取得一个字符，如果你的实际要取得一段子串的话，可以用到变量[头下标:尾下标]，就可以截取相应的字符串，其中下标是从0开始算起，可以是正数或负数，下标可以为空表示取到头或尾。</p><p>比如<br />s = &#039;ilovepython&#039;<br />s[1:5]的结果是love<br />当使用以冒号分隔的字符串，python返回一个新的对象，结果包含了以这对偏移标识的连续的内容，左边的开始是包含了下边界，比如<br />上面的结果包含了s[1]的值l，而取到的最大范围不包括上边界，就是s[5]的值p</p><p>想要了解更多,请看python字符串截取<br /><strong>字符串替换</strong><br />字符串替换可以用内置的方法和正则表达式完成。<br />1用字符串本身的replace方法:<br />a = &#039;hello word&#039;<br />b = a.replace(&#039;word&#039;,&#039;python&#039;)<br />print b<br />2用正则表达式来完成替换:<br />import re<br />a = &#039;hello word&#039;<br />strinfo = re.compile(&#039;word&#039;)<br />b = strinfo.sub(&#039;python&#039;,a)<br />print b<br />想要了解更多,请看python 字符串替换<br /><strong>字符串比较</strong><br />cmp方法比较两个对象，并根据结果返回一个整数。cmp(x,y)如果X&lt; Y,返回值是负数 如果X&gt;Y 返回的值为正数。<br />sStr1 = &#039;strch&#039;<br />sStr2 = &#039;strchr&#039;<br />print cmp(sStr1,sStr2)##-1<br />字符串相加<br />我们通过操作符号+来进行字符串的相加，不过建议还是用其他的方式来进行字符串的拼接，这样效率高点。<br />原因：在循环连接字符串的时候，他每次连接一次，就要重新开辟空间，然后把字符串连接起来，再放入新的空间，再一次循环，又要开辟新的空间，把字符串连接起来放入新的空间，如此反复，内存操作比较频繁，每次都要计算内存空间，然后开辟内存空间，再释放内存空间，效率非常低。<br />sStr1 = &#039;strch&#039;<br />sStr2 = &#039;strchr&#039;<br />newstr = sStr1 + sStr2<br />print newstr<br /><strong>字符串查找</strong><br />python 字符串查找有4个方法，1 find,2 index方法，3 rfind方法,4 rindex方法。<br /><strong>1 find()方法：</strong><br />info = &#039;abca&#039;<br />print info.find(&#039;a&#039;)##从下标0开始，查找在字符串里第一个出现的子串，返回结果：0</p><p>info = &#039;abca&#039;<br />print info.find(&#039;a&#039;,1)##从下标1开始，查找在字符串里第一个出现的子串：返回结果3</p><p>info = &#039;abca&#039;<br />print info.find(&#039;333&#039;)##返回-1,查找不到返回-1<br /><strong>2 index()方法：</strong><br />python 的index方法是在字符串里查找子串第一次出现的位置，类似字符串的find方法，不过比find方法更好的是，如果查找不到子串，会抛出异常，而不是返回-1<br />info = &#039;abca&#039;<br />print info.index(&#039;a&#039;)<br />print info.index(&#039;33&#039;)<br /><strong>字符串分割</strong><br />字符串分割，可以用split,rsplit方法，通过相应的规则来切割成生成列表对象<br />info = &#039;name:haha,age:20$name:python,age:30$name:fef,age:55&#039;<br />content = info.split(&#039;$&#039;)<br />print content<br /><strong>字符串翻转</strong><br />通过步进反转[::-1]<br />a = &#039;abcd&#039;<br />b = a[::-1]##[::-1]通过步进反转<br />print b<br /><strong>字符串编码</strong><br />通过字符串的decode和encode方法<br /><strong>1 encode([encoding,[errors]]) </strong><br />#其中encoding可以有多种值，比如gb2312 gbk gb18030 bz2 zlib big5 bzse64等都支持。errors默认值为&quot;strict&quot;，意思是UnicodeError。可能的值还有&#039;ignore&#039;, &#039;replace&#039;, &#039;xmlcharrefreplace&#039;, &#039;backslashreplace&#039; 和所有的通过codecs.register_error注册的值。<br />S.decode([encoding,[errors]]) 下面是字符串编码应用:<br />a = &#039;你好&#039;<br />b = &#039;python&#039;<br />print a.decode(&#039;utf-8&#039;).encode(&#039;gbk&#039;)##decode方法把字符串转换为unicode对象，然后通过encode方法转换为指定的编码字符串对象<br />print b.decode(&#039;utf-8&#039;)##decode方法把字符串转换为unicode对象<br /><strong>字符串追加和拼接</strong><br />通过字符串的占位符来进行字符串的拼接<br /><strong>#1 元组拼接</strong><br />m = &#039;python&#039;<br />astr = &#039;i love %s&#039; % m<br />print astr</p><p><strong>#2 字符串的format方法</strong><br />m = &#039;python&#039;<br />astr = &quot;i love {python}&quot;.format(python=m)<br />print astr</p><p><strong>#3 字典格式化字符串</strong><br />m = &#039;python&#039;<br />astr = &quot;i love %(python)s &quot; % {&#039;python&#039;:m}<br />print astr<br /><strong>字符串复制</strong><br />通过变量来进行赋值<br />fstr = &#039;strcpy&#039;<br />sstr = fstr<br />fstr = &#039;strcpy2&#039;<br />print sstr<br /><strong>字符串长度</strong><br />通过内置方法len()来计算字符串的长度，注意这个计算的是字符的长度。<br />aa = &#039;afebb&#039;<br />bb = &#039;你&#039;<br />print len(aa)<br />print len(bb)<br /><strong>字符串大小写</strong><br />通过下面的upper(),lower()等方法来转换大小写<br />S.upper()#S中的字母大写 <br />S.lower() #S中的字母小写 <br />S.capitalize() #首字母大写 <br />S.istitle() #S是否是首字母大写的 <br />S.isupper() #S中的字母是否便是大写 <br />S.islower() #S中的字母是否全是小写 <br /><strong>字符串去空格</strong><br />通过strip(),lstrip(),rstrip()方法去除字符串的空格<br />S.strip()去掉字符串的左右空格<br />S.lstrip()去掉字符串的左边空格<br />S.rstrip()去掉字符串的右边空格<br /><strong>字符串其他方法</strong><br />字符串相关的其他方法:count(),join()方法等。<br />S.center(width, [fillchar]) #中间对齐<br />S.count(substr, [start, [end]]) #计算substr在S中出现的次数<br />S.expandtabs([tabsize]) #把S中的tab字符替换没空格，每个tab替换为tabsize个空格，默认是8个 <br />S.isalnum() #是否全是字母和数字，并至少有一个字符 <br />S.isalpha() #是否全是字母，并至少有一个字符 <br />S.isspace() #是否全是空白字符，并至少有一个字符<br />S.join()#S中的join，把列表生成一个字符串对象<br />S.ljust(width,[fillchar]) #输出width个字符，S左对齐，不足部分用fillchar填充，默认的为空格。<br />S.rjust(width,[fillchar]) #右对齐 <br />S.splitlines([keepends]) #把S按照行分割符分为一个list，keepends是一个bool值，如果为真每行后而会保留行分割符。<br />S.swapcase() #大小写互换</p>]]></description>
			<author><![CDATA[dummy@example.com (xuyg)]]></author>
			<pubDate>Fri, 03 Jul 2015 06:16:53 +0000</pubDate>
			<guid>http://www.itecfun.com/viewtopic.php?id=3185&amp;action=new</guid>
		</item>
		<item>
			<title><![CDATA[python文件操作]]></title>
			<link>http://www.itecfun.com/viewtopic.php?id=3184&amp;action=new</link>
			<description><![CDATA[<p>python中对文件、文件夹（文件操作函数）的操作需要涉及到os模块和shutil模块。</p><p>得到当前工作目录，即当前Python脚本工作的目录路径: os.getcwd()</p><p>返回指定目录下的所有文件和目录名:os.listdir()</p><p>函数用来删除一个文件:os.remove()</p><p>删除多个目录：os.removedirs（r“c：python”）</p><p>检验给出的路径是否是一个文件：os.path.isfile()</p><p>检验给出的路径是否是一个目录：os.path.isdir()</p><p>判断是否是绝对路径：os.path.isabs()</p><p>检验给出的路径是否真地存:os.path.exists()</p><p>返回一个路径的目录名和文件名:os.path.split()&#160; &#160; &#160;eg os.path.split(&#039;/home/swaroop/byte/code/poem.txt&#039;) 结果：(&#039;/home/swaroop/byte/code&#039;, &#039;poem.txt&#039;) </p><p>分离扩展名：os.path.splitext()</p><p>获取路径名：os.path.dirname()</p><p>获取文件名：os.path.basename()</p><p>运行shell命令: os.system()</p><p>读取和设置环境变量:os.getenv() 与os.putenv()</p><p>给出当前平台使用的行终止符:os.linesep&#160; &#160; Windows使用&#039;\r\n&#039;，Linux使用&#039;\n&#039;而Mac使用&#039;\r&#039;</p><p>指示你正在使用的平台：os.name&#160; &#160; &#160; &#160;对于Windows，它是&#039;nt&#039;，而对于Linux/Unix用户，它是&#039;posix&#039;</p><p>重命名：os.rename（old， new）</p><p>创建多级目录：os.makedirs（r“c：python\test”）</p><p>创建单个目录：os.mkdir（“test”）</p><p>获取文件属性：os.stat（file）</p><p>修改文件权限与时间戳：os.chmod（file）</p><p>终止当前进程：os.exit（）</p><p>获取文件大小：os.path.getsize（filename）</p><br /><p>文件操作：<br />os.mknod(&quot;test.txt&quot;)&#160; &#160; &#160; &#160; 创建空文件<br />fp = open(&quot;test.txt&quot;,w)&#160; &#160; &#160;直接打开一个文件，如果文件不存在则创建文件</p><p>关于open 模式：</p><p>w&#160; &#160; &#160;以写方式打开，<br />a&#160; &#160; &#160;以追加模式打开 (从 EOF 开始, 必要时创建新文件)<br />r+&#160; &#160; &#160;以读写模式打开<br />w+&#160; &#160; &#160;以读写模式打开 (参见 w )<br />a+&#160; &#160; &#160;以读写模式打开 (参见 a )<br />rb&#160; &#160; &#160;以二进制读模式打开<br />wb&#160; &#160; &#160;以二进制写模式打开 (参见 w )<br />ab&#160; &#160; &#160;以二进制追加模式打开 (参见 a )<br />rb+&#160; &#160; 以二进制读写模式打开 (参见 r+ )<br />wb+&#160; &#160; 以二进制读写模式打开 (参见 w+ )<br />ab+&#160; &#160; 以二进制读写模式打开 (参见 a+ )</p><p> </p><p>fp.read([size])&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;#size为读取的长度，以byte为单位</p><p>fp.readline([size])&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;#读一行，如果定义了size，有可能返回的只是一行的一部分</p><p>fp.readlines([size])&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; #把文件每一行作为一个list的一个成员，并返回这个list。其实它的内部是通过循环调用readline()来实现的。如果提供size参数，size是表示读取内容的总长，也就是说可能只读到文件的一部分。</p><p>fp.write(str)&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; #把str写到文件中，write()并不会在str后加上一个换行符</p><p>fp.writelines(seq)&#160; &#160; &#160; &#160; &#160; &#160; #把seq的内容全部写到文件中(多行一次性写入)。这个函数也只是忠实地写入，不会在每行后面加上任何东西。</p><p>fp.close()&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; #关闭文件。python会在一个文件不用后自动关闭文件，不过这一功能没有保证，最好还是养成自己关闭的习惯。&#160; 如果一个文件在关闭后还对其进行操作会产生ValueError</p><p>fp.flush()&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; #把缓冲区的内容写入硬盘</p><p>fp.fileno()&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; #返回一个长整型的”文件标签“</p><p>fp.isatty()&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; #文件是否是一个终端设备文件（unix系统中的）</p><p>fp.tell()&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;#返回文件操作标记的当前位置，以文件的开头为原点</p><p>fp.next()&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;#返回下一行，并将文件操作标记位移到下一行。把一个file用于for … in file这样的语句时，就是调用next()函数来实现遍历的。</p><p>fp.seek(offset[,whence])&#160; &#160; &#160; &#160; &#160; &#160; &#160; #将文件打操作标记移到offset的位置。这个offset一般是相对于文件的开头来计算的，一般为正数。但如果提供了whence参数就不一定了，whence可以为0表示从头开始计算，1表示以当前位置为原点计算。2表示以文件末尾为原点进行计算。需要注意，如果文件以a或a+的模式打开，每次进行写操作时，文件操作标记会自动返回到文件末尾。</p><p>fp.truncate([size])&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;#把文件裁成规定的大小，默认的是裁到当前文件操作标记的位置。如果size比文件的大小还要大，依据系统的不同可能是不改变文件，也可能是用0把文件补到相应的大小，也可能是以一些随机的内容加上去。</p><p> </p><p>目录操作：<br />os.mkdir(&quot;file&quot;)&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;创建目录<br />复制文件：<br />shutil.copyfile(&quot;oldfile&quot;,&quot;newfile&quot;)&#160; &#160; &#160; &#160;oldfile和newfile都只能是文件<br />shutil.copy(&quot;oldfile&quot;,&quot;newfile&quot;)&#160; &#160; &#160; &#160; &#160; &#160; oldfile只能是文件夹，newfile可以是文件，也可以是目标目录<br />复制文件夹：<br />shutil.copytree(&quot;olddir&quot;,&quot;newdir&quot;)&#160; &#160; &#160; &#160; olddir和newdir都只能是目录，且newdir必须不存在<br />重命名文件（目录）<br />os.rename(&quot;oldname&quot;,&quot;newname&quot;)&#160; &#160; &#160; &#160;文件或目录都是使用这条命令<br />移动文件（目录）<br />shutil.move(&quot;oldpos&quot;,&quot;newpos&quot;)&#160; &#160;<br />删除文件<br />os.remove(&quot;file&quot;)<br />删除目录<br />os.rmdir(&quot;dir&quot;)只能删除空目录<br />shutil.rmtree(&quot;dir&quot;)&#160; &#160; 空目录、有内容的目录都可以删<br />转换目录<br />os.chdir(&quot;path&quot;)&#160; &#160;换路径</p><p> </p><p>相关例子 </p><p> 1 将文件夹下所有图片名称加上&#039;_fc&#039;</p><p>python代码:</p><p># -*- coding:utf-8 -*-<br />import re<br />import os<br />import time<br />#str.split(string)分割字符串<br />#&#039;连接符&#039;.join(list) 将列表组成字符串<br />def change_name(path):<br />&#160; &#160; global i<br />&#160; &#160; if not os.path.isdir(path) and not os.path.isfile(path):<br />&#160; &#160; &#160; &#160; return False<br />&#160; &#160; if os.path.isfile(path):<br />&#160; &#160; &#160; &#160; file_path = os.path.split(path) #分割出目录与文件<br />&#160; &#160; &#160; &#160; lists = file_path[1].split(&#039;.&#039;) #分割出文件与文件扩展名<br />&#160; &#160; &#160; &#160; file_ext = lists[-1] #取出后缀名(列表切片操作)<br />&#160; &#160; &#160; &#160; img_ext = [&#039;bmp&#039;,&#039;jpeg&#039;,&#039;gif&#039;,&#039;psd&#039;,&#039;png&#039;,&#039;jpg&#039;]<br />&#160; &#160; &#160; &#160; if file_ext in img_ext:<br />&#160; &#160; &#160; &#160; &#160; &#160; os.rename(path,file_path[0]+&#039;/&#039;+lists[0]+&#039;_fc.&#039;+file_ext)<br />&#160; &#160; &#160; &#160; &#160; &#160; i+=1 #注意这里的i是一个陷阱<br />&#160; &#160; &#160; &#160; #或者<br />&#160; &#160; &#160; &#160; #img_ext = &#039;bmp|jpeg|gif|psd|png|jpg&#039;<br />&#160; &#160; &#160; &#160; #if file_ext in img_ext:<br />&#160; &#160; &#160; &#160; #&#160; &#160; print(&#039;ok---&#039;+file_ext)<br />&#160; &#160; elif os.path.isdir(path):<br />&#160; &#160; &#160; &#160; for x in os.listdir(path):<br />&#160; &#160; &#160; &#160; &#160; &#160; change_name(os.path.join(path,x)) #os.path.join()在路径处理上很有用</p><br /><p>img_dir = &#039;D:\\xx\\xx\\images&#039;<br />img_dir = img_dir.replace(&#039;\\&#039;,&#039;/&#039;)<br />start = time.time()<br />i = 0<br />change_name(img_dir)<br />c = time.time() - start<br />print(&#039;程序运行耗时:%0.2f&#039;%(c))<br />print(&#039;总共处理了 %s 张图片&#039;%(i))<br />输出结果：</p><p>程序运行耗时:0.11<br />总共处理了 109 张图片</p><p>本文出处：<br /><a href="http://www.cnblogs.com/rollenholt/archive/2012/04/23/2466179.html" rel="nofollow">http://www.cnblogs.com/rollenholt/archi … 66179.html</a></p>]]></description>
			<author><![CDATA[dummy@example.com (xuyg)]]></author>
			<pubDate>Fri, 03 Jul 2015 03:45:33 +0000</pubDate>
			<guid>http://www.itecfun.com/viewtopic.php?id=3184&amp;action=new</guid>
		</item>
		<item>
			<title><![CDATA[Python logging （日志）]]></title>
			<link>http://www.itecfun.com/viewtopic.php?id=3183&amp;action=new</link>
			<description><![CDATA[<p><a href="http://python.jobbole.com/81666/?utm_source=tuicool" rel="nofollow">每个 Python 程序员都要知道的日志实践</a></p>]]></description>
			<author><![CDATA[dummy@example.com (xuyg)]]></author>
			<pubDate>Fri, 03 Jul 2015 03:06:47 +0000</pubDate>
			<guid>http://www.itecfun.com/viewtopic.php?id=3183&amp;action=new</guid>
		</item>
		<item>
			<title><![CDATA[Python正则表达式]]></title>
			<link>http://www.itecfun.com/viewtopic.php?id=3182&amp;action=new</link>
			<description><![CDATA[<p><a href="http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html" rel="nofollow">Python正则表达式</a></p>]]></description>
			<author><![CDATA[dummy@example.com (xuyg)]]></author>
			<pubDate>Thu, 02 Jul 2015 10:01:42 +0000</pubDate>
			<guid>http://www.itecfun.com/viewtopic.php?id=3182&amp;action=new</guid>
		</item>
		<item>
			<title><![CDATA[BeautifulSoup 方法小结]]></title>
			<link>http://www.itecfun.com/viewtopic.php?id=3181&amp;action=new</link>
			<description><![CDATA[<p>BeautifulSoup是Python的一个第三方库，可用于帮助解析html/XML等内容，以抓取特定的网页信息。目前最新的是v4版本，这里主要总结一下我使用的v3版本解析html的一些常用方法。<br /><strong>1.初始化<br />&#160; &#160;导入模块</strong></p><div class="codebox"><pre><code>#!/usr/bin/env python
from bs4 import BeautifulSoup       #process html
#from BeautifulSoup import BeautifulStoneSoup #process xml
#import BeautifulSoup                         #all</code></pre></div><p> <strong>创建对象：str初始化，常用urllib2或browser返回的html初始化BeautifulSoup对象。<br />doc = [&#039;hello&#039;,&#039;This is paragraph one of ptyhonclub.org.&#039;,&#039;This is paragraph two of pythonclub.org.&#039;,&#039;&#039;]<br />soup = BeautifulSoup(&#039;&#039;.join(doc))<br />指定编码：当html为其他类型编码（非utf-8和asc ii），比如GB2312的话，则需要指定相应的字符编码，BeautifulSoup才能正确解析。</strong></p><div class="codebox"><pre><code>htmlCharset = &quot;GB2312&quot;
soup = BeautifulSoup(respHtml, fromEncoding=htmlCharset)</code></pre></div><p><strong>2.获取tag内容<br />&#160; &#160;寻找感兴趣的tag块内容，返回对应tag块的剖析树</strong></p><div class="codebox"><pre><code>head = soup.find(&#039;head&#039;)
#head = soup.head
#head = soup.contents[0].contents[0]

print head</code></pre></div><p> 返回内容：hello<br /> 说明一下，contents属性是一个列表，里面保存了该剖析树的直接儿子。</p><div class="codebox"><pre><code>html = soup.contents[0]       # &lt;html&gt; ... &lt;/html&gt;
head = html.contents[0]       # &lt;head&gt; ... &lt;/head&gt;
body = html.contents[1]       # &lt;body&gt; ... &lt;/body&gt;</code></pre></div><p><strong><br />3.获取关系节点<br />&#160; &#160;使用parent获取父节点</strong></p><div class="codebox"><pre><code>body = soup.body
html = body.parent             # html是body的父亲</code></pre></div><p>使用nextSibling, previousSibling获取前后兄弟</p><div class="codebox"><pre><code>head = body.previousSibling    # head和body在同一层，是body的前一个兄弟
p1 = body.contents[0]          # p1, p2都是body的儿子，我们用contents[0]取得p1
p2 = p1.nextSibling            # p2与p1在同一层，是p1的后一个兄弟, 当然body.content[1]也可得到</code></pre></div><p> contents[]的灵活运用也可以寻找关系节点,寻找祖先或者子孙可以采用findParent(s), findNextSibling(s), findPreviousSibling(s)<br /><strong><br />4.find/findAll用法详解</strong><br />&#160; &#160;函数原型：find(name=None, attrs={}, recursive=True, text=None, **kwargs)，findAll会返回所有符合要求的结果，并以list返回。<br /> <strong>&#160; tag搜索</strong></p><div class="codebox"><pre><code>find(tagname)                                  # 直接搜索名为tagname的tag 如：find(&#039;head&#039;)
find(list)                                     # 搜索在list中的tag，如: find([&#039;head&#039;, &#039;body&#039;])
find(dict)                                     # 搜索在dict中的tag，如:find({&#039;head&#039;:True, &#039;body&#039;:True})
find(re.compile(&#039;&#039;))                           # 搜索符合正则的tag, 如:find(re.compile(&#039;^p&#039;)) 搜索以p开头的tag
find(lambda)                       # 搜索函数返回结果为true的tag, 如:find(lambda name: if len(name) == 1) 搜索长度为1的tag
find(True)                                     # 搜索所有tag</code></pre></div><p> <strong>&#160; attrs搜索</strong></p><div class="codebox"><pre><code>find(id=&#039;xxx&#039;)                                  # 寻找id属性为xxx的
find(attrs={id=re.compile(&#039;xxx&#039;), algin=&#039;xxx&#039;}) # 寻找id属性符合正则且algin属性为xxx的
find(attrs={id=True, algin=None})               # 寻找有id属性但是没有algin属性的

resp1 = soup.findAll(&#039;a&#039;, attrs = {&#039;href&#039;: match1})
resp2 = soup.findAll(&#039;h1&#039;, attrs = {&#039;class&#039;: match2})
resp3 = soup.findAll(&#039;img&#039;, attrs = {&#039;id&#039;: match3})</code></pre></div><p> <strong>&#160; text搜索</strong><br />&#160; &#160;文字的搜索会导致其他搜索给的值如：tag, attrs都失效。方法与搜索tag一致</p><div class="codebox"><pre><code>print p1.text
# u&#039;This is paragraphone.&#039;
print p2.text
# u&#039;This is paragraphtwo.&#039;
# 注意：1，每个tag的text包括了它以及它子孙的text。2，所有text已经被自动转为unicode，如果需要，可以自行转码encode(xxx)</code></pre></div><p> <strong>&#160; recursive和limit属性</strong><br />&#160; &#160; recursive=False表示只搜索直接儿子，否则搜索整个子树，默认为True。当使用findAll或者类似返回list的方法时，limit属性用于限制返回的数量，如findAll(&#039;p&#039;, limit=2)： 返回首先找到的两个tag</p>]]></description>
			<author><![CDATA[dummy@example.com (xuyg)]]></author>
			<pubDate>Thu, 02 Jul 2015 07:28:39 +0000</pubDate>
			<guid>http://www.itecfun.com/viewtopic.php?id=3181&amp;action=new</guid>
		</item>
		<item>
			<title><![CDATA[python写报警程序中的声音实现winsound]]></title>
			<link>http://www.itecfun.com/viewtopic.php?id=3176&amp;action=new</link>
			<description><![CDATA[<p>写windowns下的报警程序，有一个报警声音的实现，在python中有个winsound模块可以来实现，方法也很简单：<br />1<br />2<br />3<br />4<br />5<br />import time<br />import winsound<br />def play_music():<br />&#160; &#160; winsound.PlaySound(&#039;alert&#039;, winsound.SND_ASYNC)<br />&#160; &#160; time.sleep(3)<br />&#160; &#160;&gt;import winsound<br />&#160; &#160;PlaySound(sound, flags)<br />&#160; &#160;sound是声音文件名字，该文件为wav格式的。flags为其播放的一些参数，如：</p><p>SND_LOOP<br />重复地播放声音。SND_ASYNC标识也必须被用来避免堵塞。不能用 SND_MEMORY。</p><p>SND_MEMORY<br />提供给PlaySound()的 sound 参数是一个 WAV 文件的内存映像(memory image)，作为一个字符串。<br />注意：这个模块不支持从内存映像中异步播放，因此这个标识和 SND_ASYNC 的组合将挂起 RuntimeError。</p><p>SND_PURGE<br />停止播放所有指定声音的实例。</p><p>SND_ASYNC<br />立即返回，允许声音异步播放。</p><p>SND_NODEFAULT<br />不过指定的声音没有找到，不播放系统缺省的声音。</p><p>SND_NOSTOP<br />不中断当前播放的声音。</p><p>SND_NOWAIT<br />如果声音驱动忙立即返回。</p><p>MB_ICONASTERISK<br />播放 SystemDefault 声音。</p><p>MB_ICONEXCLAMATION<br />播放 SystemExclamation 声音。</p><p>MB_ICONHAND<br />播放 SystemHand 声音。</p><p>MB_ICONQUESTION<br />播放 SystemQuestion 声音。</p><p>MB_OK<br />播放 SystemDefault 声音。</p><p>python蜂鸣，通过python让电脑发声:<br />import winsound<br />winsound.Beep(37, 2000)<br />37是频率(Hz), 2000是蜂鸣持续多少毫秒(ms).<br />第一个参数frequency表示分贝数，大小在37到32767之间。第二个参数是持续时间，以毫秒为单位</p><p>本文出自 “王伟” 博客，请务必保留此出处http://wangwei007.blog.51cto.com/68019/1231091</p>]]></description>
			<author><![CDATA[dummy@example.com (xuyg)]]></author>
			<pubDate>Mon, 29 Jun 2015 06:16:20 +0000</pubDate>
			<guid>http://www.itecfun.com/viewtopic.php?id=3176&amp;action=new</guid>
		</item>
	</channel>
</rss>
