﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-sunrise-随笔分类-可爱的python</title><link>http://www.cppblog.com/sunrise/category/18834.html</link><description>每天不断学习，才能不断提升自己。

欢迎交流 QQ：703979707</description><language>zh-cn</language><lastBuildDate>Sat, 16 Mar 2013 06:02:40 GMT</lastBuildDate><pubDate>Sat, 16 Mar 2013 06:02:40 GMT</pubDate><ttl>60</ttl><item><title>turbogear2上传文件功能</title><link>http://www.cppblog.com/sunrise/archive/2013/03/12/198363.html</link><dc:creator>SunRise_at</dc:creator><author>SunRise_at</author><pubDate>Tue, 12 Mar 2013 09:07:00 GMT</pubDate><guid>http://www.cppblog.com/sunrise/archive/2013/03/12/198363.html</guid><wfw:comment>http://www.cppblog.com/sunrise/comments/198363.html</wfw:comment><comments>http://www.cppblog.com/sunrise/archive/2013/03/12/198363.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunrise/comments/commentRss/198363.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunrise/services/trackbacks/198363.html</trackback:ping><description><![CDATA[前台代码：<br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">&lt;</span><span style="color: #800000; ">form&nbsp;&nbsp;</span><span style="color: #FF0000; ">id</span><span style="color: #0000FF; ">="upload"</span><span style="color: #FF0000; ">&nbsp;&nbsp;action</span><span style="color: #0000FF; ">="/designer/uploadandcut"</span><span style="color: #FF0000; ">&nbsp;method</span><span style="color: #0000FF; ">="post"</span><span style="color: #FF0000; ">&nbsp;enctype</span><span style="color: #0000FF; ">="multipart/form-data"</span><span style="color: #0000FF; ">&gt;</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">&lt;</span><span style="color: #800000; ">input&nbsp;</span><span style="color: #FF0000; ">type</span><span style="color: #0000FF; ">="file"</span><span style="color: #FF0000; ">&nbsp;name</span><span style="color: #0000FF; ">="headimage"</span><span style="color: #FF0000; ">&nbsp;id</span><span style="color: #0000FF; ">="headimage"</span><span style="color: #FF0000; ">&nbsp;style</span><span style="color: #0000FF; ">=&nbsp;"display:none"</span><span style="color: #0000FF; ">/&gt;</span><br /><span style="color: #0000FF; ">&lt;/</span><span style="color: #800000; ">form</span><span style="color: #0000ff;">&gt;</span>&nbsp; &nbsp;<br /><br /><br /><br /><br />后台代码：<br /><br /><div>&nbsp; &nbsp;#上传和剪裁</div><div>&nbsp; &nbsp;@expose()</div><div>&nbsp; &nbsp;def uploadandcut(self,username,headimage):</div><div>&nbsp; &nbsp; &nbsp; &nbsp;print 'run'</div><div>&nbsp; &nbsp; &nbsp; &nbsp;permanent_file = open(os.getcwd()+"\\designerpage\\public\\"+username+"\\headimage\\headimage.png",'wb')</div><div>&nbsp; &nbsp; &nbsp; &nbsp;print permanent_file</div><div>&nbsp; &nbsp; &nbsp; &nbsp;shutil.copyfileobj(headimage.file,permanent_file)</div><div>&nbsp; &nbsp; &nbsp; &nbsp;headimage.file.close()</div><div>&nbsp; &nbsp; &nbsp; &nbsp;permanent_file.close()</div><div>&nbsp; &nbsp; &nbsp; &nbsp;redirect("/designer/designerMyIndex")</div></div><img src ="http://www.cppblog.com/sunrise/aggbug/198363.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunrise/" target="_blank">SunRise_at</a> 2013-03-12 17:07 <a href="http://www.cppblog.com/sunrise/archive/2013/03/12/198363.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于PIL库的一些概念</title><link>http://www.cppblog.com/sunrise/archive/2013/03/02/198174.html</link><dc:creator>SunRise_at</dc:creator><author>SunRise_at</author><pubDate>Sat, 02 Mar 2013 08:04:00 GMT</pubDate><guid>http://www.cppblog.com/sunrise/archive/2013/03/02/198174.html</guid><wfw:comment>http://www.cppblog.com/sunrise/comments/198174.html</wfw:comment><comments>http://www.cppblog.com/sunrise/archive/2013/03/02/198174.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunrise/comments/commentRss/198174.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunrise/services/trackbacks/198174.html</trackback:ping><description><![CDATA[<p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;"><strong>转自：</strong>http://www.iteye.com/topic/314790<strong><br /></strong><strong>pil能处理的图片</strong><strong>类型</strong><br />pil可以处理光栅图片(像素数据组成的的块)。<br /><br /><strong>通道</strong><br />一个图片可以包含一到多个数据通道，如果这些通道具有相同的维数和深度，Pil允许将这些通道进行叠加<br /><br /><strong>模式</strong></p><table width="322" border="1" style="color: #000000; font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px; background-color: #efefef; border-color: #000000; height: 207px;"><tbody><tr><td style="font-size: 1em; padding: 5px;">1</td><td style="font-size: 1em; padding: 5px;">&nbsp;1位像素，黑和白，存成8位的像素</td></tr><tr><td style="font-size: 1em; padding: 5px;">L</td><td style="font-size: 1em; padding: 5px;">&nbsp;8位像素，黑白</td></tr><tr><td style="font-size: 1em; padding: 5px;">P</td><td style="font-size: 1em; padding: 5px;">&nbsp;8位像素，使用调色板映射到任何其他模式</td></tr><tr><td style="font-size: 1em; padding: 5px;">RGB</td><td style="font-size: 1em; padding: 5px;">&nbsp;3&#215;8位像素，真彩</td></tr><tr><td style="font-size: 1em; padding: 5px;">RGBA</td><td style="font-size: 1em; padding: 5px;">&nbsp;4&#215;8位像素，真彩+透明通道</td></tr><tr><td style="font-size: 1em; padding: 5px;">CMYK</td><td style="font-size: 1em; padding: 5px;">&nbsp;4&#215;8位像素，颜色隔离</td></tr><tr><td style="font-size: 1em; padding: 5px;">YCbCr</td><td style="font-size: 1em; padding: 5px;">&nbsp;3&#215;8位像素，彩色视频格式</td></tr><tr><td style="font-size: 1em; padding: 5px;">I</td><td style="font-size: 1em; padding: 5px;">&nbsp;32位整型像素</td></tr><tr><td style="font-size: 1em; padding: 5px;">F</td><td style="font-size: 1em; padding: 5px;">&nbsp;32位浮点型像素</td></tr><tr><td style="font-size: 1em; padding: 5px;"><br /></td><td style="font-size: 1em; padding: 5px;"><br /></td></tr></tbody></table><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;"><br /><br /><strong>尺寸</strong><br />通过图片对象的size属性可以得到图片的尺寸，结果这是一个二元组，包含水平和垂直方向上的像素数。<br /><br /><strong>坐标</strong><br />Pil采取左上角为(0,0)的坐标系统<br /><br /><strong>调色板</strong><br />mode("P")为每个像素定义具体的颜色值<br /><br /><strong>图片信息</strong><br />可以通过info属性读取一张图片的附加信息，这个与图片的格式有关。<br /><br /><strong>滤镜</strong><br />在对图片的几何操作中可能会将多个输入像素映射到单个的输出像素，pil提供4种不同的采样滤镜(在目前的版本中，后续的版本可能支持更多)</p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;">&nbsp;</p><table border="1" style="color: #000000; font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px; background-color: #efefef; border-color: #000000;"><tbody><tr><td style="font-size: 1em; padding: 5px;">NEAREST</td><td style="font-size: 1em; padding: 5px;">最近</td></tr><tr><td style="font-size: 1em; padding: 5px;">BILINEAR</td><td style="font-size: 1em; padding: 5px;">双线型</td></tr><tr><td style="font-size: 1em; padding: 5px;">BICUBIC</td><td style="font-size: 1em; padding: 5px;">双三次插值</td></tr><tr><td style="font-size: 1em; padding: 5px;">ANTIALIAS</td><td style="font-size: 1em; padding: 5px;">平滑</td></tr></tbody></table><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;">&nbsp;</p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;">&nbsp;</p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;">在RGB模式下，每个图片由三个通道叠加而成，每个模式下为一个灰度图，当有一个调色板来调色的时候，这三张灰度图的叠加即可合成3*8位(每个像素)的一个真彩图片。pil库中，图片之间的模式(mode)可以转化。下面给出一些简单的例子，例子中的所有图片均来自于国家地理的官网，为了使得文档比较短小，每个图片均使用Pil缩放成1/2大小，如有侵权嫌疑，请尽快联系，我会删除这些图片。<br /><br />所有的图片操作必须有一个操作对象，Pil提供<em>open(filename)</em>进行这个过程</p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;"><br /><strong>打开图片</strong><br />1.导入pil的Image模块<br />2.使用open(filename)打开文件，返回一个image对象</p><div id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: #efefef; width: 766.296875px; margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word; line-height: 25.1875px;"><div><div style="padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin: 0px; font-weight: bold;">Python代码&nbsp;<embed wmode="transparent" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=im%20%3D%20Image.open('filename')" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="" title="收藏这段代码" style="color: #006699;"><img src="http://www.iteye.com/images/icon_star.png" alt="收藏代码" style="border: 0px;" /></a></div></div><ol start="1" style="font-size: 1em; line-height: 1.4em; margin-left: 0px; padding-top: 2px; padding-bottom: 2px; border: 1px solid #d1d7dc; color: #2b91af;"><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;">im&nbsp;=&nbsp;Image.open('filename')&nbsp;&nbsp;</li></ol></div><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;"><br />此后，一切关于图片的操作均基于这个对象。</p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;"><br /><img alt="" src="http://www.iteye.com/upload/attachment/67907/9d59e028-a64d-38b5-86a9-17f1c458d4e5.jpg" style="border: 0px;" /><br />&nbsp;<br /><br />打开后，我们可以查看一些图片信息，如im.format, im.size, im.mode等。调用im.show()会在图片查看工具中显示当前操作的image对象，这个跟个人的系统有关系，我系统中默认是用Windows Picture and Fax Viewer打开的。这个方法用来查看临时的图片效果。<br /><br /><strong>读写图片</strong><br />pil中转换图片格式非常简单(转换图片模式是另一个概念，不要混淆)，只需要调用img.save(filename)即可比如有一个bmp(位图)图片，使用img = Image.open('file.bmp')打开后，只需要img.save('file.jpg')即可转换。不过一般情况下，save(filename)是不用做这个用途的，通常，save用以保存一个临时的image对象到硬盘。而转换工作由一个功能更为强大的convert()方法来完成。<br /><br /><strong><br />拷贝，粘贴，合并</strong></p><div id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: #efefef; width: 766.296875px; margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word; line-height: 25.1875px;"><div><div style="padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin: 0px; font-weight: bold;">Python代码&nbsp;<embed wmode="transparent" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=box%20%3D%20(100%2C100%2C500%2C500)%23%E8%AE%BE%E7%BD%AE%E8%A6%81%E6%8B%B7%E8%B4%9D%E7%9A%84%E5%8C%BA%E5%9F%9F%0A%0A%23%E5%B0%86im%E8%A1%A8%E7%A4%BA%E7%9A%84%E5%9B%BE%E7%89%87%E5%AF%B9%E8%B1%A1%E6%8B%B7%E8%B4%9D%E5%88%B0region%E4%B8%AD%EF%BC%8C%E5%A4%A7%E5%B0%8F%E4%B8%BA(400*400)%E5%83%8F%E7%B4%A0%E3%80%82%E8%BF%99%E4%B8%AAregion%E5%8F%AF%E4%BB%A5%E7%94%A8%E6%9D%A5%E5%90%8E%E7%BB%AD%E7%9A%84%E6%93%8D%E4%BD%9C(region%E5%85%B6%E5%AE%9E%E5%B0%B1%E6%98%AF%E4%B8%80%E4%B8%AAImage%E5%AF%B9%E8%B1%A1)%EF%BC%8Cbox%E5%8F%98%E9%87%8F%E6%98%AF%E4%B8%80%E4%B8%AA%E5%9B%9B%E5%85%83%E7%BB%84(%E5%B7%A6%EF%BC%8C%E4%B8%8A%EF%BC%8C%E5%8F%B3%EF%BC%8C%E4%B8%8B)%E3%80%82%0Aregion%20%3D%20im.crop(box)%0A%0Aregion%20%3D%20region.transpose(Image.ROTATE_180)%23%E4%BB%8E%E5%AD%97%E9%9D%A2%E4%B8%8A%E5%B0%B1%E5%8F%AF%E4%BB%A5%E7%9C%8B%E5%87%BA%EF%BC%8C%E5%85%88%E6%8A%8Aregion%E4%B8%AD%E7%9A%84Image%E5%8F%8D%E8%BD%AC180%E5%BA%A6%EF%BC%8C%E7%84%B6%E5%90%8E%E5%86%8D%E6%94%BE%E5%9B%9E%E5%88%B0region%E4%B8%AD%E3%80%82%0Aim.paste(region%2C%20box)%23%E7%B2%98%E8%B4%B4box%E5%A4%A7%E5%B0%8F%E7%9A%84region%E5%88%B0%E5%8E%9F%E5%85%88%E7%9A%84%E5%9B%BE%E7%89%87%E5%AF%B9%E8%B1%A1%E4%B8%AD%E3%80%82" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="" title="收藏这段代码" style="color: #006699;"><img src="http://www.iteye.com/images/icon_star.png" alt="收藏代码" style="border: 0px;" /></a></div></div><ol start="1" style="font-size: 1em; line-height: 1.4em; margin-left: 0px; padding-top: 2px; padding-bottom: 2px; border: 1px solid #d1d7dc; color: #2b91af;"><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;">box&nbsp;=&nbsp;(100,100,500,500)<span style="width: auto; border-width: 0px;">#设置要拷贝的区域</span>&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;">&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="width: auto; border-width: 0px;">#将im表示的图片对象拷贝到region中，大小为(400*400)像素。这个region可以用来后续的操作(region其实就是一个Image对象)，box变量是一个四元组(左，上，右，下)。</span>&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;">region&nbsp;=&nbsp;im.crop(box)&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;">&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;">region&nbsp;=&nbsp;region.transpose(Image.ROTATE_180)<span style="width: auto; border-width: 0px;">#从字面上就可以看出，先把region中的Image反转180度，然后再放回到region中。</span>&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;">im.paste(region,&nbsp;box)<span style="width: auto; border-width: 0px;">#粘贴box大小的region到原先的图片对象中。</span>&nbsp;&nbsp;</li></ol></div><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;">&nbsp;<br /><img alt="" src="http://www.iteye.com/upload/attachment/67929/23e23659-c9d6-391a-a5c0-642a0da0da3b.jpg" style="border: 0px;" /></p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;">前面说过，每一个RGB都是由三个通道的灰度图叠加的，所以pil提供了将这三个通道分离的方法</p><div id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: #efefef; width: 766.296875px; margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word; line-height: 25.1875px;"><div><div style="padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin: 0px; font-weight: bold;">Python代码&nbsp;<embed wmode="transparent" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=r%2Cg%2Cb%20%3D%20im.split()%23%E5%88%86%E5%89%B2%E6%88%90%E4%B8%89%E4%B8%AA%E9%80%9A%E9%81%93%0Ar.show()%0Ag.show()%0Ab.show()%0Aim%20%3D%20Image.merge(%22RGB%22%2C%20(b%2C%20g%2C%20r))%23%E5%B0%86b%2Cr%E4%B8%A4%E4%B8%AA%E9%80%9A%E9%81%93%E8%BF%9B%E8%A1%8C%E7%BF%BB%E8%BD%AC%E3%80%82" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="" title="收藏这段代码" style="color: #006699;"><img src="http://www.iteye.com/images/icon_star.png" alt="收藏代码" style="border: 0px;" /></a></div></div><ol start="1" style="font-size: 1em; line-height: 1.4em; margin-left: 0px; padding-top: 2px; padding-bottom: 2px; border: 1px solid #d1d7dc; color: #2b91af;"><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;">r,g,b&nbsp;=&nbsp;im.split()<span style="width: auto; border-width: 0px;">#分割成三个通道</span>&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;">r.show()&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;">g.show()&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;">b.show()&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;">im&nbsp;=&nbsp;Image.merge("RGB",&nbsp;(b,&nbsp;g,&nbsp;r))<span style="width: auto; border-width: 0px;">#将b,r两个通道进行翻转。</span>&nbsp;&nbsp;</li></ol></div><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;">&nbsp;<br /><img alt="" src="http://www.iteye.com/upload/attachment/67917/aded3d14-6b02-3c4e-8b6b-721e6c0513ac.jpg" style="border: 0px;" /></p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;">红色通道的灰度图</p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;"><br /><img alt="" src="http://www.iteye.com/upload/attachment/67915/ab65be22-fd70-3a83-876e-7540d50b0516.jpg" style="border: 0px;" /><br />&nbsp;绿色通道的灰度图</p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;"><br /><img alt="" src="http://www.iteye.com/upload/attachment/67913/78d2e95c-de7e-3435-aff2-3a553fd9199d.jpg" style="border: 0px;" /><br />&nbsp;蓝色通道的灰度图</p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;">&nbsp;</p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;"><br /><img alt="" src="http://www.iteye.com/upload/attachment/67921/228d018c-3a4c-382d-911f-e363616f7d2c.jpg" style="border: 0px;" /><br />&nbsp;<br />&nbsp;互换红蓝通道后的合成图</p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;">&nbsp;</p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;"><strong>几何转变</strong><br />几何转变提供resize,rotate等方法，用以重定义图片大小，对图片进行旋转等操作，在实际应用中比较广泛。<br /><br />如</p><div id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: #efefef; width: 766.296875px; margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word; line-height: 25.1875px;"><div><div style="padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin: 0px; font-weight: bold;">Python代码&nbsp;<embed wmode="transparent" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=%20%20%20out%20%3D%20img.resize((128%2C%20128))%23resize%E6%88%90128*128%E5%83%8F%E7%B4%A0%E5%A4%A7%E5%B0%8F%E3%80%82%0A%20%20%20out%20%3D%20img.rotate(45)%23%E9%80%86%E6%97%B6%E9%92%88%E6%97%8B%E8%BD%AC45%E5%BA%A6" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="" title="收藏这段代码" style="color: #006699;"><img src="http://www.iteye.com/images/icon_star.png" alt="收藏代码" style="border: 0px;" /></a></div></div><ol start="1" style="font-size: 1em; line-height: 1.4em; margin-left: 0px; padding-top: 2px; padding-bottom: 2px; border: 1px solid #d1d7dc; color: #2b91af;"><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;">out&nbsp;=&nbsp;img.resize((128,&nbsp;128))<span style="width: auto; border-width: 0px;">#resize成128*128像素大小。</span>&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;">out&nbsp;=&nbsp;img.rotate(45)<span style="width: auto; border-width: 0px;">#逆时针旋转45度</span>&nbsp;&nbsp;</li></ol></div><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;"><br /><img alt="" src="http://www.iteye.com/upload/attachment/67931/d78bd43c-edff-34f1-8e24-ff4864ad7ebf.jpg" style="border: 0px;" /></p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;">逆时针45度</p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;"><br /><img alt="" src="http://www.iteye.com/upload/attachment/67923/153f70f2-ef4a-305b-8f19-1c5c50afed03.jpg" style="border: 0px;" /><br />&nbsp;<br />&nbsp;镜面效果，左右翻转</p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;">transpose()方法预定义了一些旋转方式，如<br />左右反转，上下翻转，逆时针旋转(90,180,270)度等，非常方便，rotate()和transpose()方法在表现上没有任何不同。<br /><br /><strong>图片加强</strong></p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;"><br /><em>滤镜</em><br />ImageFilter模块提供了很多预定义的图片加强滤镜。<br />比如一个常用的滤镜,细节(detail滤镜)</p><div id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: #efefef; width: 766.296875px; margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word; line-height: 25.1875px;"><div><div style="padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin: 0px; font-weight: bold;">Python代码&nbsp;<embed wmode="transparent" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=import%20ImageFilter%0Aout%20%3D%20im.filter(ImageFilter.DETAIL)" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="" title="收藏这段代码" style="color: #006699;"><img src="http://www.iteye.com/images/icon_star.png" alt="收藏代码" style="border: 0px;" /></a></div></div><ol start="1" style="font-size: 1em; line-height: 1.4em; margin-left: 0px; padding-top: 2px; padding-bottom: 2px; border: 1px solid #d1d7dc; color: #2b91af;"><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: #7f0055;">import</span>&nbsp;ImageFilter&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;">out&nbsp;=&nbsp;im.filter(ImageFilter.DETAIL)&nbsp;&nbsp;</li></ol></div><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;"><br /><img alt="" src="http://www.iteye.com/upload/attachment/67927/15d69cfb-4f77-35f2-a71f-30b2943d70da.jpg" style="border: 0px;" /><br />&nbsp;<br /><br /><em>直接操作像素点</em><br />不但可以对每个像素点进行操作，而且，每一个通道都可以独立的进行操作。比如，将每个像素点的亮度(不知道有没有更专业的词)增大20%</p><div id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: #efefef; width: 766.296875px; margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word; line-height: 25.1875px;"><div><div style="padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin: 0px; font-weight: bold;">Python代码&nbsp;<embed wmode="transparent" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=out%20%3D%20img.point(lambda%20i%20%3A%20i%20*%201.2)%23%E6%B3%A8%E6%84%8F%E8%BF%99%E9%87%8C%E7%94%A8%E5%88%B0%E4%B8%80%E4%B8%AA%E5%8C%BF%E5%90%8D%E5%87%BD%E6%95%B0(%E9%82%A3%E4%B8%AA%E5%8F%AF%E4%BB%A5%E6%8A%8Ai%E7%9A%841.2%E5%80%8D%E8%BF%94%E5%9B%9E%E7%9A%84%E5%87%BD%E6%95%B0)" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="" title="收藏这段代码" style="color: #006699;"><img src="http://www.iteye.com/images/icon_star.png" alt="收藏代码" style="border: 0px;" /></a></div></div><ol start="1" style="font-size: 1em; line-height: 1.4em; margin-left: 0px; padding-top: 2px; padding-bottom: 2px; border: 1px solid #d1d7dc; color: #2b91af;"><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;">out&nbsp;=&nbsp;img.point(<span style="color: #7f0055;">lambda</span>&nbsp;i&nbsp;:&nbsp;i&nbsp;*&nbsp;1.2)<span style="width: auto; border-width: 0px;">#注意这里用到一个匿名函数(那个可以把i的1.2倍返回的函数)</span>&nbsp;&nbsp;</li></ol></div><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;"><br /><img alt="" src="http://www.iteye.com/upload/attachment/67919/ef666066-02b8-3fb1-b8d7-ec0062f57c44.jpg" style="border: 0px;" /><br />对每个点都做20%的增强</p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;">&nbsp;</p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;">如上边的那个例子，我们可以将一个RGB模式的图分离成三个通道的层</p><div id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: #efefef; width: 766.296875px; margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word; line-height: 25.1875px;"><div><div style="padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin: 0px; font-weight: bold;">Python代码&nbsp;<embed wmode="transparent" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=r%2Cg%2Cb%20%3D%20img.split()%23%E7%A5%9E%E5%A5%87%E8%80%8C%E5%8F%88%E5%BC%BA%E5%A4%A7%E7%9A%84python%E8%AF%AD%E6%B3%95" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="" title="收藏这段代码" style="color: #006699;"><img src="http://www.iteye.com/images/icon_star.png" alt="收藏代码" style="border: 0px;" /></a></div></div><ol start="1" style="font-size: 1em; line-height: 1.4em; margin-left: 0px; padding-top: 2px; padding-bottom: 2px; border: 1px solid #d1d7dc; color: #2b91af;"><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;">r,g,b&nbsp;=&nbsp;img.split()<span style="width: auto; border-width: 0px;">#神奇而又强大的python语法</span>&nbsp;&nbsp;</li></ol></div><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;"><br /><br />然后对一个通道进行加强或减弱操作，完成后我们又可以使用Merge将通道合并，从而改变图片的色调(冷暖色调的互换)等。<br /><br />更高级的图片加强，可以使用ImageEnhance模块，其中包含了大量的预定义的图片加强方式。</p><div id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: #efefef; width: 766.296875px; margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word; line-height: 25.1875px;"><div><div style="padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin: 0px; font-weight: bold;">Python代码&nbsp;<embed wmode="transparent" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=import%20ImageEnhance%0Aenh%20%3D%20ImageEnhance.Contrast(im)%0Aenh.ehhance(1.5).show(%2250%25%20more%20contrast%22)" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="" title="收藏这段代码" style="color: #006699;"><img src="http://www.iteye.com/images/icon_star.png" alt="收藏代码" style="border: 0px;" /></a></div></div><ol start="1" style="font-size: 1em; line-height: 1.4em; margin-left: 0px; padding-top: 2px; padding-bottom: 2px; border: 1px solid #d1d7dc; color: #2b91af;"><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: #7f0055;">import</span>&nbsp;ImageEnhance&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;">enh&nbsp;=&nbsp;ImageEnhance.Contrast(im)&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;">enh.ehhance(1.5).show("50%&nbsp;more&nbsp;contrast")&nbsp;&nbsp;</li></ol></div><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;"><br /><img alt="" src="http://www.iteye.com/upload/attachment/67925/32acbe28-f089-31a6-990f-3d0e4a790486.jpg" style="border: 0px;" /></p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;">&nbsp;</p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;"><em>读写图片的更多方式</em><br />通常，我们使用open方法进行图片的打开操作。但是这不是唯一的方式。完全可以跟python的IO整合起来。如</p><div id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: #efefef; width: 766.296875px; margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word; line-height: 25.1875px;"><div><div style="padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin: 0px; font-weight: bold;">Python代码&nbsp;<embed wmode="transparent" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=fp%20%3D%20open(%22file.jpg%22%2C%20%22rb%22)%0Aim%20%3D%20Image.open(fp)" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="" title="收藏这段代码" style="color: #006699;"><img src="http://www.iteye.com/images/icon_star.png" alt="收藏代码" style="border: 0px;" /></a></div></div><ol start="1" style="font-size: 1em; line-height: 1.4em; margin-left: 0px; padding-top: 2px; padding-bottom: 2px; border: 1px solid #d1d7dc; color: #2b91af;"><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;">fp&nbsp;=&nbsp;open("file.jpg",&nbsp;"rb")&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;">im&nbsp;=&nbsp;Image.open(fp)&nbsp;&nbsp;</li></ol></div><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;">&nbsp;</p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;">甚至，你可以从一个字符串中读出图片数据来(<span style="color: #3366ff;">python真是神奇啊</span>)。</p><div id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: #efefef; width: 766.296875px; margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word; line-height: 25.1875px;"><div><div style="padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin: 0px; font-weight: bold;">Python代码&nbsp;<embed wmode="transparent" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=import%20StringIO%0Aimg%20%3D%20Image.open(StringIO.StringIO(buffer))" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="" title="收藏这段代码" style="color: #006699;"><img src="http://www.iteye.com/images/icon_star.png" alt="收藏代码" style="border: 0px;" /></a></div></div><ol start="1" style="font-size: 1em; line-height: 1.4em; margin-left: 0px; padding-top: 2px; padding-bottom: 2px; border: 1px solid #d1d7dc; color: #2b91af;"><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;"><span style="color: #7f0055;">import</span>&nbsp;StringIO&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px;">img&nbsp;=&nbsp;Image.open(StringIO.StringIO(buffer))&nbsp;&nbsp;</li></ol></div><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;"><br /><span style="text-decoration: underline;">(文中很多内容来自pil的handbook，但是原手册比较学院派，没有一张图片来进行说明，因此不免枯燥，我给每个小例子都加上一个真实的图片作为说明，希望看起来不那没抽象。)</span></p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;">&nbsp;</p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25.1875px; background-color: #efefef;">当然，PIL的功能远不止这些，由于篇幅原因，我这次先写这么多吧，后面会给出PIL中各个模块中函数的详细使用说明，同时尽量配以实图来说明。</p><img src ="http://www.cppblog.com/sunrise/aggbug/198174.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunrise/" target="_blank">SunRise_at</a> 2013-03-02 16:04 <a href="http://www.cppblog.com/sunrise/archive/2013/03/02/198174.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>python的默认参数</title><link>http://www.cppblog.com/sunrise/archive/2013/01/17/197350.html</link><dc:creator>SunRise_at</dc:creator><author>SunRise_at</author><pubDate>Thu, 17 Jan 2013 09:43:00 GMT</pubDate><guid>http://www.cppblog.com/sunrise/archive/2013/01/17/197350.html</guid><wfw:comment>http://www.cppblog.com/sunrise/comments/197350.html</wfw:comment><comments>http://www.cppblog.com/sunrise/archive/2013/01/17/197350.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunrise/comments/commentRss/197350.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunrise/services/trackbacks/197350.html</trackback:ping><description><![CDATA[刚在群里一个人问的问题，前两天正好看过，记录一下。
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #800080">__author__</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">chenyu</span><span style="color: #800000">'</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">def</span><span style="color: #000000">&nbsp;f(i,L</span><span style="color: #000000">=</span><span style="color: #000000">[]):<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;L.append(i)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;L<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">def</span><span style="color: #000000">&nbsp;f2(i,L</span><span style="color: #000000">=</span><span style="color: #000000">None):<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;L&nbsp;&nbsp;</span><span style="color: #0000ff">is</span><span style="color: #000000">&nbsp;None:<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;L&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;[]<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;L.append(i)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;L<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">print</span><span style="color: #000000">&nbsp;f(</span><span style="color: #000000">1</span><span style="color: #000000">)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">print</span><span style="color: #000000">&nbsp;f(</span><span style="color: #000000">2</span><span style="color: #000000">)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">print</span><span style="color: #000000">&nbsp;f(</span><span style="color: #000000">3</span><span style="color: #000000">)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">print</span><span style="color: #000000">&nbsp;f2(</span><span style="color: #000000">1</span><span style="color: #000000">)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">print</span><span style="color: #000000">&nbsp;f2(</span><span style="color: #000000">2</span><span style="color: #000000">)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">print</span><span style="color: #000000">&nbsp;f2(</span><span style="color: #000000">3</span><span style="color: #000000">)</span></div>输出：<br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">[</span><span style="color: #000000">1</span><span style="color: #000000">]<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />[</span><span style="color: #000000">1</span><span style="color: #000000">, </span><span style="color: #000000">2</span><span style="color: #000000">]<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />[</span><span style="color: #000000">1</span><span style="color: #000000">, </span><span style="color: #000000">2</span><span style="color: #000000">, </span><span style="color: #000000">3</span><span style="color: #000000">]<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />[</span><span style="color: #000000">1</span><span style="color: #000000">]<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />[</span><span style="color: #000000">2</span><span style="color: #000000">]<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />[</span><span style="color: #000000">3</span><span style="color: #000000">]</span></div>按照预想的结果f()输出的应该是[1],[2],[3],但是每次上次的结果都被保留下来了。<br />造成这样的原因是python支持函数中的参数默认值是一个可变的list, 函数体内修改了原来的默认值，而python会将修改后的值一直保留，并作为下次函数调用时的参数默认值。<img src ="http://www.cppblog.com/sunrise/aggbug/197350.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunrise/" target="_blank">SunRise_at</a> 2013-01-17 17:43 <a href="http://www.cppblog.com/sunrise/archive/2013/01/17/197350.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Google Translate API </title><link>http://www.cppblog.com/sunrise/archive/2013/01/17/197348.html</link><dc:creator>SunRise_at</dc:creator><author>SunRise_at</author><pubDate>Thu, 17 Jan 2013 08:59:00 GMT</pubDate><guid>http://www.cppblog.com/sunrise/archive/2013/01/17/197348.html</guid><wfw:comment>http://www.cppblog.com/sunrise/comments/197348.html</wfw:comment><comments>http://www.cppblog.com/sunrise/archive/2013/01/17/197348.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunrise/comments/commentRss/197348.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunrise/services/trackbacks/197348.html</trackback:ping><description><![CDATA[<div>近期在做的一个任务，但是网上的资料甚少，经过了几番波折，终于弄好了。实现过程很简单，拿出来共享一下，免得大家像我一样找了半天。<br /><br />谷歌翻译升级到2.0后变为收费版本的，因此首先需要向谷歌申请一个key。付费标准是<span style="widows: 2; text-transform: none; background-color: rgb(250,250,250); text-indent: 0px; letter-spacing: normal; display: inline !important; font: 14px/25px Verdana, Arial, Helvetica, sans-serif; white-space: normal; orphans: 2; float: none; color: #000000; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">每100万个文本字符20美元（觉得小贵啦）。<br /><br />谷歌翻译的工作过程：<br />1.谷歌翻译的请求网址： 
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">https:</span><span style="color: #000000">//</span><span style="color: #000000">www.googleapis.com</span><span style="color: #000000">/</span><span style="color: #000000">language</span><span style="color: #000000">/</span><span style="color: #000000">translate</span><span style="color: #000000">/</span><span style="color: #000000">v2?{parameters}</span></div>翻译请求的三个参数：<br />1)API key:需要向谷歌付费后，会给你一个key;<br />2)Target language:即你需要翻译的目标语言;<br />3)Source text string:需要翻译的文本(需要少于2k)。<br /><br />2.下面是给不同的参数的一些例子，可以根据自己的需要来对该url进行传参：<br />1)指定source和target<br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">https:</span><span style="color: #000000">//</span><span style="color: #000000">www.googleapis.com</span><span style="color: #000000">/</span><span style="color: #000000">language</span><span style="color: #000000">/</span><span style="color: #000000">translate</span><span style="color: #000000">/</span><span style="color: #000000">v2?key</span><span style="color: #000000">=</span><span style="color: #000000">INSERT</span><span style="color: #000000">-</span><span style="color: #000000">YOUR</span><span style="color: #000000">-</span><span style="color: #000000">KEY</span><span style="color: #000000">&amp;</span><span style="color: #000000">source</span><span style="color: #000000">=</span><span style="color: #000000">en</span><span style="color: #000000">&amp;</span><span style="color: #000000">target</span><span style="color: #000000">=</span><span style="color: #000000">de</span><span style="color: #000000">&amp;</span><span style="color: #000000">q</span><span style="color: #000000">=</span><span style="color: #000000">Hello</span><span style="color: #000000">%</span><span style="color: #000000">20world</span></div>JSON<br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> </span><span style="color: #800000">"</span><span style="color: #800000">data</span><span style="color: #800000">"</span><span style="color: #000000">: {<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> </span><span style="color: #800000">"</span><span style="color: #800000">translations</span><span style="color: #800000">"</span><span style="color: #000000">: [<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> {<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> </span><span style="color: #800000">"</span><span style="color: #800000">translatedText</span><span style="color: #800000">"</span><span style="color: #000000">: </span><span style="color: #800000">"</span><span style="color: #800000">Hallo Welt</span><span style="color: #800000">"</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> }<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> ]<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> }<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />}</span></div>2)传入多个q,就对多段文本进行翻译<br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">https:</span><span style="color: #000000">//</span><span style="color: #000000">www.googleapis.com</span><span style="color: #000000">/</span><span style="color: #000000">language</span><span style="color: #000000">/</span><span style="color: #000000">translate</span><span style="color: #000000">/</span><span style="color: #000000">v2?key</span><span style="color: #000000">=</span><span style="color: #000000">INSERT</span><span style="color: #000000">-</span><span style="color: #000000">YOUR</span><span style="color: #000000">-</span><span style="color: #000000">KEY</span><span style="color: #000000">&amp;</span><span style="color: #000000">source</span><span style="color: #000000">=</span><span style="color: #000000">en</span><span style="color: #000000">&amp;</span><span style="color: #000000">target</span><span style="color: #000000">=</span><span style="color: #000000">de</span><span style="color: #000000">&amp;</span><span style="color: #000000">q</span><span style="color: #000000">=</span><span style="color: #000000">Hello</span><span style="color: #000000">%</span><span style="color: #000000">20world</span><span style="color: #000000">&amp;</span><span style="color: #000000">q</span><span style="color: #000000">=</span><span style="color: #000000">My</span><span style="color: #000000">%</span><span style="color: #000000">20name</span><span style="color: #000000">%</span><span style="color: #000000">20</span><span style="color: #0000ff">is</span><span style="color: #000000">%</span><span style="color: #000000">20Jeff</span></div>JSON<br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> </span><span style="color: #800000">"</span><span style="color: #800000">data</span><span style="color: #800000">"</span><span style="color: #000000">: {<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> </span><span style="color: #800000">"</span><span style="color: #800000">translations</span><span style="color: #800000">"</span><span style="color: #000000">: [<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> {<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> </span><span style="color: #800000">"</span><span style="color: #800000">translatedText</span><span style="color: #800000">"</span><span style="color: #000000">: </span><span style="color: #800000">"</span><span style="color: #800000">Hallo Welt</span><span style="color: #800000">"</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> },<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> {<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> </span><span style="color: #800000">"</span><span style="color: #800000">translatedText</span><span style="color: #800000">"</span><span style="color: #000000">: </span><span style="color: #800000">"</span><span style="color: #800000">Mein Name ist Jeff</span><span style="color: #800000">"</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;}<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> ]<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> }<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />}</span></div>3)不指定source,直接翻译成目标语言<br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">https:</span><span style="color: #000000">//</span><span style="color: #000000">www.googleapis.com</span><span style="color: #000000">/</span><span style="color: #000000">language</span><span style="color: #000000">/</span><span style="color: #000000">translate</span><span style="color: #000000">/</span><span style="color: #000000">v2?key</span><span style="color: #000000">=</span><span style="color: #000000">INSERT</span><span style="color: #000000">-</span><span style="color: #000000">YOUR</span><span style="color: #000000">-</span><span style="color: #000000">KEY</span><span style="color: #000000">&amp;</span><span style="color: #000000">target</span><span style="color: #000000">=</span><span style="color: #000000">de</span><span style="color: #000000">&amp;</span><span style="color: #000000">q</span><span style="color: #000000">=</span><span style="color: #000000">Hello</span><span style="color: #000000">%</span><span style="color: #000000">20world</span></div>JSON<br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> </span><span style="color: #800000">"</span><span style="color: #800000">data</span><span style="color: #800000">"</span><span style="color: #000000">: {<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> </span><span style="color: #800000">"</span><span style="color: #800000">translations</span><span style="color: #800000">"</span><span style="color: #000000">: [<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> {<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> </span><span style="color: #800000">"</span><span style="color: #800000">translatedText</span><span style="color: #800000">"</span><span style="color: #000000">: </span><span style="color: #800000">"</span><span style="color: #800000">Hallo Welt</span><span style="color: #800000">"</span><span style="color: #000000">,<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> </span><span style="color: #800000">"</span><span style="color: #800000">detectedSourceLanguage</span><span style="color: #800000">"</span><span style="color: #000000">: </span><span style="color: #800000">"</span><span style="color: #800000">en</span><span style="color: #800000">"</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> }<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> ]<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> }<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />}</span></div>3.python实现的谷歌翻译源码：<br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #008000">#</span><span style="color: #008000">coding:utf8</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">import</span><span style="color: #000000"> urllib2<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">import</span><span style="color: #000000"> json<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">import</span><span style="color: #000000"> os,sys<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />reload(sys)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />sys.setdefaultencoding(</span><span style="color: #800000">"</span><span style="color: #800000">utf-8</span><span style="color: #800000">"</span><span style="color: #000000">)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #800080">__author__</span><span style="color: #000000"> </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800000">'</span><span style="color: #800000">chenyu</span><span style="color: #800000">'</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">class</span><span style="color: #000000"> GoogleTranslate:<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> </span><span style="color: #800000">"""</span><span style="color: #800000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> 谷歌翻译类<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> </span><span style="color: #800000">"""</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> </span><span style="color: #0000ff">def</span><span style="color: #000000"> google_translate(slef,text,targetlanguage):<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> text </span><span style="color: #000000">=</span><span style="color: #000000"> urllib2.quote(text)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> url </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000">https://www.googleapis.com/language/translate/v2/?key=YOUR_KEY&amp;target=</span><span style="color: #800000">"</span><span style="color: #000000">+</span><span style="color: #000000">targetlanguage</span><span style="color: #000000">+</span><span style="color: #800000">"</span><span style="color: #800000">&amp;q=</span><span style="color: #800000">"</span><span style="color: #000000">+</span><span style="color: #000000">text<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> res </span><span style="color: #000000">=</span><span style="color: #000000"> urllib2.urlopen(urllib2.Request(url))<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> dirt </span><span style="color: #000000">=</span><span style="color: #000000"> json.JSONDecoder().decode(res.read())<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> </span><span style="color: #0000ff">return</span><span style="color: #000000"> dirt[</span><span style="color: #800000">"</span><span style="color: #800000">data</span><span style="color: #800000">"</span><span style="color: #000000">][</span><span style="color: #800000">"</span><span style="color: #800000">translations</span><span style="color: #800000">"</span><span style="color: #000000">][0][</span><span style="color: #800000">"</span><span style="color: #800000">translatedText</span><span style="color: #800000">"</span><span style="color: #000000">]</span></div><br />参见网站：<a href="https://developers.google.com/translate/v2/using_rest?hl=zh-TW#target">https://developers.google.com/translate/v2/using_rest?hl=zh-TW#target</a><br />JSON相关资料可参见：<a href="http://www.cppblog.com/sunrise/archive/2013/01/17/197347.html">http://www.cppblog.com/sunrise/archive/2013/01/17/197347.html</a><br /><br /><br /></span></div><img src ="http://www.cppblog.com/sunrise/aggbug/197348.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunrise/" target="_blank">SunRise_at</a> 2013-01-17 16:59 <a href="http://www.cppblog.com/sunrise/archive/2013/01/17/197348.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>json的编码和解析</title><link>http://www.cppblog.com/sunrise/archive/2013/01/17/197347.html</link><dc:creator>SunRise_at</dc:creator><author>SunRise_at</author><pubDate>Thu, 17 Jan 2013 08:28:00 GMT</pubDate><guid>http://www.cppblog.com/sunrise/archive/2013/01/17/197347.html</guid><wfw:comment>http://www.cppblog.com/sunrise/comments/197347.html</wfw:comment><comments>http://www.cppblog.com/sunrise/archive/2013/01/17/197347.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunrise/comments/commentRss/197347.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunrise/services/trackbacks/197347.html</trackback:ping><description><![CDATA[<div>
<p><span class="search_hit"><br />&nbsp; JSON</span>是<a class="wikilink2" title="modules:javascript" href="http://www.pythonclub.org/modules/javascript" rel="nofollow">JavaScript</a> Object Notation的缩写，SJON是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。 </p>
<p>它基于<a class="wikilink2" title="modules:javascript" href="http://www.pythonclub.org/modules/javascript" rel="nofollow">JavaScript</a> Programming Language, Standard ECMA-262 3rd Edition - December 1999的一个子集。</p>
<p>&nbsp;&nbsp; JSON建构于两种结构：</p>
<p>&#8220;名称/值&#8221;对的集合（A collection of name/value pairs）。不同的语言中，它被理解为对象（object），纪录（record），结构（struct），字典（dictionary），哈希表（hash table），有键列表（keyed list），或者关联数组 （associative array）。 <br />值的有序列表（An ordered list of values）。在大部分语言中，它被理解为数组（array）。 <br />这些都是常见的数据结构。事实上大部分现代计算机语言都以某种形式支持它们。这使得一种数据格式在同样基于这些结构的编程语言之间交换成为可能。</p><br />1.对简单数据类型的encoding和decoding:<br /><font face="Courier New">json.<tt style="padding-bottom: 0px; line-height: normal; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; font-style: normal; text-indent: 0px; padding-left: 1px; letter-spacing: normal; padding-right: 1px; white-space: normal; orphans: 2; color: rgb(0,0,0); font-size: 16px; font-weight: bold; word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="descname">dumps</tt></font><big style="line-height: normal; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; font-style: normal; text-indent: 0px; letter-spacing: normal; font-family: sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); font-weight: normal; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">(</big><em style="line-height: normal; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; text-indent: 0px; letter-spacing: normal; font-family: sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); font-size: 16px; font-weight: normal; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">obj</em><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; display: inline !important; font: 16px sans-serif; white-space: normal; orphans: 2; float: none; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">,<span class="Apple-converted-space">&nbsp;</span></span><em style="line-height: normal; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; text-indent: 0px; letter-spacing: normal; font-family: sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); font-size: 16px; font-weight: normal; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">skipkeys=False</em><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; display: inline !important; font: 16px sans-serif; white-space: normal; orphans: 2; float: none; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">,<span class="Apple-converted-space">&nbsp;</span></span><em style="line-height: normal; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; text-indent: 0px; letter-spacing: normal; font-family: sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); font-size: 16px; font-weight: normal; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">ensure_ascii=True</em><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; display: inline !important; font: 16px sans-serif; white-space: normal; orphans: 2; float: none; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">,<span class="Apple-converted-space">&nbsp;</span></span><em style="line-height: normal; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; text-indent: 0px; letter-spacing: normal; font-family: sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); font-size: 16px; font-weight: normal; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">check_circular=True</em><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; display: inline !important; font: 16px sans-serif; white-space: normal; orphans: 2; float: none; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">,<span class="Apple-converted-space">&nbsp;</span></span><em style="line-height: normal; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; text-indent: 0px; letter-spacing: normal; font-family: sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); font-size: 16px; font-weight: normal; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">allow_nan=True</em><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; display: inline !important; font: 16px sans-serif; white-space: normal; orphans: 2; float: none; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">,<span class="Apple-converted-space">&nbsp;</span></span><em style="line-height: normal; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; text-indent: 0px; letter-spacing: normal; font-family: sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); font-size: 16px; font-weight: normal; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">cls=None</em><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; display: inline !important; font: 16px sans-serif; white-space: normal; orphans: 2; float: none; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">,<span class="Apple-converted-space">&nbsp;</span></span><em style="line-height: normal; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; text-indent: 0px; letter-spacing: normal; font-family: sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); font-size: 16px; font-weight: normal; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">indent=None</em><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; display: inline !important; font: 16px sans-serif; white-space: normal; orphans: 2; float: none; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">,<span class="Apple-converted-space">&nbsp;</span></span><em style="line-height: normal; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; text-indent: 0px; letter-spacing: normal; font-family: sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); font-size: 16px; font-weight: normal; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">separators=None</em><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; display: inline !important; font: 16px sans-serif; white-space: normal; orphans: 2; float: none; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">,<span class="Apple-converted-space">&nbsp;</span></span><em style="line-height: normal; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; text-indent: 0px; letter-spacing: normal; font-family: sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); font-size: 16px; font-weight: normal; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">encoding="utf-8"</em><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; display: inline !important; font: 16px sans-serif; white-space: normal; orphans: 2; float: none; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">,<span class="Apple-converted-space">&nbsp;</span></span><em style="line-height: normal; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; text-indent: 0px; letter-spacing: normal; font-family: sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); font-size: 16px; font-weight: normal; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">default=None</em><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; display: inline !important; font: 16px sans-serif; white-space: normal; orphans: 2; float: none; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">,<span class="Apple-converted-space">&nbsp;</span></span><em style="line-height: normal; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; text-indent: 0px; letter-spacing: normal; font-family: sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); font-size: 16px; font-weight: normal; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">sort_keys=False</em><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; display: inline !important; font: 16px sans-serif; white-space: normal; orphans: 2; float: none; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">,<span class="Apple-converted-space">&nbsp;</span></span><em style="line-height: normal; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; text-indent: 0px; letter-spacing: normal; font-family: sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); font-size: 16px; font-weight: normal; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">**kw</em><big style="line-height: normal; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; font-style: normal; text-indent: 0px; letter-spacing: normal; font-family: sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); font-weight: normal; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">)</big><br />&nbsp; 
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">print</span><span style="color: #000000">&nbsp;json.dumps([</span><span style="color: #800000">'</span><span style="color: #800000">foo</span><span style="color: #800000">'</span><span style="color: #000000">,{</span><span style="color: #800000">'</span><span style="color: #800000">bar</span><span style="color: #800000">'</span><span style="color: #000000">:(</span><span style="color: #800000">'</span><span style="color: #800000">baz</span><span style="color: #800000">'</span><span style="color: #000000">,None,</span><span style="color: #000000">1.0</span><span style="color: #000000">,</span><span style="color: #000000">2</span><span style="color: #000000">)}])<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">print</span><span style="color: #000000">&nbsp;json.dumps(</span><span style="color: #800000">"</span><span style="color: #800000">\</span><span style="color: #800000">"</span><span style="color: #000000">foo\bar</span><span style="color: #800000">"</span><span style="color: #800000">)</span><span style="color: #800000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">print</span><span style="color: #000000">&nbsp;json.dumps(u</span><span style="color: #800000">'</span><span style="color: #800000">\u1234</span><span style="color: #800000">'</span><span style="color: #000000">)<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">print</span><span style="color: #000000">&nbsp;json.dumps(</span><span style="color: #800000">'</span><span style="color: #800000">\\</span><span style="color: #800000">'</span><span style="color: #000000">)<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">print</span><span style="color: #000000">&nbsp;json.dumps({</span><span style="color: #800000">"</span><span style="color: #800000">c</span><span style="color: #800000">"</span><span style="color: #000000">:0,</span><span style="color: #800000">"</span><span style="color: #800000">b</span><span style="color: #800000">"</span><span style="color: #000000">:0,</span><span style="color: #800000">"</span><span style="color: #800000">a</span><span style="color: #800000">"</span><span style="color: #000000">:0},sort_keys</span><span style="color: #000000">=</span><span style="color: #000000">True)</span></div><br />输出：<br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><span style="color: #000000">[</span><span style="color: #800000">"</span><span style="color: #800000">foo</span><span style="color: #800000">"</span><span style="color: #000000">, {</span><span style="color: #800000">"</span><span style="color: #800000">bar</span><span style="color: #800000">"</span><span style="color: #000000">: [</span><span style="color: #800000">"</span><span style="color: #800000">baz</span><span style="color: #800000">"</span><span style="color: #000000">, null, </span><span style="color: #000000">1.0</span><span style="color: #000000">, </span><span style="color: #000000">2</span><span style="color: #000000">]}]<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #800000">"</span><span style="color: #800000">\</span><span style="color: #800000">"</span><span style="color: #000000">foo\bar</span><span style="color: #800000">"<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />"</span><span style="color: #800000">\u1234</span><span style="color: #800000">"</span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #800000">"</span><span style="color: #800000">\\</span><span style="color: #800000">"</span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />{</span><span style="color: #800000">"</span><span style="color: #800000">a</span><span style="color: #800000">"</span><span style="color: #000000">: 0, </span><span style="color: #800000">"</span><span style="color: #800000">b</span><span style="color: #800000">"</span><span style="color: #000000">: 0, </span><span style="color: #800000">"</span><span style="color: #800000">c</span><span style="color: #800000">"</span><span style="color: #000000">: 0}</span></div></div>&nbsp;&nbsp;&nbsp;compact encoding: 
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><span style="color: #000000"> </span><span style="color: #008000">#</span><span style="color: #008000">json主要是作为一种数据通信的格式存在的，而网络通信是很在乎数据的大小的，无用的空格会占据很多通信带宽，所以适当时候也要对数据进行压缩。separator参数可以起到这样的作用，该参数传递是一个元组，包含分割对象的字符串。</span><span style="color: #008000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #000000"> </span><span style="color: #0000ff">print</span><span style="color: #000000"> json.dumps([</span><span style="color: #000000">1</span><span style="color: #000000">,</span><span style="color: #000000">2</span><span style="color: #000000">,</span><span style="color: #000000">3</span><span style="color: #000000">,{</span><span style="color: #800000">'</span><span style="color: #800000">4</span><span style="color: #800000">'</span><span style="color: #000000">:</span><span style="color: #000000">5</span><span style="color: #000000">,</span><span style="color: #800000">'</span><span style="color: #800000">6</span><span style="color: #800000">'</span><span style="color: #000000">:</span><span style="color: #000000">7</span><span style="color: #000000">}],separators</span><span style="color: #000000">=</span><span style="color: #000000">(</span><span style="color: #800000">'</span><span style="color: #800000">,</span><span style="color: #800000">'</span><span style="color: #000000">,</span><span style="color: #800000">'</span><span style="color: #800000">:</span><span style="color: #800000">'</span><span style="color: #000000">))<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /> </span><span style="color: #008000">#</span><span style="color: #008000">indent参数是缩进的意思，它可以使得数据存储的格式变得更加优雅。</span><span style="color: #008000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #000000"> </span><span style="color: #0000ff">print</span><span style="color: #000000"> json.dumps([</span><span style="color: #000000">1</span><span style="color: #000000">,</span><span style="color: #000000">2</span><span style="color: #000000">,</span><span style="color: #000000">3</span><span style="color: #000000">,{</span><span style="color: #800000">'</span><span style="color: #800000">4</span><span style="color: #800000">'</span><span style="color: #000000">:</span><span style="color: #000000">5</span><span style="color: #000000">,</span><span style="color: #800000">'</span><span style="color: #800000">6</span><span style="color: #800000">'</span><span style="color: #000000">:</span><span style="color: #000000">7</span><span style="color: #000000">}],separators</span><span style="color: #000000">=</span><span style="color: #000000">(</span><span style="color: #800000">'</span><span style="color: #800000">,</span><span style="color: #800000">'</span><span style="color: #000000">,</span><span style="color: #800000">'</span><span style="color: #800000">:</span><span style="color: #800000">'</span><span style="color: #000000">),indent </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">4</span><span style="color: #000000">)</span></div>2.decoding JSON<br /><font face="Courier New">json.<tt style="padding-bottom: 0px; line-height: normal; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; font-style: normal; text-indent: 0px; padding-left: 1px; letter-spacing: normal; padding-right: 1px; white-space: normal; orphans: 2; color: rgb(0,0,0); font-size: 16px; font-weight: bold; word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="descname">load</tt></font><big style="line-height: normal; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; font-style: normal; text-indent: 0px; letter-spacing: normal; font-family: sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); font-weight: normal; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">(</big><em style="line-height: normal; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; text-indent: 0px; letter-spacing: normal; font-family: sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); font-size: 16px; font-weight: normal; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">fp</em><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 1.3em sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="optional">[</span><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; display: inline !important; font: 16px sans-serif; white-space: normal; orphans: 2; float: none; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">,<span class="Apple-converted-space">&nbsp;</span></span><em style="line-height: normal; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; text-indent: 0px; letter-spacing: normal; font-family: sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); font-size: 16px; font-weight: normal; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">encoding</em><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 1.3em sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="optional">[</span><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; display: inline !important; font: 16px sans-serif; white-space: normal; orphans: 2; float: none; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">,<span class="Apple-converted-space">&nbsp;</span></span><em style="line-height: normal; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; text-indent: 0px; letter-spacing: normal; font-family: sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); font-size: 16px; font-weight: normal; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">cls</em><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 1.3em sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="optional">[</span><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; display: inline !important; font: 16px sans-serif; white-space: normal; orphans: 2; float: none; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">,<span class="Apple-converted-space">&nbsp;</span></span><em style="line-height: normal; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; text-indent: 0px; letter-spacing: normal; font-family: sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); font-size: 16px; font-weight: normal; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">object_hook</em><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 1.3em sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="optional">[</span><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; display: inline !important; font: 16px sans-serif; white-space: normal; orphans: 2; float: none; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">,<span class="Apple-converted-space">&nbsp;</span></span><em style="line-height: normal; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; text-indent: 0px; letter-spacing: normal; font-family: sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); font-size: 16px; font-weight: normal; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">parse_float</em><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 1.3em sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="optional">[</span><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; display: inline !important; font: 16px sans-serif; white-space: normal; orphans: 2; float: none; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">,<span class="Apple-converted-space">&nbsp;</span></span><em style="line-height: normal; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; text-indent: 0px; letter-spacing: normal; font-family: sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); font-size: 16px; font-weight: normal; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">parse_int</em><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 1.3em sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="optional">[</span><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; display: inline !important; font: 16px sans-serif; white-space: normal; orphans: 2; float: none; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">,<span class="Apple-converted-space">&nbsp;</span></span><em style="line-height: normal; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; text-indent: 0px; letter-spacing: normal; font-family: sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); font-size: 16px; font-weight: normal; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">parse_constant</em><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 1.3em sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="optional">[</span><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; display: inline !important; font: 16px sans-serif; white-space: normal; orphans: 2; float: none; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">,<span class="Apple-converted-space">&nbsp;</span></span><em style="line-height: normal; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; text-indent: 0px; letter-spacing: normal; font-family: sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); font-size: 16px; font-weight: normal; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">object_pairs_hook</em><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 1.3em sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="optional">[</span><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; display: inline !important; font: 16px sans-serif; white-space: normal; orphans: 2; float: none; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">,<span class="Apple-converted-space">&nbsp;</span></span><em style="line-height: normal; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; text-indent: 0px; letter-spacing: normal; font-family: sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); font-size: 16px; font-weight: normal; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">**kw</em><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 1.3em sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="optional">]</span><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 1.3em sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="optional">]</span><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 1.3em sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="optional">]</span><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 1.3em sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="optional">]</span><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 1.3em sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="optional">]</span><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 1.3em sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="optional">]</span><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 1.3em sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="optional">]</span><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 1.3em sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="optional">]</span><big style="line-height: normal; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; font-style: normal; text-indent: 0px; letter-spacing: normal; font-family: sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); font-weight: normal; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">)</big><br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><span style="color: #000000"> </span><span style="color: #0000ff">print</span><span style="color: #000000"> json.loads(</span><span style="color: #800000">'</span><span style="color: #800000">["foo",{"bar":["baz",null,1.0,2]}]</span><span style="color: #800000">'</span><span style="color: #000000">)<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /> </span><span style="color: #0000ff">print</span><span style="color: #000000"> json.loads(</span><span style="color: #800000">'</span><span style="color: #800000">"\\"foo\\bar"</span><span style="color: #800000">'</span><span style="color: #000000">)</span></div>输出：<br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><span style="color: #000000">[u</span><span style="color: #800000">'</span><span style="color: #800000">foo</span><span style="color: #800000">'</span><span style="color: #000000">, {u</span><span style="color: #800000">'</span><span style="color: #800000">bar</span><span style="color: #800000">'</span><span style="color: #000000">: [u</span><span style="color: #800000">'</span><span style="color: #800000">baz</span><span style="color: #800000">'</span><span style="color: #000000">, None, </span><span style="color: #000000">1.0</span><span style="color: #000000">, </span><span style="color: #000000">2</span><span style="color: #000000">]}]<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #800000">"</span><span style="color: #800000">foar</span></div>3.特殊JSON对象的解析 
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><span style="color: #0000ff">def</span><span style="color: #000000"> as_complex(dct):<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /> </span><span style="color: #0000ff">if</span><span style="color: #000000"> </span><span style="color: #800000">'</span><span style="color: #800000">__complex__</span><span style="color: #800000">'</span><span style="color: #000000"> </span><span style="color: #0000ff">in</span><span style="color: #000000"> dct:<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /> </span><span style="color: #0000ff">return</span><span style="color: #000000"> complex(dct[</span><span style="color: #800000">'</span><span style="color: #800000">real</span><span style="color: #800000">'</span><span style="color: #000000">],dct[</span><span style="color: #800000">'</span><span style="color: #800000">imag</span><span style="color: #800000">'</span><span style="color: #000000">])<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /> </span><span style="color: #0000ff">return</span><span style="color: #000000"> dct</span></div><br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><span style="color: #0000ff">print</span><span style="color: #000000"> json.loads(</span><span style="color: #800000">'</span><span style="color: #800000">{"__complex__":true,"real":1,"imag":2}</span><span style="color: #800000">'</span><span style="color: #000000">,object_hook</span><span style="color: #000000">=</span><span style="color: #000000">as_complex)</span></div>输出：<br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><span style="color: #000000">(</span><span style="color: #000000">1</span><span style="color: #000000">+</span><span style="color: #000000">2j</span><span style="color: #000000">)</span></div><span class="search_hit">&nbsp;<br /><br />参见：<a href="http://docs.python.org/2/library/json.html">http://docs.python.org/2/library/json.html</a> </span><img src ="http://www.cppblog.com/sunrise/aggbug/197347.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunrise/" target="_blank">SunRise_at</a> 2013-01-17 16:28 <a href="http://www.cppblog.com/sunrise/archive/2013/01/17/197347.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>python多线程</title><link>http://www.cppblog.com/sunrise/archive/2013/01/04/196939.html</link><dc:creator>SunRise_at</dc:creator><author>SunRise_at</author><pubDate>Fri, 04 Jan 2013 06:25:00 GMT</pubDate><guid>http://www.cppblog.com/sunrise/archive/2013/01/04/196939.html</guid><wfw:comment>http://www.cppblog.com/sunrise/comments/196939.html</wfw:comment><comments>http://www.cppblog.com/sunrise/archive/2013/01/04/196939.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunrise/comments/commentRss/196939.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunrise/services/trackbacks/196939.html</trackback:ping><description><![CDATA[1.<span style="margin: 0px; padding: 0px; color: #111111; font-size: 13px; font-family: Verdana, Arial, Helvetica, sans-serif; background-color: #ffffff;">创建</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: 13px; font-family: Verdana, Arial, Helvetica, sans-serif; background-color: #ffffff;">threading.Thread</span><span style="margin: 0px; padding: 0px; background-color: #ffffff;"><font color="#111111" face="Verdana, Arial, Helvetica, sans-serif" size="2">的子类来包装一个线程对象</font>
<div style="background-color: #eeeeee; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all;">
<div style="font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008000; ">#</span><span style="color: #008000; ">encoding:utf8</span><span style="color: #008000; "><br />
</span><br />
<span style="color: #0000FF; ">import</span>&nbsp;threading<br />
<span style="color: #0000FF; ">import</span>&nbsp;time<br />
<br />
<span style="color: #0000FF; ">class</span>&nbsp;timer(threading.Thread):<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">def</span>&nbsp;<span style="color: #800080; ">__init__</span>(self,num,interval):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;threading.Thread.<span style="color: #800080; ">__init__</span>(self)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">设置产生线程的个数</span><span style="color: #008000; "><br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.thread_num&nbsp;=&nbsp;num<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">产生线程的相隔时间</span><span style="color: #008000; "><br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.interval&nbsp;=&nbsp;interval<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.thread_stop&nbsp;=&nbsp;False<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">def</span>&nbsp;run(self):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>&nbsp;<span style="color: #0000FF; ">not</span>&nbsp;self.thread_stop:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">print</span>&nbsp;<span style="color: #800000; ">'</span><span style="color: #800000; ">Thread&nbsp;Object(%d),Time:%s\n</span><span style="color: #800000; ">'</span>%(self.thread_num,time.ctime())<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time.sleep(self.interval)<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">def</span>&nbsp;stop(self):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.thread_stop&nbsp;=&nbsp;True<br />
<br />
<br />
<span style="color: #0000FF; ">def</span>&nbsp;test():<br />
&nbsp;&nbsp;&nbsp;&nbsp;thread1&nbsp;=&nbsp;timer(1,1)<br />
&nbsp;&nbsp;&nbsp;&nbsp;thread2&nbsp;=&nbsp;timer(2,2)<br />
&nbsp;&nbsp;&nbsp;&nbsp;thread1.start()<br />
&nbsp;&nbsp;&nbsp;&nbsp;thread2.start()<br />
&nbsp;&nbsp;&nbsp;&nbsp;time.sleep(10)<br />
&nbsp;&nbsp;&nbsp;&nbsp;thread1.stop()<br />
&nbsp;&nbsp;&nbsp;&nbsp;thread2.stop()<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span><br />
<br />
<span style="color: #0000FF; ">if</span>&nbsp;<span style="color: #800080; ">__name__</span>&nbsp;==&nbsp;<span style="color: #800000; ">'</span><span style="color: #800000; ">__main__</span><span style="color: #800000; ">'</span>:<br />
&nbsp;&nbsp;&nbsp;&nbsp;test()<br />
&nbsp; &nbsp;&nbsp;</div>
<span style="color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px;"></span></div>
<p style="color: #111111; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; margin: 1em 0px 0.5em; padding: 0px; line-height: 19px;"><span style="margin: 0px; padding: 0px; line-height: 21px;">threading.Thread</span><span style="margin: 0px; padding: 0px; line-height: 21px;">类的使用：</span></p>
<p style="color: #111111; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; margin: 1em 0px 0.5em; padding: 0px; line-height: 19px;"><span style="margin: 0px; padding: 0px; line-height: 21px;">1).</span><span style="margin: 0px; padding: 0px; line-height: 21px;">在自己的线程类的</span><span style="margin: 0px; padding: 0px; line-height: 21px;">__init__</span><span style="margin: 0px; padding: 0px; line-height: 21px;">里调用</span><span style="margin: 0px; padding: 0px; line-height: 21px;">threading.Thread.__init__(self, name = threadname)</span></p>
<p style="color: #111111; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; margin: 1em 0px 0.5em; padding: 0px; line-height: 19px;"><span style="margin: 0px; padding: 0px; line-height: 21px;">Threadname</span><span style="margin: 0px; padding: 0px; line-height: 21px;">为线程的名字</span></p>
<p style="color: #111111; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; margin: 1em 0px 0.5em; padding: 0px; line-height: 19px;"><span style="margin: 0px; padding: 0px; line-height: 21px;">2).</span>&nbsp;<span style="margin: 0px; padding: 0px; line-height: 21px;">run()</span><span style="margin: 0px; padding: 0px; line-height: 21px;">，通常需要重写，编写代码实现做需要的功能。</span></p>
<p style="color: #111111; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; margin: 1em 0px 0.5em; padding: 0px; line-height: 19px;"><span style="margin: 0px; padding: 0px; line-height: 21px;">3).</span><span style="margin: 0px; padding: 0px; line-height: 21px;">getName()</span><span style="margin: 0px; padding: 0px; line-height: 21px;">，获得线程对象名称</span></p>
<p style="color: #111111; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; margin: 1em 0px 0.5em; padding: 0px; line-height: 19px;"><span style="margin: 0px; padding: 0px; line-height: 21px;">4).</span><span style="margin: 0px; padding: 0px; line-height: 21px;">setName()</span><span style="margin: 0px; padding: 0px; line-height: 21px;">，设置线程对象名称</span></p>
<p style="color: #111111; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; margin: 1em 0px 0.5em; padding: 0px; line-height: 19px;"><span style="margin: 0px; padding: 0px; line-height: 21px;">5).</span><span style="margin: 0px; padding: 0px; line-height: 21px;">start()</span><span style="margin: 0px; padding: 0px; line-height: 21px;">，启动线程</span></p>
<p style="color: #111111; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; margin: 1em 0px 0.5em; padding: 0px; line-height: 19px;"><span style="margin: 0px; padding: 0px; line-height: 21px;">6).</span><span style="margin: 0px; padding: 0px; line-height: 21px;">jion([timeout])</span><span style="margin: 0px; padding: 0px; line-height: 21px;">，等待另一线程结束后再运行。</span></p>
<p style="color: #111111; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; margin: 1em 0px 0.5em; padding: 0px; line-height: 19px;"><span style="margin: 0px; padding: 0px; line-height: 21px;">7).</span><span style="margin: 0px; padding: 0px; line-height: 21px;">setDaemon(bool)</span><span style="margin: 0px; padding: 0px; line-height: 21px;">，设置子线程是否随主线程一起结束，必须在</span><span style="margin: 0px; padding: 0px; line-height: 21px;">start()</span><span style="margin: 0px; padding: 0px; line-height: 21px;">之前调用。默认为</span><span style="margin: 0px; padding: 0px; line-height: 21px;">False</span><span style="margin: 0px; padding: 0px; line-height: 21px;">。</span></p>
<p style="color: #111111; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; margin: 1em 0px 0.5em; padding: 0px; line-height: 19px;"><span style="margin: 0px; padding: 0px; line-height: 21px;">8).</span><span style="margin: 0px; padding: 0px; line-height: 21px;">isDaemon()</span><span style="margin: 0px; padding: 0px; line-height: 21px;">，判断线程是否随主线程一起结束。</span></p>
<p style="color: #111111; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; margin: 1em 0px 0.5em; padding: 0px; line-height: 19px;"><span style="margin: 0px; padding: 0px; line-height: 21px;">9).</span><span style="margin: 0px; padding: 0px; line-height: 21px;">isAlive()</span><span style="margin: 0px; padding: 0px; line-height: 21px;">，检查线程是否在运行中。</span></p>
<p style="color: #111111; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; margin: 1em 0px 0.5em; padding: 0px; line-height: 19px;"><span style="margin: 0px; padding: 0px; line-height: 21px;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="margin: 0px; padding: 0px; line-height: 21px;">此外</span><span style="margin: 0px; padding: 0px; line-height: 21px;">threading</span><span style="margin: 0px; padding: 0px; line-height: 21px;">模块本身也提供了很多方法和其他的类，可以帮助我们更好的使用和管理线程。可以参看</span><span style="margin: 0px; padding: 0px; line-height: 21px;"><a href="http://www.python.org/doc/2.5.2/lib/module-threading.html" style="margin: 0px; padding: 0px; text-decoration: none; color: #34579c;">http://www.python.org/doc/2.5.2/lib/module-threading.html</a></span><span style="margin: 0px; padding: 0px; line-height: 21px;">。<br />
2.简单的同步<br />
</span></p>
<div style="background-color: #eeeeee; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008000; ">#</span><span style="color: #008000; ">encoding:utf8</span><span style="color: #008000; "><br />
</span><span style="color: #000000; "><br />
</span><span style="color: #0000FF; ">import</span><span style="color: #000000; ">&nbsp;threading<br />
<br />
mylock&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;threading.RLock()<br />
num&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;0<br />
<br />
</span><span style="color: #0000FF; ">class</span><span style="color: #000000; ">&nbsp;myThread(threading.Thread):<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">def</span><span style="color: #000000; ">&nbsp;</span><span style="color: #800080; ">__init__</span><span style="color: #000000; ">(self,name):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;threading.Thread.</span><span style="color: #800080; ">__init__</span><span style="color: #000000; ">(self)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.t_name&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;name<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">def</span><span style="color: #000000; ">&nbsp;run(self):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">global</span><span style="color: #000000; ">&nbsp;num<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">while</span><span style="color: #000000; ">&nbsp;True:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mylock.acquire()<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">print</span><span style="color: #000000; ">&nbsp;</span><span style="color: #800000; ">'</span><span style="color: #800000; ">\nThread(%s)&nbsp;locked,Number:%d</span><span style="color: #800000; ">'</span><span style="color: #000000; ">%</span><span style="color: #000000; ">(self.t_name,num)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">&nbsp;num&nbsp;</span><span style="color: #000000; ">&gt;=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">4</span><span style="color: #000000; ">:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mylock.release()<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">print</span><span style="color: #000000; ">&nbsp;</span><span style="color: #800000; ">'</span><span style="color: #800000; ">\nThread(%s)&nbsp;released,Number:%d</span><span style="color: #800000; ">'</span><span style="color: #000000; ">%</span><span style="color: #000000; ">(self.t_name,num)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">break</span><span style="color: #000000; "><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num&nbsp;</span><span style="color: #000000; ">+=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">1</span><span style="color: #000000; "><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">print</span><span style="color: #000000; ">&nbsp;</span><span style="color: #800000; ">'</span><span style="color: #800000; ">\nThread(%s)&nbsp;released,Number:%d</span><span style="color: #800000; ">'</span><span style="color: #000000; ">%</span><span style="color: #000000; ">(self.t_name,num)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mylock.release()<br />
<br />
<br />
</span><span style="color: #0000FF; ">def</span><span style="color: #000000; ">&nbsp;test():<br />
&nbsp;&nbsp;&nbsp;&nbsp;thread1&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;myThread(</span><span style="color: #800000; ">'</span><span style="color: #800000; ">A</span><span style="color: #800000; ">'</span><span style="color: #000000; ">)<br />
&nbsp;&nbsp;&nbsp;&nbsp;thread2&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;myThread(</span><span style="color: #800000; ">'</span><span style="color: #800000; ">B</span><span style="color: #800000; ">'</span><span style="color: #000000; ">)<br />
&nbsp;&nbsp;&nbsp;&nbsp;thread1.start()<br />
&nbsp;&nbsp;&nbsp;&nbsp;thread2.start()<br />
<br />
</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">&nbsp;</span><span style="color: #800080; ">__name__</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">==</span><span style="color: #000000; ">&nbsp;</span><span style="color: #800000; ">'</span><span style="color: #800000; ">__main__</span><span style="color: #800000; ">'</span><span style="color: #000000;">:<br />
&nbsp;&nbsp;&nbsp;&nbsp;test()<br />
</span></div>
</span>
<p>&nbsp; &nbsp;<span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">Python</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">的</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">threading module</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">是在建立在</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">thread module</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">基础之上的一个</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">module</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">，在</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">threading module</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">中，暴露了许多</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">thread module</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">中的属性。在</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">thread module</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">中，</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">python</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">提供了用户级的线程同步工具&#8220;</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">Lock</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">&#8221;对象。而在</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">threading module</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">中，</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">python</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">又提供了</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">Lock</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">对象的变种</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">: RLock</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">对象。</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">RLock</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">对象内部维护着一个</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">Lock</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">对象，它是一种可重入的对象。对于</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">Lock</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">对象而言，如果一个线程连续两次进行</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">acquire</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">操作，那么由于第一次</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">acquire</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">之后没有</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">release</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">，第二次</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">acquire</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">将挂起线程。这会导致</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">Lock</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">对象永远不会</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">release</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">，使得线程死锁。</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">RLock</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">对象允许一个线程多次对其进行</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">acquire</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">操作，因为在其内部通过一个</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">counter</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">变量维护着线程</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">acquire</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">的次数。而且每一次的</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">acquire</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">操作必须有一个</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">release</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">操作与之对应，在所有的</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">release</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">操作完成之后，别的线程才能申请该</span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; font-family: Verdana, Arial, Helvetica, sans-serif; text-indent: 28px; background-color: #ffffff;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">RLock</span></span><span style="margin: 0px; padding: 0px; color: #111111; font-size: small; line-height: 19px; text-indent: 28px; background-color: #ffffff; font-family: 宋体;">对象。<span style="margin: 0px; padding: 0px;">修改共享数据的代码成为&#8220;临界区&#8221;。必须将所有&#8220;临界区&#8221;都封闭在同一个锁对象的</span><span style="margin: 0px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">acquire</span></span><span style="margin: 0px; padding: 0px;">和</span><span style="margin: 0px; padding: 0px; font-family: Verdana, Arial, Helvetica, sans-serif;"><span style="margin: 0px; padding: 0px; font-size: 13px; font-family: 'Times New Roman';">release</span></span><span style="margin: 0px; padding: 0px;">之间。</span><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /></span></p><img src ="http://www.cppblog.com/sunrise/aggbug/196939.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunrise/" target="_blank">SunRise_at</a> 2013-01-04 14:25 <a href="http://www.cppblog.com/sunrise/archive/2013/01/04/196939.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>python编码转换</title><link>http://www.cppblog.com/sunrise/archive/2012/12/27/196731.html</link><dc:creator>SunRise_at</dc:creator><author>SunRise_at</author><pubDate>Thu, 27 Dec 2012 08:11:00 GMT</pubDate><guid>http://www.cppblog.com/sunrise/archive/2012/12/27/196731.html</guid><wfw:comment>http://www.cppblog.com/sunrise/comments/196731.html</wfw:comment><comments>http://www.cppblog.com/sunrise/archive/2012/12/27/196731.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunrise/comments/commentRss/196731.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunrise/services/trackbacks/196731.html</trackback:ping><description><![CDATA[<p style="text-align: justify; padding-bottom: 0px; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; margin: 0px 0px 1em; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 13px/19px Verdana, 'Lucida Grande', Lucida, Helvetica, Arial, sans-serif; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">参见：<a href="http://www.pythonclub.org/python-basic/codec">http://www.pythonclub.org/python-basic/codec</a><br /><br />主要介绍了python的编码机制，unicode, utf-8, utf-16, GBK, GB2312,<acronym style="border-bottom: rgb(51,51,51) 1px dotted; padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; cursor: help; padding-top: 0px" title="International Organization for Standardization">ISO</acronym>-8859-1 等编码之间的转换。</p>
<p style="text-align: justify; padding-bottom: 0px; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; margin: 0px 0px 1em; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 13px/19px Verdana, 'Lucida Grande', Lucida, Helvetica, Arial, sans-serif; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><strong style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px">常见的编码转换分为以下几种情况：<br /></strong>1.自动识别字符串编码：</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #008000">#</span><span style="color: #008000">coding:utf8</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#</span><span style="color: #008000">chartdet官方下载网站http://pypi.python.org/pypi/chardet</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">import</span><span style="color: #000000">&nbsp;urllib<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">import</span><span style="color: #000000">&nbsp;chardet<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />rawdata&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;urllib.urlopen(</span><span style="color: #800000">'</span><span style="color: #800000">http://www.google.cn/</span><span style="color: #800000">'</span><span style="color: #000000">).read()<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">print</span><span style="color: #000000">&nbsp;chardet.detect(rawdata)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div>
<p style="text-align: justify; padding-bottom: 0px; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; margin: 0px 0px 1em; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 13px/19px Verdana, 'Lucida Grande', Lucida, Helvetica, Arial, sans-serif; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><br />输出：</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #008000">#</span><span style="color: #008000">confidence是可信度，encoding是编码</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000">{</span><span style="color: #800000">'</span><span style="color: #800000">confidence</span><span style="color: #800000">'</span><span style="color: #000000">:&nbsp;</span><span style="color: #000000">0.99</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">encoding</span><span style="color: #800000">'</span><span style="color: #000000">:&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">utf-8</span><span style="color: #800000">'</span><span style="color: #000000">}</span></div>
<p style="text-align: justify; padding-bottom: 0px; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; margin: 0px 0px 1em; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 13px/19px Verdana, 'Lucida Grande', Lucida, Helvetica, Arial, sans-serif; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><br />2.unicode转换为其他编码</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #008000">#</span><span style="color: #008000">coding:utf8</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />a&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;u</span><span style="color: #800000">'</span><span style="color: #800000">中文</span><span style="color: #800000">'</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />a_gb2312&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;a.encode(</span><span style="color: #800000">'</span><span style="color: #800000">gb2312</span><span style="color: #800000">'</span><span style="color: #000000">)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">print</span><span style="color: #000000">&nbsp;a_gb2312<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div>
<p style="text-align: justify; padding-bottom: 0px; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; margin: 0px 0px 1em; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 13px/19px Verdana, 'Lucida Grande', Lucida, Helvetica, Arial, sans-serif; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">输出：</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">中文</span></div>
<p style="text-align: justify; padding-bottom: 0px; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; margin: 0px 0px 1em; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 13px/19px Verdana, 'Lucida Grande', Lucida, Helvetica, Arial, sans-serif; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><br />3.其他编码转换为unicode</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #008000">#</span><span style="color: #008000">coding:utf8</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />a&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;u</span><span style="color: #800000">'</span><span style="color: #800000">中文</span><span style="color: #800000">'</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />a_gb2312&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;a.encode(</span><span style="color: #800000">'</span><span style="color: #800000">gb2312</span><span style="color: #800000">'</span><span style="color: #000000">)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">print</span><span style="color: #000000">&nbsp;a_gb2312<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #008000">#</span><span style="color: #008000">a为gb2312编码，要转为unicode.&nbsp;unicode(a,&nbsp;'gb2312')或a.decode('gb2312')</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">print</span><span style="color: #000000">&nbsp;[unicode(a_gb2312,</span><span style="color: #800000">'</span><span style="color: #800000">gb2312</span><span style="color: #800000">'</span><span style="color: #000000">)]<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">print</span><span style="color: #000000">&nbsp;[a_gb2312.decode(</span><span style="color: #800000">'</span><span style="color: #800000">gb2312</span><span style="color: #800000">'</span><span style="color: #000000">)]</span></div>
<p style="text-align: justify; padding-bottom: 0px; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; margin: 0px 0px 1em; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 13px/19px Verdana, 'Lucida Grande', Lucida, Helvetica, Arial, sans-serif; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">输出：</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">中文<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />[u</span><span style="color: #800000">'</span><span style="color: #800000">\u4e2d\u6587</span><span style="color: #800000">'</span><span style="color: #000000">]<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />[u</span><span style="color: #800000">'</span><span style="color: #800000">\u4e2d\u6587</span><span style="color: #800000">'</span><span style="color: #000000">]</span></div>
<p style="text-align: justify; padding-bottom: 0px; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; margin: 0px 0px 1em; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 13px/19px Verdana, 'Lucida Grande', Lucida, Helvetica, Arial, sans-serif; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><br />4.非unicode编码之间的相互转化</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #008000">#</span><span style="color: #008000">coding:utf8</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />a&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;u</span><span style="color: #800000">'</span><span style="color: #800000">中文</span><span style="color: #800000">'</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />a_gb2312&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;a.encode(</span><span style="color: #800000">'</span><span style="color: #800000">gb2312</span><span style="color: #800000">'</span><span style="color: #000000">)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">print</span><span style="color: #000000">&nbsp;a_gb2312<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #008000">#</span><span style="color: #008000">编码1转换为编码2可以先转为unicode再转为编码2</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000">a_unicode&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;a_gb2312.decode(</span><span style="color: #800000">'</span><span style="color: #800000">gb2312</span><span style="color: #800000">'</span><span style="color: #000000">)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">print</span><span style="color: #000000">&nbsp;[a_unicode]<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />a_utf8&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;a_unicode.encode(</span><span style="color: #800000">'</span><span style="color: #800000">utf8</span><span style="color: #800000">'</span><span style="color: #000000">)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #008000">#</span><span style="color: #008000">dos不识别utf8编码，直接输出会是乱码</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">print</span><span style="color: #000000">&nbsp;[a_utf8]</span></div>
<p style="text-align: justify; padding-bottom: 0px; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; margin: 0px 0px 1em; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 13px/19px Verdana, 'Lucida Grande', Lucida, Helvetica, Arial, sans-serif; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><br />输出：</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">中文<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />[u</span><span style="color: #800000">'</span><span style="color: #800000">\u4e2d\u6587</span><span style="color: #800000">'</span><span style="color: #000000">]<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />[</span><span style="color: #800000">'</span><span style="color: #800000">\xe4\xb8\xad\xe6\x96\x87</span><span style="color: #800000">'</span><span style="color: #000000">]</span></div>
<p style="text-align: justify; padding-bottom: 0px; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; margin: 0px 0px 1em; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 13px/19px Verdana, 'Lucida Grande', Lucida, Helvetica, Arial, sans-serif; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><br />5.判断字符串编码</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #008000">#</span><span style="color: #008000">coding:utf8</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #008000">#</span><span style="color: #008000">isinstance(s,&nbsp;str)&nbsp;用来判断是否为一般字符串&nbsp;</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#</span><span style="color: #008000">isinstance(s,&nbsp;unicode)&nbsp;用来判断是否为unicode&nbsp;3</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#</span><span style="color: #008000">如果一个字符串已经是unicode了，再执行unicode转换有时会出错(并不都出错)&nbsp;</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">def</span><span style="color: #000000">&nbsp;u(s,encoding):<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;isinstance(s,unicode):<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;s<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">:<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;unicode(s,encoding)</span></div>
<p style="text-align: justify; padding-bottom: 0px; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; margin: 0px 0px 1em; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 13px/19px Verdana, 'Lucida Grande', Lucida, Helvetica, Arial, sans-serif; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><br />6.汉字转化为unicode编码</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #008000">#</span><span style="color: #008000">coding:utf8</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #008000">#</span><span style="color: #008000">该方法没看懂，先留下了</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000">name&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">中国</span><span style="color: #800000">'</span><span style="color: #000000">&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />name&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;name.decode(</span><span style="color: #800000">'</span><span style="color: #800000">utf8</span><span style="color: #800000">'</span><span style="color: #000000">)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">print</span><span style="color: #000000">&nbsp;name<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />tmpname&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #800000">""</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;c&nbsp;</span><span style="color: #0000ff">in</span><span style="color: #000000">&nbsp;name:<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;c&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #800000">"</span><span style="color: #800000">%%u%04X</span><span style="color: #800000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">%</span><span style="color: #000000">&nbsp;ord(c)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;tmpname&nbsp;</span><span style="color: #000000">+=</span><span style="color: #000000">&nbsp;c<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">print</span><span style="color: #000000">&nbsp;tmpname<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div>
<p style="text-align: justify; padding-bottom: 0px; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; margin: 0px 0px 1em; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 13px/19px Verdana, 'Lucida Grande', Lucida, Helvetica, Arial, sans-serif; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">输出结果：</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">中国<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000">%</span><span style="color: #000000">u4E2D</span><span style="color: #000000">%</span><span style="color: #000000">u56FD</span></div>
<p style="text-align: justify; padding-bottom: 0px; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; margin: 0px 0px 1em; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 13px/19px Verdana, 'Lucida Grande', Lucida, Helvetica, Arial, sans-serif; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><br /><br /><br /><br /><br /><br /><br /><br /><br />&nbsp;</p><img src ="http://www.cppblog.com/sunrise/aggbug/196731.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunrise/" target="_blank">SunRise_at</a> 2012-12-27 16:11 <a href="http://www.cppblog.com/sunrise/archive/2012/12/27/196731.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Python yield 用法</title><link>http://www.cppblog.com/sunrise/archive/2012/12/27/196704.html</link><dc:creator>SunRise_at</dc:creator><author>SunRise_at</author><pubDate>Thu, 27 Dec 2012 04:04:00 GMT</pubDate><guid>http://www.cppblog.com/sunrise/archive/2012/12/27/196704.html</guid><wfw:comment>http://www.cppblog.com/sunrise/comments/196704.html</wfw:comment><comments>http://www.cppblog.com/sunrise/archive/2012/12/27/196704.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunrise/comments/commentRss/196704.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunrise/services/trackbacks/196704.html</trackback:ping><description><![CDATA[<a style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 13px/19px Verdana, Geneva, Arial, Helvetica, sans-serif; white-space: normal; orphans: 2; color: rgb(26,139,200); word-spacing: 0px; text-decoration: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" href="http://www.pythonclub.org/python-basic/yield"><font class="Apple-style-span" color="#000000">http://www.pythonclub.org/python-basic/yield</font></a><br />
<h2 style="border-bottom: rgb(204,204,204) 1px solid; padding-bottom: 0px; widows: 2; text-transform: none; border-top-color: rgb(204,204,204); text-indent: 0px; margin: 0px 0px 1em 20px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 22px/19px Verdana, 'Lucida Grande', Lucida, Helvetica, Arial, sans-serif; white-space: normal; orphans: 2; color: rgb(51,51,51); border-right-color: rgb(204,204,204); clear: left; border-left-color: rgb(204,204,204); word-spacing: 0px; padding-top: 0.5em; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><a style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: rgb(51,51,51) !important; text-decoration: none !important; padding-top: 0px" id="yield_用法说明" name="yield_用法说明">yield 用法说明</a></h2>
<p style="padding-bottom: 0px; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; margin: 0px 0px 1em; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 13px/19px Verdana, 'Lucida Grande', Lucida, Helvetica, Arial, sans-serif; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">yield 简单说来就是一个<strong style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px">生成器</strong>，<strong style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px">生成器</strong>是这样一个函数，它记住上一次返回时在函数体中的位置。对生成器函数的第二次（或第 n 次）调用跳转至该函数中间，而上次调用的所有局部变量都保持不变。</p>
<ul style="padding-bottom: 0px; widows: 2; text-transform: none; background-color: rgb(255,255,255); list-style-type: square; text-indent: 0px; margin: 0px 0px 1em 3.5em; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 12px/1.5em Verdana, 'Lucida Grande', Lucida, Helvetica, Arial, sans-serif; white-space: normal; orphans: 2; color: rgb(99,140,156); list-style-image: none; word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><li style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px" class="level1">
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: rgb(51,51,51); font-weight: normal; padding-top: 0px" class="li">生成器 是 一个函数</div></li></ul>
<p style="padding-bottom: 0px; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; margin: 0px 0px 1em; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 13px/19px Verdana, 'Lucida Grande', Lucida, Helvetica, Arial, sans-serif; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">函数的所有参数都会保留</p>
<ul style="padding-bottom: 0px; widows: 2; text-transform: none; background-color: rgb(255,255,255); list-style-type: square; text-indent: 0px; margin: 0px 0px 1em 3.5em; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 12px/1.5em Verdana, 'Lucida Grande', Lucida, Helvetica, Arial, sans-serif; white-space: normal; orphans: 2; color: rgb(99,140,156); list-style-image: none; word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><li style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px" class="level1">
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: rgb(51,51,51); font-weight: normal; padding-top: 0px" class="li">第二次调用 此函数 时</div></li></ul>
<p style="padding-bottom: 0px; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; margin: 0px 0px 1em; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 13px/19px Verdana, 'Lucida Grande', Lucida, Helvetica, Arial, sans-serif; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">使用的参数是前一次保留下的.</p>
<ul style="padding-bottom: 0px; widows: 2; text-transform: none; background-color: rgb(255,255,255); list-style-type: square; text-indent: 0px; margin: 0px 0px 1em 3.5em; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 12px/1.5em Verdana, 'Lucida Grande', Lucida, Helvetica, Arial, sans-serif; white-space: normal; orphans: 2; color: rgb(99,140,156); list-style-image: none; word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><li style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px" class="level1">
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: rgb(51,51,51); font-weight: normal; padding-top: 0px" class="li">生成器还&#8220;记住&#8221;了它在流控制构造</div></li></ul>
<p style="padding-bottom: 0px; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; margin: 0px 0px 1em; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 13px/19px Verdana, 'Lucida Grande', Lucida, Helvetica, Arial, sans-serif; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; padding-top: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">生成器不仅&#8220;记住&#8221;了它数据状态。 生成器还&#8220;记住&#8221;了它在流控制构造（在命令式编程中，这种构造不只是数据值）中的位置。由于连续性使您在执行框架间任意跳转，而不总是返回到直接调用者的上下文（如同生成器那样），因此它仍是比较一般的。</p><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 13px/19px Verdana, 'Lucida Grande', Lucida, Helvetica, Arial, sans-serif; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"> 
<h2 style="border-bottom: rgb(204,204,204) 1px solid; padding-bottom: 0px; border-top-color: rgb(204,204,204); margin: 0px 0px 1em 20px; padding-left: 0px; padding-right: 0px; color: rgb(51,51,51); border-right-color: rgb(204,204,204); clear: left; font-size: 22px; border-left-color: rgb(204,204,204); font-weight: normal; padding-top: 0.5em"><a style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: rgb(51,51,51) !important; text-decoration: none !important; padding-top: 0px" id="yield_生成器的运行机制" name="yield_生成器的运行机制">yield 生成器的运行机制</a></h2></span>
<p style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; margin: 10px auto; letter-spacing: normal; font: 13px/19px Verdana, Geneva, Arial, Helvetica, sans-serif; white-space: normal; orphans: 2; color: rgb(75,75,75); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><span style="line-height: 19px; font-family: Verdana, 'Lucida Grande', Lucida, Helvetica, Arial, sans-serif; color: rgb(51,51,51); font-size: 13px" class="Apple-style-span">当你问生成器要一个数时，生成器会执行，直至出现 yield 语句，生成器把 yield 的参数给你，之后生成器就不会往下继续运行。 当你问他要下一个数时，它会从上次的状态开始运行，直至出现yield语句，把参数给你，之后停下。如此反复直至退出函数。</span></p>下面借助几个例子来理解yield:<br />例1 yield的应用：
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #0000ff">def</span><span style="color: #000000">&nbsp;addlist(alist):<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #0000ff">in</span><span style="color: #000000">&nbsp;alist:<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">yield</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">+</span><span style="color: #000000">2</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />alist&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;[</span><span style="color: #000000">1</span><span style="color: #000000">,</span><span style="color: #000000">2</span><span style="color: #000000">,</span><span style="color: #000000">3</span><span style="color: #000000">,</span><span style="color: #000000">4</span><span style="color: #000000">]<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;x&nbsp;</span><span style="color: #0000ff">in</span><span style="color: #000000">&nbsp;addlist(alist):<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">#</span><span style="color: #008000">对alist的每个list数据进行i+2操作</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">print</span><span style="color: #000000">&nbsp;x</span></div>输出结果：<br /><br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">3</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000">4</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000">5</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000">6</span></div><br />例2.next的应用：<br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #0000ff">def</span><span style="color: #000000"> h():<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> </span><span style="color: #0000ff">print</span><span style="color: #000000"> </span><span style="color: #800000">'</span><span style="color: #800000">hello</span><span style="color: #800000">'</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> </span><span style="color: #0000ff">yield</span><span style="color: #000000"> </span><span style="color: #000000">5</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> </span><span style="color: #0000ff">print</span><span style="color: #000000"> </span><span style="color: #800000">'</span><span style="color: #800000">word</span><span style="color: #800000">'</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />c </span><span style="color: #000000">=</span><span style="color: #000000"> h()<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #008000">#</span><span style="color: #008000">执行到yield处</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000">c.next()<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">print</span><span style="color: #000000"> </span><span style="color: #800000">'</span><span style="color: #800000">flag</span><span style="color: #800000">'</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #008000">#</span><span style="color: #008000">因为后面没有yield了，而导致程序出错</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000">c.next()</span></div><br />输出结果：<br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">hello<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />flag<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />word<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />Traceback (most recent call last):<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> File </span><span style="color: #800000">"</span><span style="color: #800000">test.py</span><span style="color: #800000">"</span><span style="color: #000000">, line </span><span style="color: #000000">9</span><span style="color: #000000">, </span><span style="color: #0000ff">in</span><span style="color: #000000"> </span><span style="color: #000000">&lt;</span><span style="color: #000000">module</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> c.next()<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />StopIteration</span></div><br />例3.next和send的不同<br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #008000">#</span><span style="color: #008000">coding:utf8</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">def</span><span style="color: #000000"> h():<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> </span><span style="color: #0000ff">print</span><span style="color: #000000"> </span><span style="color: #800000">'</span><span style="color: #800000">hello</span><span style="color: #800000">'</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> m </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">yield</span><span style="color: #000000"> </span><span style="color: #000000">5</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> </span><span style="color: #0000ff">print</span><span style="color: #000000"> m<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> d </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">yield</span><span style="color: #000000"> </span><span style="color: #000000">12</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> </span><span style="color: #0000ff">print</span><span style="color: #000000"> </span><span style="color: #800000">'</span><span style="color: #800000">word</span><span style="color: #800000">'</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> <br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />c </span><span style="color: #000000">=</span><span style="color: #000000"> h()<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #008000">#</span><span style="color: #008000">c.next()相当于c.send(None)</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000">c.next()<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">print</span><span style="color: #000000"> </span><span style="color: #800000">'</span><span style="color: #800000">flag</span><span style="color: #800000">'</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #008000">#</span><span style="color: #008000">(yield 5)被赋值了,yield接收了send的值</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000">c.send(</span><span style="color: #800000">'</span><span style="color: #800000">!</span><span style="color: #800000">'</span><span style="color: #000000">)</span></div>例4.next和send的返回值<br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #008000">#</span><span style="color: #008000">coding:utf8</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">def</span><span style="color: #000000"> h():<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> </span><span style="color: #0000ff">print</span><span style="color: #000000"> </span><span style="color: #800000">'</span><span style="color: #800000">hello</span><span style="color: #800000">'</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> m </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">yield</span><span style="color: #000000"> </span><span style="color: #000000">5</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> </span><span style="color: #0000ff">print</span><span style="color: #000000"> m<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> d </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">yield</span><span style="color: #000000"> </span><span style="color: #000000">12</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> </span><span style="color: #0000ff">print</span><span style="color: #000000"> </span><span style="color: #800000">'</span><span style="color: #800000">word</span><span style="color: #800000">'</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /> <br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />m </span><span style="color: #000000">=</span><span style="color: #000000"> c.next()<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">print</span><span style="color: #000000"> </span><span style="color: #800000">'</span><span style="color: #800000">flag</span><span style="color: #800000">'</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />d </span><span style="color: #000000">=</span><span style="color: #000000"> c.send(</span><span style="color: #800000">'</span><span style="color: #800000">!</span><span style="color: #800000">'</span><span style="color: #000000">)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #008000">#</span><span style="color: #008000">m,d的值为5,12可见send和next的返回值就是yield的参数</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">print</span><span style="color: #000000"> m,d<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><img src ="http://www.cppblog.com/sunrise/aggbug/196704.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunrise/" target="_blank">SunRise_at</a> 2012-12-27 12:04 <a href="http://www.cppblog.com/sunrise/archive/2012/12/27/196704.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>python enumerate用法</title><link>http://www.cppblog.com/sunrise/archive/2012/12/27/196702.html</link><dc:creator>SunRise_at</dc:creator><author>SunRise_at</author><pubDate>Thu, 27 Dec 2012 02:48:00 GMT</pubDate><guid>http://www.cppblog.com/sunrise/archive/2012/12/27/196702.html</guid><wfw:comment>http://www.cppblog.com/sunrise/comments/196702.html</wfw:comment><comments>http://www.cppblog.com/sunrise/archive/2012/12/27/196702.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunrise/comments/commentRss/196702.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunrise/services/trackbacks/196702.html</trackback:ping><description><![CDATA[<p style="padding-bottom: 2px; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; margin: 4px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 12px Arial, Helvetica, sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); word-spacing: 0px; padding-top: 2px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><span style="font-size: 12pt">在同时需要用到index和value值的时候可以用到enumerate，参数为可遍历的变量，如字符串，列表等，返回enumerate类。</span></p>
<p style="padding-bottom: 2px; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; margin: 4px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 12px Arial, Helvetica, sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); word-spacing: 0px; padding-top: 2px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><span style="font-size: 12pt">例：</span></p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #0000ff; font-size: 12pt">import</span><span style="color: #000000; font-size: 12pt">&nbsp;string<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000; font-size: 12pt">s&nbsp;</span><span style="color: #000000; font-size: 12pt">=</span><span style="color: #000000; font-size: 12pt">&nbsp;string.ascii_lowercase<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000; font-size: 12pt">e&nbsp;</span><span style="color: #000000; font-size: 12pt">=</span><span style="color: #000000; font-size: 12pt">&nbsp;enumerate(s)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff; font-size: 12pt">print</span><span style="color: #000000; font-size: 12pt">&nbsp;s<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff; font-size: 12pt">print</span><span style="color: #000000; font-size: 12pt">&nbsp;list(e)</span></div>
<p style="padding-bottom: 2px; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; margin: 4px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 12px Arial, Helvetica, sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); word-spacing: 0px; padding-top: 2px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><span style="font-size: 12pt">输出结果为：</span><br /><br /></p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 10pt; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">[(0,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">a</span><span style="color: #800000">'</span><span style="color: #000000">),&nbsp;(</span><span style="color: #000000">1</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">b</span><span style="color: #800000">'</span><span style="color: #000000">),&nbsp;(</span><span style="color: #000000">2</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">c</span><span style="color: #800000">'</span><span style="color: #000000">),&nbsp;(</span><span style="color: #000000">3</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">d</span><span style="color: #800000">'</span><span style="color: #000000">),&nbsp;(</span><span style="color: #000000">4</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">e</span><span style="color: #800000">'</span><span style="color: #000000">),&nbsp;(</span><span style="color: #000000">5</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">f</span><span style="color: #800000">'</span><span style="color: #000000">),&nbsp;(</span><span style="color: #000000">6</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">g</span><span style="color: #800000">'</span><span style="color: #000000">),&nbsp;(</span><span style="color: #000000">7</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">h</span><span style="color: #800000">'</span><span style="color: #000000">),<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;(</span><span style="color: #000000">8</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">i</span><span style="color: #800000">'</span><span style="color: #000000">),&nbsp;(</span><span style="color: #000000">9</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">j</span><span style="color: #800000">'</span><span style="color: #000000">),&nbsp;(</span><span style="color: #000000">10</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">k</span><span style="color: #800000">'</span><span style="color: #000000">),&nbsp;(</span><span style="color: #000000">11</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">l</span><span style="color: #800000">'</span><span style="color: #000000">),&nbsp;(</span><span style="color: #000000">12</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">m</span><span style="color: #800000">'</span><span style="color: #000000">),&nbsp;(</span><span style="color: #000000">13</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">n</span><span style="color: #800000">'</span><span style="color: #000000">),&nbsp;(</span><span style="color: #000000">14</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">o</span><span style="color: #800000">'</span><span style="color: #000000">),&nbsp;(</span><span style="color: #000000">15</span><span style="color: #000000">,<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">p</span><span style="color: #800000">'</span><span style="color: #000000">),&nbsp;(</span><span style="color: #000000">16</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">q</span><span style="color: #800000">'</span><span style="color: #000000">),&nbsp;(</span><span style="color: #000000">17</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">r</span><span style="color: #800000">'</span><span style="color: #000000">),&nbsp;(</span><span style="color: #000000">18</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">s</span><span style="color: #800000">'</span><span style="color: #000000">),&nbsp;(</span><span style="color: #000000">19</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">t</span><span style="color: #800000">'</span><span style="color: #000000">),&nbsp;(</span><span style="color: #000000">20</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">u</span><span style="color: #800000">'</span><span style="color: #000000">),&nbsp;(</span><span style="color: #000000">21</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">v</span><span style="color: #800000">'</span><span style="color: #000000">),&nbsp;(</span><span style="color: #000000">22</span><span style="color: #000000">,&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">w</span><span style="color: #800000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />'</span><span style="color: #800000">),&nbsp;(23,&nbsp;</span><span style="color: #800000">'</span><span style="color: #000000">x</span><span style="color: #800000">'</span><span style="color: #800000">),&nbsp;(24,&nbsp;</span><span style="color: #800000">'</span><span style="color: #000000">y</span><span style="color: #800000">'</span><span style="color: #800000">),&nbsp;(25,&nbsp;</span><span style="color: #800000">'</span><span style="color: #000000">z</span><span style="color: #800000">'</span><span style="color: #800000">)]</span><span style="color: #800000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div>
<p style="padding-bottom: 2px; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; margin: 4px 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 12px Arial, Helvetica, sans-serif; white-space: normal; orphans: 2; color: rgb(0,0,0); word-spacing: 0px; padding-top: 2px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"></p>用enumerate将string中的1都找出来，用enumerate实现： 
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #0000ff">def</span><span style="color: #000000">&nbsp;fun(line):<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;&nbsp;((ids,int(val))&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">&nbsp;ids,val&nbsp;</span><span style="color: #0000ff">in</span><span style="color: #000000">&nbsp;enumerate(line)&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">&nbsp;val&nbsp;</span><span style="color: #000000">!=</span><span style="color: #000000">&nbsp;</span><span style="color: #800000">'</span><span style="color: #800000">0</span><span style="color: #800000">'</span><span style="color: #000000">)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">print</span><span style="color: #000000">&nbsp;list(fun(</span><span style="color: #800000">"</span><span style="color: #800000">000111000111</span><span style="color: #800000">"</span><span style="color: #000000">))<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div><br />输出结果为：<br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">[(</span><span style="color: #000000">3</span><span style="color: #000000">, </span><span style="color: #000000">1</span><span style="color: #000000">), (</span><span style="color: #000000">4</span><span style="color: #000000">, </span><span style="color: #000000">1</span><span style="color: #000000">), (</span><span style="color: #000000">5</span><span style="color: #000000">, </span><span style="color: #000000">1</span><span style="color: #000000">), (</span><span style="color: #000000">9</span><span style="color: #000000">, </span><span style="color: #000000">1</span><span style="color: #000000">), (</span><span style="color: #000000">10</span><span style="color: #000000">, </span><span style="color: #000000">1</span><span style="color: #000000">), (</span><span style="color: #000000">11</span><span style="color: #000000">, </span><span style="color: #000000">1</span><span style="color: #000000">)]</span></div><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><img src ="http://www.cppblog.com/sunrise/aggbug/196702.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunrise/" target="_blank">SunRise_at</a> 2012-12-27 10:48 <a href="http://www.cppblog.com/sunrise/archive/2012/12/27/196702.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>python之Queue</title><link>http://www.cppblog.com/sunrise/archive/2012/12/02/195902.html</link><dc:creator>SunRise_at</dc:creator><author>SunRise_at</author><pubDate>Sun, 02 Dec 2012 12:32:00 GMT</pubDate><guid>http://www.cppblog.com/sunrise/archive/2012/12/02/195902.html</guid><wfw:comment>http://www.cppblog.com/sunrise/comments/195902.html</wfw:comment><comments>http://www.cppblog.com/sunrise/archive/2012/12/02/195902.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunrise/comments/commentRss/195902.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunrise/services/trackbacks/195902.html</trackback:ping><description><![CDATA[<strong style="font-size: 14px; font-family: tahoma, helvetica, arial; font-variant: normal; white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font-style: normal; orphans: 2; widows: 2; letter-spacing: normal; line-height: 21px; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">创建一个&#8220;队列&#8221;对象</strong><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="white-space: normal; text-transform: none; word-spacing: 0px; float: none; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">import Queue</span><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="white-space: normal; text-transform: none; word-spacing: 0px; float: none; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">myqueue = Queue.Queue(maxsize = 10)</span><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="white-space: normal; text-transform: none; word-spacing: 0px; float: none; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">Queue.Queue类即是一个队列的同步实现。队列长度可为无限或者有限。可通过Queue的构造函数的可选参数maxsize来设定队列长度。如果maxsize小于1就表示队列长度无限。</span><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="white-space: normal; text-transform: none; word-spacing: 0px; float: none; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">将一个值放入队列中</span><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="white-space: normal; text-transform: none; word-spacing: 0px; float: none; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">myqueue.put(10)</span><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="white-space: normal; text-transform: none; word-spacing: 0px; float: none; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">调用队列对象的put()方法在队尾插入一个项目。put()有两个参数，第一个item为必需的，为插入项目的值；第二个block为可选参数，默认为1。如果队列当前为空且block为1，put()方法就使调用线程暂停,直到空出一个数据单元。如果block为0，put方法将引发Full异常。</span><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="white-space: normal; text-transform: none; word-spacing: 0px; float: none; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">将一个值从队列中取出</span><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="white-space: normal; text-transform: none; word-spacing: 0px; float: none; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">myqueue.get()</span><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="white-space: normal; text-transform: none; word-spacing: 0px; float: none; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">调用队列对象的get()方法从队头删除并返回一个项目。可选参数为block，默认为True。如果队列为空且block为True，get()就使调用线程暂停，直至有项目可用。如果队列为空且block为False，队列将引发Empty异常。</span><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><strong style="font-size: 14px; font-family: tahoma, helvetica, arial; font-variant: normal; white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font-style: normal; orphans: 2; widows: 2; letter-spacing: normal; line-height: 21px; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">python queue模块有三种队列:</strong><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="white-space: normal; text-transform: none; word-spacing: 0px; float: none; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">1、python queue模块的FIFO队列先进先出。</span><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="white-space: normal; text-transform: none; word-spacing: 0px; float: none; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">2、LIFO类似于堆。即先进后出。</span><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="white-space: normal; text-transform: none; word-spacing: 0px; float: none; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">3、还有一种是优先级队列级别越低越先出来。<span class="Apple-converted-space">&nbsp;</span></span><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="white-space: normal; text-transform: none; word-spacing: 0px; float: none; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">针对这三种队列分别有三个构造函数:</span><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="white-space: normal; text-transform: none; word-spacing: 0px; float: none; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">1、class Queue.Queue(maxsize) FIFO<span class="Apple-converted-space">&nbsp;</span></span><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="white-space: normal; text-transform: none; word-spacing: 0px; float: none; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">2、class Queue.LifoQueue(maxsize) LIFO<span class="Apple-converted-space">&nbsp;</span></span><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="white-space: normal; text-transform: none; word-spacing: 0px; float: none; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">3、class Queue.PriorityQueue(maxsize) 优先级队列<span class="Apple-converted-space">&nbsp;</span></span><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="white-space: normal; text-transform: none; word-spacing: 0px; float: none; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">介绍一下此包中的常用方法:</span><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="white-space: normal; text-transform: none; word-spacing: 0px; float: none; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">Queue.qsize() 返回队列的大小<span class="Apple-converted-space">&nbsp;</span></span><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="white-space: normal; text-transform: none; word-spacing: 0px; float: none; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">Queue.empty() 如果队列为空，返回True,反之False<span class="Apple-converted-space">&nbsp;</span></span><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="white-space: normal; text-transform: none; word-spacing: 0px; float: none; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">Queue.full() 如果队列满了，返回True,反之False</span><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="white-space: normal; text-transform: none; word-spacing: 0px; float: none; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">Queue.full 与 maxsize 大小对应<span class="Apple-converted-space">&nbsp;</span></span><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="white-space: normal; text-transform: none; word-spacing: 0px; float: none; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">Queue.get([block[, timeout]]) 获取队列，timeout等待时间<span class="Apple-converted-space">&nbsp;</span></span><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="white-space: normal; text-transform: none; word-spacing: 0px; float: none; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">Queue.get_nowait() 相当Queue.get(False)</span><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="white-space: normal; text-transform: none; word-spacing: 0px; float: none; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">非阻塞 Queue.put(item) 写入队列，timeout等待时间<span class="Apple-converted-space">&nbsp;</span></span><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="white-space: normal; text-transform: none; word-spacing: 0px; float: none; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">Queue.put_nowait(item) 相当Queue.put(item, False)</span><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="white-space: normal; text-transform: none; word-spacing: 0px; float: none; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">Queue.task_done() 在完成一项工作之后，Queue.task_done() 函数向任务已经完成的队列发送一个信号</span><br style="white-space: normal; text-transform: none; word-spacing: 0px; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" /><span style="white-space: normal; text-transform: none; word-spacing: 0px; float: none; color: rgb(69,69,69); font: 14px/21px tahoma, helvetica, arial; orphans: 2; widows: 2; display: inline !important; letter-spacing: normal; background-color: rgb(255,255,255); text-indent: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">Queue.join() 实际上意味着等到队列为空，再执行别的操作</span><img src ="http://www.cppblog.com/sunrise/aggbug/195902.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunrise/" target="_blank">SunRise_at</a> 2012-12-02 20:32 <a href="http://www.cppblog.com/sunrise/archive/2012/12/02/195902.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>去除dbpedia中的标签</title><link>http://www.cppblog.com/sunrise/archive/2012/09/13/190540.html</link><dc:creator>SunRise_at</dc:creator><author>SunRise_at</author><pubDate>Thu, 13 Sep 2012 09:29:00 GMT</pubDate><guid>http://www.cppblog.com/sunrise/archive/2012/09/13/190540.html</guid><wfw:comment>http://www.cppblog.com/sunrise/comments/190540.html</wfw:comment><comments>http://www.cppblog.com/sunrise/archive/2012/09/13/190540.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunrise/comments/commentRss/190540.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunrise/services/trackbacks/190540.html</trackback:ping><description><![CDATA[话不多说，奉上代码。<br /><br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008000; ">#</span><span style="color: #008000; ">/usr/bin/env&nbsp;python</span><span style="color: #008000; "><br />#</span><span style="color: #008000; ">coding=utf8</span><span style="color: #008000; "><br /></span><br /><span style="color: #008000; ">#</span><span style="color: #008000; ">对提取的数据进行预处理</span><span style="color: #008000; "><br /></span><br /><span style="color: #0000FF; ">def</span>&nbsp;pretreat(infile,outfile):<br />&nbsp;&nbsp;rfile&nbsp;=&nbsp;open(infile,<span style="color: #800000; ">'</span><span style="color: #800000; ">r</span><span style="color: #800000; ">'</span>)<br />&nbsp;&nbsp;wfile&nbsp;=&nbsp;open(outfile,<span style="color: #800000; ">'</span><span style="color: #800000; ">wa+</span><span style="color: #800000; ">'</span>)<br />&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>(1):<br />&nbsp;&nbsp;&nbsp;&nbsp;line&nbsp;=&nbsp;rfile.readline()<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;<span style="color: #0000FF; ">not</span>&nbsp;line:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">break</span><br />&nbsp;&nbsp;&nbsp;&nbsp;line&nbsp;=&nbsp;line.split(<span style="color: #800000; ">'</span><span style="color: #800000; ">&gt;</span><span style="color: #800000; ">'</span>)<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">数据的长度，避免重复计算</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;lens&nbsp;=&nbsp;len(line)<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">获得有效信息</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>&nbsp;i&nbsp;<span style="color: #0000FF; ">in</span>&nbsp;range(lens):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line[i]&nbsp;=&nbsp;line[i].split(<span style="color: #800000; ">'</span><span style="color: #800000; ">/</span><span style="color: #800000; ">'</span>)<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>&nbsp;i&nbsp;<span style="color: #0000FF; ">in</span>&nbsp;range(lens):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">处理三元组第三个元素</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">print&nbsp;line[i]</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flag&nbsp;=&nbsp;0<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;<span style="color: #800000; ">'</span><span style="color: #800000; ">@zh</span><span style="color: #800000; ">'</span>&nbsp;<span style="color: #0000FF; ">in</span>&nbsp;line[i][0]:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line[i][0]&nbsp;=&nbsp;line[i][0].replace(<span style="color: #800000; ">'</span><span style="color: #800000; ">@zh&nbsp;.</span><span style="color: #800000; ">'</span>,<span style="color: #800000; ">''</span>)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line[i][0]&nbsp;=&nbsp;line[i][0].replace(<span style="color: #800000; ">'</span><span style="color: #800000; ">／</span><span style="color: #800000; ">'</span>,<span style="color: #800000; ">''</span>)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;<span style="color: #800000; ">'</span><span style="color: #800000; ">^^&lt;http:</span><span style="color: #800000; ">'</span>&nbsp;<span style="color: #0000FF; ">in</span>&nbsp;line[i][0]:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flag&nbsp;=&nbsp;1<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line[i][0]&nbsp;=&nbsp;line[i][0].replace(<span style="color: #800000; ">'</span><span style="color: #800000; ">^^&lt;http:</span><span style="color: #800000; ">'</span>,<span style="color: #800000; ">''</span>)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line[i][0]&nbsp;=&nbsp;line[i][0].replace(<span style="color: #800000; ">'</span><span style="color: #800000; ">／</span><span style="color: #800000; ">'</span>,<span style="color: #800000; ">''</span>)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">print</span>&nbsp;line[i][0]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wfile.write(line[i][0].strip())<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;len(line[i])&nbsp;&gt;=&nbsp;1&nbsp;<span style="color: #0000FF; ">and</span>&nbsp;i&nbsp;!=&nbsp;3&nbsp;<span style="color: #0000FF; ">and</span>&nbsp;0&nbsp;==&nbsp;flag:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;<span style="color: #800000; ">'</span><span style="color: #800000; ">／</span><span style="color: #800000; ">'</span>&nbsp;<span style="color: #0000FF; ">in</span>&nbsp;line[i][len(line[i])-1]:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line[i][len(line[i])-1]&nbsp;=&nbsp;line[i][len(line[i])-1].replace(<span style="color: #800000; ">'</span><span style="color: #800000; ">／</span><span style="color: #800000; ">'</span>,<span style="color: #800000; ">''</span>)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wfile.write(line[i][len(line[i])-1].strip()+<span style="color: #800000; ">'</span><span style="color: #800000; ">&nbsp;</span><span style="color: #800000; ">'</span>)<br />&nbsp;&nbsp;&nbsp;&nbsp;wfile.write(<span style="color: #800000; ">'</span><span style="color: #800000; ">\n</span><span style="color: #800000; ">'</span>)<br />&nbsp;&nbsp;wfile.close()<br /><br /><span style="color: #008000; ">#</span><span style="color: #008000; ">判断是否含有字母</span><span style="color: #008000; "><br /></span><span style="color: #0000FF; ">def</span>&nbsp;is_alphabet(input):<br />&nbsp;&nbsp;input&nbsp;=&nbsp;unicode(input,<span style="color: #800000; ">"</span><span style="color: #800000; ">utf-8</span><span style="color: #800000; ">"</span>)<br />&nbsp;&nbsp;buf&nbsp;=&nbsp;[]<br />&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>&nbsp;uchar&nbsp;<span style="color: #0000FF; ">in</span>&nbsp;input:<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(uchar&nbsp;&gt;=&nbsp;u<span style="color: #800000; ">'</span><span style="color: #800000; ">\u0041</span><span style="color: #800000; ">'</span>&nbsp;<span style="color: #0000FF; ">and</span>&nbsp;uchar&lt;=u<span style="color: #800000; ">'</span><span style="color: #800000; ">\u005a</span><span style="color: #800000; ">'</span>)&nbsp;<span style="color: #0000FF; ">or</span>&nbsp;(uchar&nbsp;&gt;=&nbsp;u<span style="color: #800000; ">'</span><span style="color: #800000; ">\u0061</span><span style="color: #800000; ">'</span>&nbsp;<span style="color: #0000FF; ">and</span>&nbsp;uchar&lt;=u<span style="color: #800000; ">'</span><span style="color: #800000; ">\u007a</span><span style="color: #800000; ">'</span>):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;True<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span>:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;False<br /><br />&nbsp;&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">去除国家名中含有字母的三元组</span><span style="color: #008000; "><br /></span><span style="color: #0000FF; ">def</span>&nbsp;removealp(infile,outfile):<br />&nbsp;&nbsp;rfile&nbsp;=&nbsp;open(infile,<span style="color: #800000; ">'</span><span style="color: #800000; ">r</span><span style="color: #800000; ">'</span>)<br />&nbsp;&nbsp;wfile&nbsp;=&nbsp;open(outfile,<span style="color: #800000; ">'</span><span style="color: #800000; ">w</span><span style="color: #800000; ">'</span>)<br />&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>(1):<br />&nbsp;&nbsp;&nbsp;&nbsp;line&nbsp;=&nbsp;rfile.readline()<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;<span style="color: #0000FF; ">not</span>&nbsp;line:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">break</span><br />&nbsp;&nbsp;&nbsp;&nbsp;linetmp&nbsp;=&nbsp;line<br />&nbsp;&nbsp;&nbsp;&nbsp;line&nbsp;=&nbsp;line.split(<span style="color: #800000; ">'</span><span style="color: #800000; ">&nbsp;</span><span style="color: #800000; ">'</span>)<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;False&nbsp;==&nbsp;is_alphabet(line[0]):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wfile.write(linetmp)<br />&nbsp;&nbsp;wfile.close()<br /><br /><br />pretreat(<span style="color: #800000; ">'</span><span style="color: #800000; ">article_categories_en_uris_zh.nt</span><span style="color: #800000; ">'</span>,<span style="color: #800000; ">'</span><span style="color: #800000; ">tag_article_categories_en_uris_zh.txt</span><span style="color: #800000; ">'</span>)</div><img src ="http://www.cppblog.com/sunrise/aggbug/190540.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunrise/" target="_blank">SunRise_at</a> 2012-09-13 17:29 <a href="http://www.cppblog.com/sunrise/archive/2012/09/13/190540.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>用python进行web应用：创建一个简单的web客户端</title><link>http://www.cppblog.com/sunrise/archive/2012/09/13/190536.html</link><dc:creator>SunRise_at</dc:creator><author>SunRise_at</author><pubDate>Thu, 13 Sep 2012 09:05:00 GMT</pubDate><guid>http://www.cppblog.com/sunrise/archive/2012/09/13/190536.html</guid><wfw:comment>http://www.cppblog.com/sunrise/comments/190536.html</wfw:comment><comments>http://www.cppblog.com/sunrise/archive/2012/09/13/190536.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunrise/comments/commentRss/190536.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunrise/services/trackbacks/190536.html</trackback:ping><description><![CDATA[<div>一.首先讲一下统一资源定位符，简单的Web应用被称为URL（统一资源定位器，Uniform Resource Locator）的web地址。URL是大型标识符URI（统一资源标识，Uniform Resource Identifier）的一部分。</div>
<div>网络定位元素：</div>
<div>1.部件：描述</div>
<div>2.user:登录名</div>
<div>3.password:用户的密码</div>
<div>4.host:web服务器运行的机器名或地址</div>
<div>5.port:端口号</div>
<div>二.urllib2模块</div>
<div>urllib2是python的一个获取url（Uniform Resource Locators，统一资源定址器）的模块。它用urlopen函数的形式提供了一个非常简洁的接口。这使得用各种各样的协议获取url成为可能。它同时 也提供了一个稍微复杂的接口来处理常见的状况-如基本的认证，cookies，代理，等等。这些都是由叫做opener和handler的对象来处理的。<br /><div style="font-size: 13px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; border-image: initial; padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; background-color: #eeeeee; "><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">import</span>&nbsp;urllib2&nbsp;&nbsp;<br />response&nbsp;=&nbsp;urllib2.urlopen(<span style="color: #800000; ">'</span><span style="color: #800000; ">http://python.org/</span><span style="color: #800000; ">'</span>)&nbsp;&nbsp;<br />html&nbsp;=&nbsp;response.read()&nbsp;</div>三.下面是一个通过访问yago的提供的接口来统计wiki中数据在yago中的覆盖率实例<br /><div style="font-size: 13px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; border-image: initial; padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; background-color: #eeeeee; "><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">def</span>&nbsp;coveragerate(infile,outfile):<br />&nbsp;&nbsp;rfile&nbsp;=&nbsp;open(infile,<span style="color: #800000; ">'</span><span style="color: #800000; ">r</span><span style="color: #800000; ">'</span>)<br />&nbsp;&nbsp;wfile&nbsp;=&nbsp;open(outfile,<span style="color: #800000; ">'</span><span style="color: #800000; ">wa+</span><span style="color: #800000; ">'</span>)<br />&nbsp;&nbsp;unfindcount&nbsp;=&nbsp;0.0<br />&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>(1):<br />&nbsp;&nbsp;&nbsp;&nbsp;line&nbsp;=&nbsp;rfile.readline()<br />&nbsp;&nbsp;&nbsp;&nbsp;tmpline&nbsp;=&nbsp;line<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;<span style="color: #0000FF; ">not</span>&nbsp;line:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">break</span><br />&nbsp;&nbsp;&nbsp;&nbsp;line&nbsp;=&nbsp;line.split(<span style="color: #800000; ">'</span><span style="color: #800000; ">&nbsp;</span><span style="color: #800000; ">'</span>)<br />&nbsp;&nbsp;&nbsp;&nbsp;URL&nbsp;=&nbsp;<span style="color: #800000; ">'</span><span style="color: #800000; ">https://d5gate.ag5.mpi-sb.mpg.de/webyagospotlx/WebInterface?passedQuery=I%3A0%09S%3A</span><span style="color: #800000; ">'</span><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>&nbsp;i&nbsp;<span style="color: #0000FF; ">in</span>&nbsp;range(len(line)):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;i&nbsp;==&nbsp;0:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;URL&nbsp;+=&nbsp;line[i].strip()<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span>:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;URL&nbsp;+=&nbsp;<span style="color: #800000; ">'</span><span style="color: #800000; ">%20</span><span style="color: #800000; ">'</span>&nbsp;+&nbsp;line[i].strip()<br />&nbsp;&nbsp;&nbsp;&nbsp;URL&nbsp;+=&nbsp;<span style="color: #800000; ">'</span><span style="color: #800000; ">%3B</span><span style="color: #800000; ">'</span><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">print</span>&nbsp;URL<br />&nbsp;&nbsp;&nbsp;&nbsp;req&nbsp;=&nbsp;urllib2.Request(URL)<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">try</span>:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;response&nbsp;=&nbsp;urllib2.urlopen(req)<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">except</span>&nbsp;URLError,e:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">print</span>&nbsp;e.reason<br />&nbsp;&nbsp;&nbsp;&nbsp;html&nbsp;=&nbsp;response.read().split(<span style="color: #800000; ">'</span><span style="color: #800000; ">\n</span><span style="color: #800000; ">'</span>)<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>&nbsp;i&nbsp;<span style="color: #0000FF; ">in</span>&nbsp;range(len(html)):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;<span style="color: #800000; ">'</span><span style="color: #800000; ">&lt;/div&gt;&lt;h2&gt;Results&lt;/h2&gt;There&nbsp;were&nbsp;no&nbsp;results.&lt;P&gt;</span><span style="color: #800000; ">'</span>&nbsp;<span style="color: #0000FF; ">in</span>&nbsp;html:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unfindcount&nbsp;+=&nbsp;1.0<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wfile.write(tmpline.strip()&nbsp;+&nbsp;<span style="color: #800000; ">'</span><span style="color: #800000; ">&nbsp;Flase</span><span style="color: #800000; ">'</span>+<span style="color: #800000; ">'</span><span style="color: #800000; ">\n</span><span style="color: #800000; ">'</span>)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">print</span>&nbsp;<span style="color: #800000; ">'</span><span style="color: #800000; ">run</span><span style="color: #800000; ">'</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">break</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span>:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wfile.write(tmpline.strip()&nbsp;+&nbsp;<span style="color: #800000; ">'</span><span style="color: #800000; ">&nbsp;True</span><span style="color: #800000; ">'</span>+<span style="color: #800000; ">'</span><span style="color: #800000; ">\n</span><span style="color: #800000; ">'</span>)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">print</span>&nbsp;<span style="color: #800000; ">'</span><span style="color: #800000; ">run</span><span style="color: #800000; ">'</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">break</span><br />&nbsp;&nbsp;covrate&nbsp;=&nbsp;1.0&nbsp;-&nbsp;(unfindcount/len(html))<br />&nbsp;&nbsp;wfile(covrate)<br />&nbsp;&nbsp;wfile.close()<br />&nbsp;&nbsp;rfile.close()</div><font color="#800000"><br /><span>通过观察yago在进行查询的时候URL的变化规则，来补全URL，在进行访问就可以获得该页面的html源码。最后通过分析源码，来判断是否被查询到，最后在统计一下能够被覆盖的数据即可。</span><br /><br /><br /></font></div><img src ="http://www.cppblog.com/sunrise/aggbug/190536.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunrise/" target="_blank">SunRise_at</a> 2012-09-13 17:05 <a href="http://www.cppblog.com/sunrise/archive/2012/09/13/190536.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>python判断unicode是否是汉字，数字，英文，或者其他字符</title><link>http://www.cppblog.com/sunrise/archive/2012/08/29/188654.html</link><dc:creator>SunRise_at</dc:creator><author>SunRise_at</author><pubDate>Wed, 29 Aug 2012 09:47:00 GMT</pubDate><guid>http://www.cppblog.com/sunrise/archive/2012/08/29/188654.html</guid><wfw:comment>http://www.cppblog.com/sunrise/comments/188654.html</wfw:comment><comments>http://www.cppblog.com/sunrise/archive/2012/08/29/188654.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunrise/comments/commentRss/188654.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunrise/services/trackbacks/188654.html</trackback:ping><description><![CDATA[<span style="color: #454545; font-family: Tahoma, Arial, Helvetica, STHeiti; line-height: 25px; ">ps:中文处理经验不足，学习了。<br />下面这个小工具包含了 判断unicode是否是汉字，数字，英文，或者其他字符。 全角符号转半角符号。 unicode字符串归一化等工作。 还有一个能处理多音字的汉字转拼音的程序，还在整理中。<br />转自：</span><a href="http://hi.baidu.com/fenghua1893/item/d1a71d5ac47ffdcfd3e10cd1">http://hi.baidu.com/fenghua1893/item/d1a71d5ac47ffdcfd3e10cd1</a><span style="color: #454545; font-family: Tahoma, Arial, Helvetica, STHeiti; line-height: 25px; "><br /></span><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008000; ">#</span><span style="color: #008000; ">!/usr/bin/env&nbsp;python</span><span style="color: #008000; "><br />#</span><span style="color: #008000; ">&nbsp;-*-&nbsp;coding:GBK&nbsp;-*-&nbsp;</span><span style="color: #008000; "><br /></span>&nbsp;<br /><span style="color: #800000; ">"""</span><span style="color: #800000; ">汉字处理的工具:<br />判断unicode是否是汉字，数字，英文，或者其他字符。<br />全角符号转半角符号。</span><span style="color: #800000; ">"""</span><font color="#800080"><br /></font>&nbsp;<br /><span style="color: #0000FF; ">def</span>&nbsp;is_chinese(uchar):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #800000; ">"""</span><span style="color: #800000; ">判断一个unicode是否是汉字</span><span style="color: #800000; ">"""</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;uchar&nbsp;&gt;=&nbsp;u<span style="color: #800000; ">'</span><span style="color: #800000; ">\u4e00</span><span style="color: #800000; ">'</span>&nbsp;<span style="color: #0000FF; ">and</span>&nbsp;uchar&lt;=u<span style="color: #800000; ">'</span><span style="color: #800000; ">\u9fa5</span><span style="color: #800000; ">'</span>:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;True<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span>:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;False<br />&nbsp;<br /><span style="color: #0000FF; ">def</span>&nbsp;is_number(uchar):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #800000; ">"""</span><span style="color: #800000; ">判断一个unicode是否是数字</span><span style="color: #800000; ">"""</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;uchar&nbsp;&gt;=&nbsp;u<span style="color: #800000; ">'</span><span style="color: #800000; ">\u0030</span><span style="color: #800000; ">'</span>&nbsp;<span style="color: #0000FF; ">and</span>&nbsp;uchar&lt;=u<span style="color: #800000; ">'</span><span style="color: #800000; ">\u0039</span><span style="color: #800000; ">'</span>:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;True<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span>:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;False<br />&nbsp;<br /><span style="color: #0000FF; ">def</span>&nbsp;is_alphabet(uchar):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #800000; ">"""</span><span style="color: #800000; ">判断一个unicode是否是英文字母</span><span style="color: #800000; ">"""</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(uchar&nbsp;&gt;=&nbsp;u<span style="color: #800000; ">'</span><span style="color: #800000; ">\u0041</span><span style="color: #800000; ">'</span>&nbsp;<span style="color: #0000FF; ">and</span>&nbsp;uchar&lt;=u<span style="color: #800000; ">'</span><span style="color: #800000; ">\u005a</span><span style="color: #800000; ">'</span>)&nbsp;<span style="color: #0000FF; ">or</span>&nbsp;(uchar&nbsp;&gt;=&nbsp;u<span style="color: #800000; ">'</span><span style="color: #800000; ">\u0061</span><span style="color: #800000; ">'</span>&nbsp;<span style="color: #0000FF; ">and</span>&nbsp;uchar&lt;=u<span style="color: #800000; ">'</span><span style="color: #800000; ">\u007a</span><span style="color: #800000; ">'</span>):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;True<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span>:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;False<br />&nbsp;<br /><span style="color: #0000FF; ">def</span>&nbsp;is_other(uchar):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #800000; ">"""</span><span style="color: #800000; ">判断是否非汉字，数字和英文字符</span><span style="color: #800000; ">"""</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;<span style="color: #0000FF; ">not</span>&nbsp;(is_chinese(uchar)&nbsp;<span style="color: #0000FF; ">or</span>&nbsp;is_number(uchar)&nbsp;<span style="color: #0000FF; ">or</span>&nbsp;is_alphabet(uchar)):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;True<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span>:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;False<br />&nbsp;<br /><span style="color: #0000FF; ">def</span>&nbsp;B2Q(uchar):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #800000; ">"""</span><span style="color: #800000; ">半角转全角</span><span style="color: #800000; ">"""</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inside_code=ord(uchar)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;inside_code&lt;0x0020&nbsp;<span style="color: #0000FF; ">or</span>&nbsp;inside_code&gt;0x7e:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">不是半角字符就返回原来的字符</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;uchar<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;inside_code==0x0020:&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">除了空格其他的全角半角的公式为:半角=全角-0xfee0</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inside_code=0x3000<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span>:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inside_code+=0xfee0<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;unichr(inside_code)<br />&nbsp;<br /><span style="color: #0000FF; ">def</span>&nbsp;Q2B(uchar):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #800000; ">"""</span><span style="color: #800000; ">全角转半角</span><span style="color: #800000; ">"""</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inside_code=ord(uchar)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;inside_code==0x3000:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inside_code=0x0020<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span>:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inside_code-=0xfee0<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;inside_code&lt;0x0020&nbsp;<span style="color: #0000FF; ">or</span>&nbsp;inside_code&gt;0x7e:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">转完之后不是半角字符返回原来的字符</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;uchar<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;unichr(inside_code)<br /><br /><br />&nbsp;<br /><span style="color: #0000FF; ">def</span>&nbsp;stringQ2B(ustring):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #800000; ">"""</span><span style="color: #800000; ">把字符串全角转半角</span><span style="color: #800000; ">"""</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;<span style="color: #800000; ">""</span>.join([Q2B(uchar)&nbsp;<span style="color: #0000FF; ">for</span>&nbsp;uchar&nbsp;<span style="color: #0000FF; ">in</span>&nbsp;ustring])<br />&nbsp;<br /><span style="color: #0000FF; ">def</span>&nbsp;uniform(ustring):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #800000; ">"""</span><span style="color: #800000; ">格式化字符串，完成全角转半角，大写转小写的工作</span><span style="color: #800000; ">"""</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;stringQ2B(ustring).lower()<br />&nbsp;<br /><span style="color: #0000FF; ">def</span>&nbsp;string2List(ustring):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #800000; ">"""</span><span style="color: #800000; ">将ustring按照中文，字母，数字分开</span><span style="color: #800000; ">"""</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;retList=[]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;utmp=[]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>&nbsp;uchar&nbsp;<span style="color: #0000FF; ">in</span>&nbsp;ustring:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;is_other(uchar):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;len(utmp)==0:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">continue</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span>:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;retList.append(<span style="color: #800000; ">""</span>.join(utmp))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;utmp=[]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">else</span>:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;utmp.append(uchar)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;len(utmp)!=0:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;retList.append(<span style="color: #800000; ">""</span>.join(utmp))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;retList<br />&nbsp;<br /><span style="color: #0000FF; ">if</span>&nbsp;<span style="color: #800080; ">__name__</span>==<span style="color: #800000; ">"</span><span style="color: #800000; ">__main__</span><span style="color: #800000; ">"</span>:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">test&nbsp;Q2B&nbsp;and&nbsp;B2Q</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>&nbsp;i&nbsp;<span style="color: #0000FF; ">in</span>&nbsp;range(0x0020,0x007F):<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">print</span>&nbsp;Q2B(B2Q(unichr(i))),B2Q(unichr(i))<br />&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">test&nbsp;uniform</span><span style="color: #008000; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ustring=u<span style="color: #800000; ">'</span><span style="color: #800000; ">中国&nbsp;人名ａ高频Ａ</span><span style="color: #800000; ">'</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ustring=uniform(ustring)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ret=string2List(ustring)<br /><br /><br /><br /><br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">print</span>&nbsp;ret<br /></div><img src ="http://www.cppblog.com/sunrise/aggbug/188654.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunrise/" target="_blank">SunRise_at</a> 2012-08-29 17:47 <a href="http://www.cppblog.com/sunrise/archive/2012/08/29/188654.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>python中对Excel进行读写操作</title><link>http://www.cppblog.com/sunrise/archive/2012/07/10/182616.html</link><dc:creator>SunRise_at</dc:creator><author>SunRise_at</author><pubDate>Tue, 10 Jul 2012 06:54:00 GMT</pubDate><guid>http://www.cppblog.com/sunrise/archive/2012/07/10/182616.html</guid><wfw:comment>http://www.cppblog.com/sunrise/comments/182616.html</wfw:comment><comments>http://www.cppblog.com/sunrise/archive/2012/07/10/182616.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunrise/comments/commentRss/182616.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunrise/services/trackbacks/182616.html</trackback:ping><description><![CDATA[<div>python中对Excel进行读写操作</div><div></div><div>额，因为烦透了手动的操作Excel表，就花时间把python操作Excel表学了一下。好吧，我承认我很懒......</div><div></div><div>模块介绍：</div><div>xlrd 0.7.5 &nbsp;下载地址：http://pypi.python.org/pypi/xlrd &nbsp;文档地址：https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html</div><div>xlwt 0.7.3 &nbsp;下载地址：http://pypi.python.org/pypi/xlwt &nbsp;文档地址：https://secure.simplistix.co.uk/svn/xlwt/trunk/xlwt/doc/xlwt.html</div><div></div><div>模块安装：</div><div>分别进入到xlrd和xlwt文件中对setup.py进行安装，安装命令为setup.py install</div><div>进入python解释器，输入import xlwt,正常无报错即可</div><div></div><div>下面是我写的一个将Excle文件中的数据读取到普通文本中，和从普通文本写到Excel的一个互换程序:<br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080; ">&nbsp;1</span>&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">encoding:utf8</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">&nbsp;2</span>&nbsp;<span style="color: #008000; "></span><span style="color: #0000FF; ">import</span>&nbsp;xlrd<br /><span style="color: #008080; ">&nbsp;3</span>&nbsp;<span style="color: #0000FF; ">import</span>&nbsp;xlwt<br /><span style="color: #008080; ">&nbsp;4</span>&nbsp;<br /><span style="color: #008080; ">&nbsp;5</span>&nbsp;<span style="color: #0000FF; ">class</span>&nbsp;OperExcel():<br /><span style="color: #008080; ">&nbsp;6</span>&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">读取Excel表</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">&nbsp;7</span>&nbsp;<span style="color: #008000; "></span>&nbsp;&nbsp;<span style="color: #0000FF; ">def</span>&nbsp;rExcel(self,inEfile,outfile):<br /><span style="color: #008080; ">&nbsp;8</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rfile&nbsp;=&nbsp;xlrd.open_workbook(inEfile)<br /><span style="color: #008080; ">&nbsp;9</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">创建索引顺序获取一个工作表</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">10</span>&nbsp;<span style="color: #008000; "></span>&nbsp;&nbsp;&nbsp;&nbsp;table&nbsp;=&nbsp;rfile.sheet_by_index(0)<br /><span style="color: #008080; ">11</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">其他方式</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">12</span>&nbsp;<span style="color: #008000; "></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">table&nbsp;=&nbsp;rfile.sheets()[0]</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">13</span>&nbsp;<span style="color: #008000; "></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">table&nbsp;=&nbsp;rfile.sheet_by_name(u'Sheet1')</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">14</span>&nbsp;<span style="color: #008000; "></span><br /><span style="color: #008080; ">15</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">获取整行，整列的值</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">16</span>&nbsp;<span style="color: #008000; "></span>&nbsp;&nbsp;&nbsp;&nbsp;table.row_values(0)<br /><span style="color: #008080; ">17</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;table.col_values(0)<br /><span style="color: #008080; ">18</span>&nbsp;<br /><span style="color: #008080; ">19</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">获取行数和列数</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">20</span>&nbsp;<span style="color: #008000; "></span>&nbsp;&nbsp;&nbsp;&nbsp;nrows&nbsp;=&nbsp;table.nrows&nbsp;-&nbsp;1<br /><span style="color: #008080; ">21</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ncols&nbsp;=&nbsp;table.ncols<br /><span style="color: #008080; ">22</span>&nbsp;<br /><span style="color: #008080; ">23</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">循环获取列表的数据</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">24</span>&nbsp;<span style="color: #008000; "></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">for&nbsp;i&nbsp;in&nbsp;range(nrows):</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">25</span>&nbsp;<span style="color: #008000; "></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">&nbsp;&nbsp;print&nbsp;table.row_values(i)</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">26</span>&nbsp;<span style="color: #008000; "></span>&nbsp;&nbsp;&nbsp;&nbsp;wfile&nbsp;=&nbsp;open(outfile,<span style="color: #800000; ">'</span><span style="color: #800000; ">w</span><span style="color: #800000; ">'</span>)<br /><span style="color: #008080; ">27</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">获取第一列中的所有值</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">28</span>&nbsp;<span style="color: #008000; "></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>&nbsp;i&nbsp;<span style="color: #0000FF; ">in</span>&nbsp;range(nrows):<br /><span style="color: #008080; ">29</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">table.cell(i,0).value获取某一单元格的值</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">30</span>&nbsp;<span style="color: #008000; "></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wfile.write(table.cell(i,0).value.encode(<span style="color: #800000; ">'</span><span style="color: #800000; ">utf8</span><span style="color: #800000; ">'</span>)&nbsp;+&nbsp;<span style="color: #800000; ">'</span><span style="color: #800000; ">\n</span><span style="color: #800000; ">'</span>)<br /><span style="color: #008080; ">31</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wfile.close()<br /><span style="color: #008080; ">32</span>&nbsp;<br /><span style="color: #008080; ">33</span>&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">将数据写入Excel表</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">34</span>&nbsp;<span style="color: #008000; "></span>&nbsp;&nbsp;<span style="color: #0000FF; ">def</span>&nbsp;wExcel(self,infile,outEfile):<br /><span style="color: #008080; ">35</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rfile&nbsp;=&nbsp;open(infile,<span style="color: #800000; ">'</span><span style="color: #800000; ">r</span><span style="color: #800000; ">'</span>)<br /><span style="color: #008080; ">36</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buf&nbsp;=&nbsp;rfile.read().split(<span style="color: #800000; ">'</span><span style="color: #800000; ">\n</span><span style="color: #800000; ">'</span>)<br /><span style="color: #008080; ">37</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rfile.close()<br /><span style="color: #008080; ">38</span>&nbsp;<br /><span style="color: #008080; ">39</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;w&nbsp;=&nbsp;xlwt.Workbook()<br /><span style="color: #008080; ">40</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sheet&nbsp;=&nbsp;w.add_sheet(<span style="color: #800000; ">'</span><span style="color: #800000; ">sheet1</span><span style="color: #800000; ">'</span>)<br /><span style="color: #008080; ">41</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">for</span>&nbsp;i&nbsp;<span style="color: #0000FF; ">in</span>&nbsp;range(len(buf)):<br /><span style="color: #008080; ">42</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">print</span>&nbsp;buf[i]<br /><span style="color: #008080; ">43</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sheet.write(i,0,buf[i].decode(<span style="color: #800000; ">'</span><span style="color: #800000; ">utf8</span><span style="color: #800000; ">'</span>))<br /><span style="color: #008080; ">44</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;w.save(outEfile)<br /><span style="color: #008080; ">45</span>&nbsp;<br /><span style="color: #008080; ">46</span>&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;<span style="color: #800080; ">__name__</span>&nbsp;==&nbsp;<span style="color: #800000; ">'</span><span style="color: #800000; ">__main__</span><span style="color: #800000; ">'</span>:<br /><span style="color: #008080; ">47</span>&nbsp;&nbsp;&nbsp;t&nbsp;=&nbsp;OperExcel()<br /><span style="color: #008080; ">48</span>&nbsp;&nbsp;&nbsp;t.rExcel(<span style="color: #800000; ">'</span><span style="color: #800000; ">test.xls</span><span style="color: #800000; ">'</span>,<span style="color: #800000; ">'</span><span style="color: #800000; ">test</span><span style="color: #800000; ">'</span>)<br /><span style="color: #008080; ">49</span>&nbsp;&nbsp;&nbsp;t.wExcel(<span style="color: #800000; ">'</span><span style="color: #800000; ">test</span><span style="color: #800000; ">'</span>,<span style="color: #800000; ">'</span><span style="color: #800000; ">1.xls</span><span style="color: #800000; ">'</span>)<br /><span style="color: #008080; ">50</span>&nbsp;</div><br /><br /><br /></div><img src ="http://www.cppblog.com/sunrise/aggbug/182616.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunrise/" target="_blank">SunRise_at</a> 2012-07-10 14:54 <a href="http://www.cppblog.com/sunrise/archive/2012/07/10/182616.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>文本预处理模板初稿(二)</title><link>http://www.cppblog.com/sunrise/archive/2012/05/07/173899.html</link><dc:creator>SunRise_at</dc:creator><author>SunRise_at</author><pubDate>Mon, 07 May 2012 06:32:00 GMT</pubDate><guid>http://www.cppblog.com/sunrise/archive/2012/05/07/173899.html</guid><wfw:comment>http://www.cppblog.com/sunrise/comments/173899.html</wfw:comment><comments>http://www.cppblog.com/sunrise/archive/2012/05/07/173899.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunrise/comments/commentRss/173899.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunrise/services/trackbacks/173899.html</trackback:ping><description><![CDATA[额 闲得没事，写的文本预处理模板，很简单，但是为了以后处理文本时，不再做重复的事，就整理了一下，随着处理文本增多，会慢慢更新。。<br />
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><div>#!/usr/bin/enc python</div><div>#-*-coding:UTF-8 -*-</div><div>#</div><div>#</div><div>#Copyrigtht (c)</div><div>#Laiseek Comany 2012</div><div>#All rights reserved.</div><div>#</div><div>#Finename:nearsyn.py</div><div>#Function:预处理工具</div><div>#</div><div>#Current version:1.0</div><div>#author: Chen Yu</div><div>#Date: 05/07/2012</div><div>#</div><div></div><div>import re</div><div></div><div>class NearSyn:</div><div>&nbsp; def init(self):</div><div>&nbsp; &nbsp; self.syn = []</div><div>&nbsp; &nbsp; self.det = []</div><div>&nbsp; #加载数据</div><div>&nbsp; def load(self,filename,detfile):</div><div>&nbsp; &nbsp; sock = open(filename,'r')</div><div>&nbsp; &nbsp; self.syn = sock.read().split('\n')</div><div>&nbsp; &nbsp; sock.close()</div><div>&nbsp; &nbsp; for i in range(len(self.syn)):</div><div>&nbsp; &nbsp; &nbsp; self.syn[i] = self.syn[i].split(' ')</div><div>&nbsp; &nbsp; self.det = []</div><div>&nbsp; &nbsp; sock = open(detfile,'r')</div><div>&nbsp; &nbsp; self.det = sock.read().split('\n')</div><div>&nbsp; &nbsp; sock.close()</div><div></div><div>&nbsp; #去除读入元素中的空list</div><div>&nbsp; def empty(self):</div><div>&nbsp; &nbsp; for i in range(len(self.syn)):</div><div>&nbsp; &nbsp; &nbsp; #空元素移除后元素减少</div><div>&nbsp; &nbsp; &nbsp; flag = 0</div><div>&nbsp; &nbsp; &nbsp; for j in range(len(self.syn[i])):</div><div>&nbsp; &nbsp; &nbsp; &nbsp; if self.syn[i][j - flag] == " "or len(self.syn[i][j - flag]) == 0:</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.syn[i].remove(self.syn[i][j - flag])</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag += 1</div><div></div><div>&nbsp; #从A中剔除D</div><div>&nbsp; def dete(self):</div><div>&nbsp; &nbsp; for i in range(len(self.syn)):</div><div>&nbsp; &nbsp; &nbsp; self.det[i] = self.det[i].split(' ')</div><div>&nbsp; &nbsp; for i in range(len(self.syn)):</div><div>&nbsp; &nbsp; &nbsp; for j in range(len(self.det[i])):</div><div>&nbsp; &nbsp; &nbsp; &nbsp; if self.det[i][j]:</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.syn[i].remove(self.det[i][j])</div><div></div><div>&nbsp; #合并A和B</div><div>&nbsp; def merge(self,filename1,filename2):</div><div>&nbsp; &nbsp; sock = open(filename1,'r')</div><div>&nbsp; &nbsp; buf1 = sock.read().split('\n')</div><div>&nbsp; &nbsp; sock.close()</div><div>&nbsp; &nbsp; sock = open(filename2,'r')</div><div>&nbsp; &nbsp; buf2 = sock.read().split('\n')</div><div>&nbsp; &nbsp; sock.close()</div><div>&nbsp; &nbsp; sock = open('app','w')</div><div>&nbsp; &nbsp; print len(buf1)</div><div>&nbsp; &nbsp; for i in range(len(buf1)):</div><div>&nbsp; &nbsp; &nbsp; sock.write(buf1[i] + ' '+ buf2[i]+ '\n')</div><div>&nbsp; &nbsp; sock.close()</div><div></div><div>&nbsp; #去除重复的元素</div><div>&nbsp; def unrep(self,filename):</div><div>&nbsp; &nbsp; sock = open(filename,'r')</div><div>&nbsp; &nbsp; buf = sock.read().split('\n')</div><div>&nbsp; &nbsp; sock.close()</div><div>&nbsp; &nbsp; ls = list(set(buf))</div><div>&nbsp; &nbsp; ls.sort(key = buf.index)</div><div>&nbsp; &nbsp; sock = open('sims2','w')</div><div>&nbsp; &nbsp; for i in range(len(ls)):</div><div>&nbsp; &nbsp; &nbsp; sock.write(ls[i] + '\n')</div><div>&nbsp; &nbsp; sock.close()</div><div></div><div>&nbsp; #去除html标签</div><div>&nbsp; def offhtml(self,infile,outfile):</div><div>&nbsp; &nbsp; rfile = open(infile,'r')</div><div>&nbsp; &nbsp; buf = rfile.read()</div><div>&nbsp; &nbsp; rfile.close()</div><div>&nbsp; &nbsp; wfile = open(outfile,'w')</div><div>&nbsp; &nbsp; buf = re.sub(r"&lt;[^!&gt;](?:[^&gt;]|\n)*&gt;", '',buf)</div><div>&nbsp; &nbsp; wfile.write(buf)</div><div></div><div>&nbsp; #输出</div><div>&nbsp; def output(self,filename):</div><div>&nbsp; &nbsp; sock = open(filename,'w')</div><div>&nbsp; &nbsp; for i in range(len(self.syn)):</div><div>&nbsp; &nbsp; &nbsp; #去掉单个词</div><div>&nbsp; &nbsp; &nbsp; if len(self.syn[i]) &gt; 1:</div><div>&nbsp; &nbsp; &nbsp; &nbsp; for j in range(len(self.syn[i])):</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sock.write(self.syn[i][j] + ' &nbsp;')</div><div>&nbsp; &nbsp; &nbsp; &nbsp; sock.write("\n")</div><div>&nbsp; &nbsp; sock.close()</div><div></div><div>if __name__ == '__main__':</div><div>&nbsp; t = NearSyn()</div></div><img src ="http://www.cppblog.com/sunrise/aggbug/173899.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunrise/" target="_blank">SunRise_at</a> 2012-05-07 14:32 <a href="http://www.cppblog.com/sunrise/archive/2012/05/07/173899.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>python学习（二）－－－ python列表排序 </title><link>http://www.cppblog.com/sunrise/archive/2012/04/27/172910.html</link><dc:creator>SunRise_at</dc:creator><author>SunRise_at</author><pubDate>Fri, 27 Apr 2012 03:28:00 GMT</pubDate><guid>http://www.cppblog.com/sunrise/archive/2012/04/27/172910.html</guid><wfw:comment>http://www.cppblog.com/sunrise/comments/172910.html</wfw:comment><comments>http://www.cppblog.com/sunrise/archive/2012/04/27/172910.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/sunrise/comments/commentRss/172910.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunrise/services/trackbacks/172910.html</trackback:ping><description><![CDATA[<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">python列表排序&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " />留着用的时候查查 &nbsp;转自：<a href="http://gaopenghigh.iteye.com/blog/1483864">http://gaopenghigh.iteye.com/blog/1483864</a><br /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">简单记一下python中List的sort方法（或者sorted内建函数）的用法。&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">关键字：&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">python列表排序 python字典排序 sorted&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">List的元素可以是各种东西，字符串，字典，自己定义的类等。&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">sorted函数用法如下：&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><div id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: #ffffff; width: 679px; margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word; line-height: 27px; text-align: left; "><div><div style="padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-weight: bold; ">Python代码&nbsp;<embed wmode="transparent" src="http://gaopenghigh.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=sorted(data%2C%20cmp%3DNone%2C%20key%3DNone%2C%20reverse%3DFalse)%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="" title="收藏这段代码" style="color: #108ac6; "><img src="http://gaopenghigh.iteye.com/images/icon_star.png" alt="收藏代码" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; " /></a></div></div><ol start="1" style="font-size: 1em; line-height: 1.4em; margin-left: 0px; padding-top: 2px; padding-bottom: 2px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #d1d7dc; border-right-color: #d1d7dc; border-bottom-color: #d1d7dc; border-left-color: #d1d7dc; color: #2b91af; "><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">sorted(data,&nbsp;cmp=None,&nbsp;key=None,&nbsp;reverse=False)&nbsp;&nbsp;</li></ol></div><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">其中，data是待排序数据，可以使List或者iterator, cmp和key都是函数，这两个函数作用与data的元素上产生一个结果，sorted方法根据这个结果来排序。&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">cmp(e1, e2) 是带两个参数的比较函数, 返回值: 负数: e1 &lt; e2, 0: e1 == e2, 正数: e1 &gt; e2. 默认为 None, 即用内建的比较函数.&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">key 是带一个参数的函数, 用来为每个元素提取比较值. 默认为 None, 即直接比较每个元素.&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">通常, key 和 reverse 比 cmp 快很多, 因为对每个元素它们只处理一次; 而 cmp 会处理多次.&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">通过例子来说明sorted的用法：&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">1. 对由tuple组成的List排序&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><div id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: #ffffff; width: 679px; margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word; line-height: 27px; text-align: left; "><div><div style="padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-weight: bold; ">Python代码&nbsp;<embed wmode="transparent" src="http://gaopenghigh.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=%3E%3E%3E%20students%20%3D%20%5B('john'%2C%20'A'%2C%2015)%2C%20('jane'%2C%20'B'%2C%2012)%2C%20('dave'%2C%20'B'%2C%2010)%2C%5D%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="" title="收藏这段代码" style="color: #108ac6; "><img src="http://gaopenghigh.iteye.com/images/icon_star.png" alt="收藏代码" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; " /></a></div></div><ol start="1" style="font-size: 1em; line-height: 1.4em; margin-left: 0px; padding-top: 2px; padding-bottom: 2px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #d1d7dc; border-right-color: #d1d7dc; border-bottom-color: #d1d7dc; border-left-color: #d1d7dc; color: #2b91af; "><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&gt;&gt;&gt;&nbsp;students&nbsp;=&nbsp;[('john',&nbsp;'A',&nbsp;15),&nbsp;('jane',&nbsp;'B',&nbsp;12),&nbsp;('dave',&nbsp;'B',&nbsp;10),]&nbsp;&nbsp;</li></ol></div><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">用key函数排序(lambda的用法见 注释1)&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><div id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: #ffffff; width: 679px; margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word; line-height: 27px; text-align: left; "><div><div style="padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-weight: bold; ">Python代码&nbsp;<embed wmode="transparent" src="http://gaopenghigh.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=%3E%3E%3E%20sorted(students%2C%20key%3Dlambda%20student%20%3A%20student%5B2%5D)%20%20%20%23%20sort%20by%20age%0A%5B('dave'%2C%20'B'%2C%2010)%2C%20('jane'%2C%20'B'%2C%2012)%2C%20('john'%2C%20'A'%2C%2015)%5D%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="" title="收藏这段代码" style="color: #108ac6; "><img src="http://gaopenghigh.iteye.com/images/icon_star.png" alt="收藏代码" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; " /></a></div></div><ol start="1" style="font-size: 1em; line-height: 1.4em; margin-left: 0px; padding-top: 2px; padding-bottom: 2px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #d1d7dc; border-right-color: #d1d7dc; border-bottom-color: #d1d7dc; border-left-color: #d1d7dc; color: #2b91af; "><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&gt;&gt;&gt;&nbsp;sorted(students,&nbsp;key=<span style="color: #7f0055; ">lambda</span>&nbsp;student&nbsp;:&nbsp;student[2])&nbsp;&nbsp;&nbsp;#&nbsp;sort&nbsp;by&nbsp;age&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">[('dave',&nbsp;'B',&nbsp;10),&nbsp;('jane',&nbsp;'B',&nbsp;12),&nbsp;('john',&nbsp;'A',&nbsp;15)]&nbsp;&nbsp;</li></ol></div><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">用cmp函数排序&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><div id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: #ffffff; width: 679px; margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word; line-height: 27px; text-align: left; "><div><div style="padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-weight: bold; ">Python代码&nbsp;<embed wmode="transparent" src="http://gaopenghigh.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=%3E%3E%3E%20sorted(students%2C%20cmp%3Dlambda%20x%2Cy%20%3A%20cmp(x%5B2%5D%2C%20y%5B2%5D))%20%23%20sort%20by%20age%0A%5B('dave'%2C%20'B'%2C%2010)%2C%20('jane'%2C%20'B'%2C%2012)%2C%20('john'%2C%20'A'%2C%2015)%5D%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="" title="收藏这段代码" style="color: #108ac6; "><img src="http://gaopenghigh.iteye.com/images/icon_star.png" alt="收藏代码" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; " /></a></div></div><ol start="1" style="font-size: 1em; line-height: 1.4em; margin-left: 0px; padding-top: 2px; padding-bottom: 2px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #d1d7dc; border-right-color: #d1d7dc; border-bottom-color: #d1d7dc; border-left-color: #d1d7dc; color: #2b91af; "><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&gt;&gt;&gt;&nbsp;sorted(students,&nbsp;cmp=<span style="color: #7f0055; ">lambda</span>&nbsp;x,y&nbsp;:&nbsp;cmp(x[2],&nbsp;y[2]))&nbsp;#&nbsp;sort&nbsp;by&nbsp;age&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">[('dave',&nbsp;'B',&nbsp;10),&nbsp;('jane',&nbsp;'B',&nbsp;12),&nbsp;('john',&nbsp;'A',&nbsp;15)]&nbsp;&nbsp;</li></ol></div><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">用 operator 函数来加快速度, 上面排序等价于:(itemgetter的用法见 注释2)&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><div id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: #ffffff; width: 679px; margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word; line-height: 27px; text-align: left; "><div><div style="padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-weight: bold; ">Python代码&nbsp;<embed wmode="transparent" src="http://gaopenghigh.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=%3E%3E%3E%20from%20operator%20import%20itemgetter%2C%20attrgetter%0A%3E%3E%3E%20sorted(students%2C%20key%3Ditemgetter(2))%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="" title="收藏这段代码" style="color: #108ac6; "><img src="http://gaopenghigh.iteye.com/images/icon_star.png" alt="收藏代码" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; " /></a></div></div><ol start="1" style="font-size: 1em; line-height: 1.4em; margin-left: 0px; padding-top: 2px; padding-bottom: 2px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #d1d7dc; border-right-color: #d1d7dc; border-bottom-color: #d1d7dc; border-left-color: #d1d7dc; color: #2b91af; "><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&gt;&gt;&gt;&nbsp;<span style="color: #7f0055; ">from</span>&nbsp;operator&nbsp;<span style="color: #7f0055; ">import</span>&nbsp;itemgetter,&nbsp;attrgetter&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&gt;&gt;&gt;&nbsp;sorted(students,&nbsp;key=itemgetter(2))&nbsp;&nbsp;</li></ol></div><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">用 operator 函数进行多级排序&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><div id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: #ffffff; width: 679px; margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word; line-height: 27px; text-align: left; "><div><div style="padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-weight: bold; ">Python代码&nbsp;<embed wmode="transparent" src="http://gaopenghigh.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=%3E%3E%3E%20sorted(students%2C%20key%3Ditemgetter(1%2C2))%20%20%23%20sort%20by%20grade%20then%20by%20age%0A%5B('john'%2C%20'A'%2C%2015)%2C%20('dave'%2C%20'B'%2C%2010)%2C%20('jane'%2C%20'B'%2C%2012)%5D%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="" title="收藏这段代码" style="color: #108ac6; "><img src="http://gaopenghigh.iteye.com/images/icon_star.png" alt="收藏代码" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; " /></a></div></div><ol start="1" style="font-size: 1em; line-height: 1.4em; margin-left: 0px; padding-top: 2px; padding-bottom: 2px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #d1d7dc; border-right-color: #d1d7dc; border-bottom-color: #d1d7dc; border-left-color: #d1d7dc; color: #2b91af; "><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&gt;&gt;&gt;&nbsp;sorted(students,&nbsp;key=itemgetter(1,2))&nbsp;&nbsp;#&nbsp;sort&nbsp;by&nbsp;grade&nbsp;then&nbsp;by&nbsp;age&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">[('john',&nbsp;'A',&nbsp;15),&nbsp;('dave',&nbsp;'B',&nbsp;10),&nbsp;('jane',&nbsp;'B',&nbsp;12)]&nbsp;&nbsp;</li></ol></div><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">2. 对由字典排序&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><div id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: #ffffff; width: 679px; margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word; line-height: 27px; text-align: left; "><div><div style="padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-weight: bold; ">Python代码&nbsp;<embed wmode="transparent" src="http://gaopenghigh.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=%3E%3E%3E%20d%20%3D%20%7B'data1'%3A3%2C%20'data2'%3A1%2C%20'data3'%3A2%2C%20'data4'%3A4%7D%0A%3E%3E%3E%20sorted(d.iteritems()%2C%20key%3Ditemgetter(1)%2C%20reverse%3DTrue)%0A%5B('data4'%2C%204)%2C%20('data1'%2C%203)%2C%20('data3'%2C%202)%2C%20('data2'%2C%201)%5D%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="" title="收藏这段代码" style="color: #108ac6; "><img src="http://gaopenghigh.iteye.com/images/icon_star.png" alt="收藏代码" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; " /></a></div></div><ol start="1" style="font-size: 1em; line-height: 1.4em; margin-left: 0px; padding-top: 2px; padding-bottom: 2px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #d1d7dc; border-right-color: #d1d7dc; border-bottom-color: #d1d7dc; border-left-color: #d1d7dc; color: #2b91af; "><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&gt;&gt;&gt;&nbsp;d&nbsp;=&nbsp;{'data1':3,&nbsp;'data2':1,&nbsp;'data3':2,&nbsp;'data4':4}&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&gt;&gt;&gt;&nbsp;sorted(d.iteritems(),&nbsp;key=itemgetter(1),&nbsp;reverse=True)&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">[('data4',&nbsp;4),&nbsp;('data1',&nbsp;3),&nbsp;('data3',&nbsp;2),&nbsp;('data2',&nbsp;1)]&nbsp;&nbsp;</li></ol></div><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">注释1&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">参考：http://jasonwu.me/2011/10/29/introduce-to-python-lambda.html&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">注释2&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">参考:http://ar.newsmth.net/thread-90745710c90cf1.html&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">class itemgetter(__builtin__.object)&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">|&nbsp; itemgetter(item, ...) --&gt; itemgetter object&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">|&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">|&nbsp; Return a callable object that fetches the given item(s) from its operand.&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">|&nbsp; After, f=itemgetter(2), the call f(r) returns r[2].&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">|&nbsp; After, g=itemgetter(2,5,3), the call g(r) returns (r[2], r[5], r[3])&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">相当于&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><div id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: #ffffff; width: 679px; margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word; line-height: 27px; text-align: left; "><div><div style="padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-weight: bold; ">Python代码&nbsp;<embed wmode="transparent" src="http://gaopenghigh.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=def%20itemgetter(i%2C*a)%3A%20%0A%20%20%20%20def%20func(obj)%3A%20%0A%20%20%20%20%20%20%20%20r%20%3D%20obj%5Bi%5D%20%0A%20%20%20%20%20%20%20%20if%20a%3A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20r%20%3D%20(r%2C)%20%2B%20tuple(obj%5Bi%5D%20for%20i%20in%20a)%20%0A%20%20%20%20%20%20%20%20return%20r%20%0A%20%20%20%20return%20func%20%0A%0A%3E%3E%3E%20a%20%3D%20%5B1%2C2%2C3%5D%20%0A%3E%3E%3E%20b%3Doperator.itemgetter(1)%20%0A%3E%3E%3E%20b(a)%20%0A2%20%0A%3E%3E%3E%20b%3Doperator.itemgetter(1%2C0)%20%0A%3E%3E%3E%20b(a)%20%0A(2%2C%201)%20%0A%3E%3E%3E%20b%3Ditemgetter(1)%20%0A%3E%3E%3E%20b(a)%20%0A2%20%0A%3E%3E%3E%20b%3Ditemgetter(1%2C0)%20%0A%3E%3E%3E%20b(a)%20%0A(2%2C%201)%20%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="" title="收藏这段代码" style="color: #108ac6; "><img src="http://gaopenghigh.iteye.com/images/icon_star.png" alt="收藏代码" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; " /></a></div></div><ol start="1" style="font-size: 1em; line-height: 1.4em; margin-left: 0px; padding-top: 2px; padding-bottom: 2px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #d1d7dc; border-right-color: #d1d7dc; border-bottom-color: #d1d7dc; border-left-color: #d1d7dc; color: #2b91af; "><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; "><span style="color: #7f0055; ">def</span>&nbsp;itemgetter(i,*a):&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7f0055; ">def</span>&nbsp;func(obj):&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r&nbsp;=&nbsp;obj[i]&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7f0055; ">if</span>&nbsp;a:&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r&nbsp;=&nbsp;(r,)&nbsp;+&nbsp;tuple(obj[i]&nbsp;<span style="color: #7f0055; ">for</span>&nbsp;i&nbsp;<span style="color: #7f0055; ">in</span>&nbsp;a)&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7f0055; ">return</span>&nbsp;r&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7f0055; ">return</span>&nbsp;func&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&gt;&gt;&gt;&nbsp;a&nbsp;=&nbsp;[1,2,3]&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&gt;&gt;&gt;&nbsp;b=operator.itemgetter(1)&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&gt;&gt;&gt;&nbsp;b(a)&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">2&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&gt;&gt;&gt;&nbsp;b=operator.itemgetter(1,0)&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&gt;&gt;&gt;&nbsp;b(a)&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">(2,&nbsp;1)&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&gt;&gt;&gt;&nbsp;b=itemgetter(1)&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&gt;&gt;&gt;&nbsp;b(a)&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">2&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&gt;&gt;&gt;&nbsp;b=itemgetter(1,0)&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&gt;&gt;&gt;&nbsp;b(a)&nbsp;&nbsp;&nbsp;</li><li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">(2,&nbsp;1)&nbsp;&nbsp;&nbsp;</li></ol></div><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">参考资料：&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">1. http://www.linuxso.com/linuxbiancheng/13340.html&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; " /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 27px; text-align: left; background-color: #ffffff; ">2. http://www.douban.com/note/13460891/</span><img src ="http://www.cppblog.com/sunrise/aggbug/172910.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunrise/" target="_blank">SunRise_at</a> 2012-04-27 11:28 <a href="http://www.cppblog.com/sunrise/archive/2012/04/27/172910.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>用python实现的字典树</title><link>http://www.cppblog.com/sunrise/archive/2012/04/05/170125.html</link><dc:creator>SunRise_at</dc:creator><author>SunRise_at</author><pubDate>Thu, 05 Apr 2012 03:04:00 GMT</pubDate><guid>http://www.cppblog.com/sunrise/archive/2012/04/05/170125.html</guid><wfw:comment>http://www.cppblog.com/sunrise/comments/170125.html</wfw:comment><comments>http://www.cppblog.com/sunrise/archive/2012/04/05/170125.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/sunrise/comments/commentRss/170125.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunrise/services/trackbacks/170125.html</trackback:ping><description><![CDATA[<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;1</span>&nbsp;<span style="color: #008000">#</span><span style="color: #008000">!/usr/bin/env&nbsp;python</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;2</span>&nbsp;<span style="color: #008000">#</span><span style="color: #008000">&nbsp;-*-coding:&nbsp;UTF-8-*-</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;3</span>&nbsp;<span style="color: #008000"></span><br /><span style="color: #008080">&nbsp;4</span>&nbsp;<span style="color: #008000">#</span><span style="color: #008000">构建节点</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;5</span>&nbsp;<span style="color: #008000"></span><span style="color: #0000ff">class</span>&nbsp;Node:<br /><span style="color: #008080">&nbsp;6</span>&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">def</span>&nbsp;<span style="color: #800080">__init__</span>(self):<br /><span style="color: #008080">&nbsp;7</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.map&nbsp;=&nbsp;{}<br /><span style="color: #008080">&nbsp;8</span>&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">def</span>&nbsp;contain(self,key):<br /><span style="color: #008080">&nbsp;9</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">return</span>&nbsp;self.map.<span style="color: #800080">__contains__</span>(key)<br /><span style="color: #008080">10</span>&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">def</span>&nbsp;<span style="color: #800080">__getitem__</span>(self,key):<br /><span style="color: #008080">11</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">return</span>&nbsp;self.map[key]<br /><span style="color: #008080">12</span>&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">def</span>&nbsp;<span style="color: #800080">__setitem__</span>(self,key,value):<br /><span style="color: #008080">13</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.map[key]=value<br /><span style="color: #008080">14</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><span style="color: #008080">15</span>&nbsp;<span style="color: #0000ff">class</span>&nbsp;Item:<br /><span style="color: #008080">16</span>&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">def</span>&nbsp;<span style="color: #800080">__init__</span>(self,key):<br /><span style="color: #008080">17</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.key=key<br /><span style="color: #008080">18</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.subNum=0<br /><span style="color: #008080">19</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.subNode=Node()<br /><span style="color: #008080">20</span>&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">def</span>&nbsp;add(self,key,item):<br /><span style="color: #008080">21</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.subNum&nbsp;+=&nbsp;1<br /><span style="color: #008080">22</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.subNode[key]&nbsp;=&nbsp;item<br /><span style="color: #008080">23</span>&nbsp;<br /><span style="color: #008080">24</span>&nbsp;<span style="color: #008000">#</span><span style="color: #008000">建树</span><span style="color: #008000"><br /></span><span style="color: #008080">25</span>&nbsp;<span style="color: #008000"></span><span style="color: #0000ff">def</span>&nbsp;build_tree(input):<br /><span style="color: #008080">26</span>&nbsp;&nbsp;&nbsp;<span style="color: #800000">"""</span><span style="color: #800000">build&nbsp;a&nbsp;tree</span><span style="color: #800000">"""</span><br /><span style="color: #008080">27</span>&nbsp;&nbsp;&nbsp;sock&nbsp;=&nbsp;open(input,<span style="color: #800000">"</span><span style="color: #800000">r</span><span style="color: #800000">"</span>)<br /><span style="color: #008080">28</span>&nbsp;&nbsp;&nbsp;buf&nbsp;=&nbsp;sock.read()<br /><span style="color: #008080">29</span>&nbsp;&nbsp;&nbsp;buf&nbsp;=&nbsp;chinese(buf)<br /><span style="color: #008080">30</span>&nbsp;&nbsp;&nbsp;tree&nbsp;=&nbsp;Item(<span style="color: #800000">"</span><span style="color: #800000">&nbsp;</span><span style="color: #800000">"</span>)<br /><span style="color: #008080">31</span>&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">for</span>&nbsp;word&nbsp;<span style="color: #0000ff">in</span>&nbsp;buf:<br /><span style="color: #008080">32</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;current&nbsp;=&nbsp;tree<br /><span style="color: #008080">33</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">for</span>&nbsp;x&nbsp;<span style="color: #0000ff">in</span>&nbsp;word:<br /><span style="color: #008080">34</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">if</span>&nbsp;current.subNode.contain(x):<br /><span style="color: #008080">35</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;current&nbsp;=&nbsp;current.subNode[x]<br /><span style="color: #008080">36</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">else</span>:<br /><span style="color: #008080">37</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item&nbsp;=&nbsp;Item(x)<br /><span style="color: #008080">38</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;current.add(x,item)<br /><span style="color: #008080">39</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;current&nbsp;=&nbsp;item<br /><span style="color: #008080">40</span>&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">print</span>&nbsp;tree<br /><span style="color: #008080">41</span>&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">return</span>&nbsp;tree<br /><span style="color: #008080">42</span>&nbsp;<br /><span style="color: #008080">43</span>&nbsp;<span style="color: #0000ff">def</span>&nbsp;search(buf,thefile):<br /><span style="color: #008080">44</span>&nbsp;&nbsp;&nbsp;<span style="color: #800000">"</span><span style="color: #800000">search.</span><span style="color: #800000">"</span><br /><span style="color: #008080">45</span>&nbsp;&nbsp;&nbsp;buf&nbsp;=&nbsp;chinese(buf)<br /><span style="color: #008080">46</span>&nbsp;&nbsp;&nbsp;tree&nbsp;=&nbsp;build_tree(thefile)<br /><span style="color: #008080">47</span>&nbsp;&nbsp;&nbsp;havefind&nbsp;=&nbsp;<span style="color: #800000">""</span><br /><span style="color: #008080">48</span>&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">for</span>&nbsp;word&nbsp;<span style="color: #0000ff">in</span>&nbsp;buf:<br /><span style="color: #008080">49</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;current&nbsp;=&nbsp;tree<br /><span style="color: #008080">50</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">for</span>&nbsp;x&nbsp;<span style="color: #0000ff">in</span>&nbsp;word:<br /><span style="color: #008080">51</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">if</span>&nbsp;current.subNode.contain(x):<br /><span style="color: #008080">52</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;current&nbsp;=&nbsp;current.subNode[x]<br /><span style="color: #008080">53</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">if</span>&nbsp;&nbsp;current.subNum&nbsp;==&nbsp;0:<br /><span style="color: #008080">54</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;havefind&nbsp;+=&nbsp;<span style="color: #800000">""</span>.join(word)<br /><span style="color: #008080">55</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">else</span>:<br /><span style="color: #008080">56</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">break</span><br /><span style="color: #008080">57</span>&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">return</span>&nbsp;havefind<br /><span style="color: #008080">58</span>&nbsp;<br /><span style="color: #008080">59</span>&nbsp;<span style="color: #008000">#</span><span style="color: #008000">判断读入的是否为汉字</span><span style="color: #008000"><br /></span><span style="color: #008080">60</span>&nbsp;<span style="color: #008000"></span><span style="color: #0000ff">def</span>&nbsp;chinese(input):<br /><span style="color: #008080">61</span>&nbsp;&nbsp;&nbsp;<span style="color: #800000">"</span><span style="color: #800000">remove&nbsp;the&nbsp;not&nbsp;chinese</span><span style="color: #800000">"</span><br /><span style="color: #008080">62</span>&nbsp;&nbsp;&nbsp;input&nbsp;=&nbsp;unicode(input,<span style="color: #800000">"</span><span style="color: #800000">utf8</span><span style="color: #800000">"</span>)<br /><span style="color: #008080">63</span>&nbsp;&nbsp;&nbsp;buf&nbsp;=&nbsp;[]<br /><span style="color: #008080">64</span>&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">for</span>&nbsp;word&nbsp;<span style="color: #0000ff">in</span>&nbsp;input:<br /><span style="color: #008080">65</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">if</span>&nbsp;word&nbsp;&gt;=&nbsp;u<span style="color: #800000">'</span><span style="color: #800000">\u4e00</span><span style="color: #800000">'</span>&nbsp;<span style="color: #0000ff">and</span>&nbsp;word&nbsp;&lt;=&nbsp;u<span style="color: #800000">'</span><span style="color: #800000">\u9fa5</span><span style="color: #800000">'</span>:<br /><span style="color: #008080">66</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buf&nbsp;=&nbsp;buf+[word.encode(<span style="color: #800000">'</span><span style="color: #800000">utf-8</span><span style="color: #800000">'</span>)]<br /><span style="color: #008080">67</span>&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">return</span>&nbsp;buf<br /><span style="color: #008080">68</span>&nbsp;<br /><span style="color: #008080">69</span>&nbsp;<br /><span style="color: #008080">70</span>&nbsp;<span style="color: #0000ff">def</span>&nbsp;run():<br /><span style="color: #008080">71</span>&nbsp;&nbsp;&nbsp;fileName&nbsp;=&nbsp;<span style="color: #800000">"</span><span style="color: #800000">test</span><span style="color: #800000">"</span><br /><span style="color: #008080">72</span>&nbsp;&nbsp;&nbsp;textChar&nbsp;=&nbsp;<span style="color: #800000">"</span><span style="color: #800000">我</span><span style="color: #800000">"</span><br /><span style="color: #008080">73</span>&nbsp;&nbsp;&nbsp;buf&nbsp;=&nbsp;search(textChar,fileName)<br /><span style="color: #008080">74</span>&nbsp;&nbsp;&nbsp;<span style="color: #0000ff">print</span>&nbsp;buf<br /><span style="color: #008080">75</span>&nbsp;<br /><span style="color: #008080">76</span>&nbsp;<span style="color: #0000ff">if</span>&nbsp;<span style="color: #800080">__name__</span>&nbsp;==&nbsp;<span style="color: #800000">"</span><span style="color: #800000">__main__</span><span style="color: #800000">"</span>:<br /><span style="color: #008080">77</span>&nbsp;&nbsp;&nbsp;run()</div>有关字典树的理论和概念可以参见<a href="http://blog.csdn.net/v_july_v/article/details/6897097">http://blog.csdn.net/v_july_v/article/details/6897097</a><br />有关python的建树过程<a href="http://bytes.com/topic/python/answers/827476-dictionary-tree-format-hopefully-simple">http://bytes.com/topic/python/answers/827476-dictionary-tree-format-hopefully-simple</a><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><img src ="http://www.cppblog.com/sunrise/aggbug/170125.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunrise/" target="_blank">SunRise_at</a> 2012-04-05 11:04 <a href="http://www.cppblog.com/sunrise/archive/2012/04/05/170125.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>python中的专用类方法</title><link>http://www.cppblog.com/sunrise/archive/2012/03/28/169270.html</link><dc:creator>SunRise_at</dc:creator><author>SunRise_at</author><pubDate>Wed, 28 Mar 2012 06:43:00 GMT</pubDate><guid>http://www.cppblog.com/sunrise/archive/2012/03/28/169270.html</guid><wfw:comment>http://www.cppblog.com/sunrise/comments/169270.html</wfw:comment><comments>http://www.cppblog.com/sunrise/archive/2012/03/28/169270.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.cppblog.com/sunrise/comments/commentRss/169270.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunrise/services/trackbacks/169270.html</trackback:ping><description><![CDATA[<div>1.__getitem__专用方法</div><div><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080; ">1</span>&nbsp;&gt;&gt;&gt;&nbsp;map&nbsp;=&nbsp;{<span style="color: #800000; ">'</span><span style="color: #800000; ">name</span><span style="color: #800000; ">'</span>:<span style="color: #800000; ">'</span><span style="color: #800000; ">c小加</span><span style="color: #800000; ">'</span>}<br /><span style="color: #008080; ">2</span>&nbsp;&gt;&gt;&gt;&nbsp;map.<span style="color: #800080; ">__getitem__</span>(<span style="color: #800000; ">"</span><span style="color: #800000; ">name</span><span style="color: #800000; ">"</span>)<br /><span style="color: #008080; ">3</span>&nbsp;<span style="color: #800000; ">'</span><span style="color: #800000; ">c\xe5\xb0\x8f\xe5\x8a\xa0</span><span style="color: #800000; ">'</span></div></div><div></div><div>只是重定向到字典，返回字典的值</div><div></div><div>2.__setitem__专用方法<br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080; ">1</span>&nbsp;&gt;&gt;&gt;&nbsp;map&nbsp;=&nbsp;{<span style="color: #800000; ">'</span><span style="color: #800000; ">name</span><span style="color: #800000; ">'</span>:<span style="color: #800000; ">'</span><span style="color: #800000; ">c小加</span><span style="color: #800000; ">'</span>}<br /><span style="color: #008080; ">2</span>&nbsp;&gt;&gt;&gt;&nbsp;map.<span style="color: #800080; ">__setitem__</span>(<span style="color: #800000; ">'</span><span style="color: #800000; ">monaker</span><span style="color: #800000; ">'</span>,<span style="color: #800000; ">'</span><span style="color: #800000; ">队花</span><span style="color: #800000; ">'</span>)<br /><span style="color: #008080; ">3</span>&nbsp;&gt;&gt;&gt;&nbsp;map<br /><span style="color: #008080; ">4</span>&nbsp;{<span style="color: #800000; ">'</span><span style="color: #800000; ">monaker</span><span style="color: #800000; ">'</span>:&nbsp;<span style="color: #800000; ">'</span><span style="color: #800000; ">\xe9\x98\x9f\xe8\x8a\xb1</span><span style="color: #800000; ">'</span>,&nbsp;<span style="color: #800000; ">'</span><span style="color: #800000; ">name</span><span style="color: #800000; ">'</span>:&nbsp;<span style="color: #800000; ">'</span><span style="color: #800000; ">c\xe5\xb0\x8f\xe5\x8a\xa0</span><span style="color: #800000; ">'</span>}<font color="#008080"><br /></font></div></div><div></div><div>map实际上是一个类，它尽可能的扮成一个字典，就像C 小加，尽可能的扮作队花，虽然他是个男的。</div><div></div><div><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080; ">1</span>&nbsp;&gt;&gt;&gt;&nbsp;map[<span style="color: #800000; ">'</span><span style="color: #800000; ">monaker</span><span style="color: #800000; ">'</span>]&nbsp;=&nbsp;<span style="color: #800000; ">'</span><span style="color: #800000; ">队花</span><span style="color: #800000; ">'</span><br /><span style="color: #008080; ">2</span>&nbsp;&gt;&gt;&gt;&nbsp;map<br /><span style="color: #008080; ">3</span>&nbsp;{<span style="color: #800000; ">'</span><span style="color: #800000; ">monaker</span><span style="color: #800000; ">'</span>:&nbsp;<span style="color: #800000; ">'</span><span style="color: #800000; ">\xe9\x98\x9f\xe8\x8a\xb1</span><span style="color: #800000; ">'</span>,&nbsp;<span style="color: #800000; ">'</span><span style="color: #800000; ">name</span><span style="color: #800000; ">'</span>:&nbsp;<span style="color: #800000; ">'</span><span style="color: #800000; ">c\xe5\xb0\x8f\xe5\x8a\xa0</span><span style="color: #800000; ">'</span>}&nbsp;</div></div><div>这个看上去像个正常的字典语法，这行代码实际上暗自调用了map.__setitem__('monaker','队花')</div><div></div><div>在类中覆盖__setitem__方法严格按照与父类方法相同的形式进行定义。参数的名字没关系，只是个数。同时直接调用父类的时候，尽管没有__setitem__.但是Python会沿着父类树走，知道找到我们正在调用方法的类。</div><div></div><div></div><div>3 .__repr__ 是一个专用的方法，在当调用 repr(instance) 时被调用。repr 函数是一个内置函数，它返回一个对象的字符串表示。它可以用在任何对象上，不仅仅是类的实例。你已经对 repr 相当熟悉了，尽管你不知道它。在交互式窗口中，当你只敲入一个变量名，接着按ENTER，Python 使用 repr 来显示变量的值。自已用一些数据来创建一个字典 d ，然后用 print repr(d) 来看一看吧。</div><div>4.__cmp__ 在比较类实例时被调用。通常，你可以通过使用 == 比较任意两个 Python 对象，不只是类实例。有一些规则，定义了何时内置数据类型被认为是相等的，例如，字典在有着全部相同的关键字和值时是相等的。对于类实例，你可以定义 __cmp__ 方法，自已编写比较逻辑，然后你可以使用 == 来比较你的类，Python 将会替你调用你的 __cmp__ 专用方法。</div><div>5.__len__ 在调用 len(instance) 时被调用。len 是一个内置函数，可以返回一个对象的长度。它可以用于任何被认为理应有长度的对象。字符串的 len 是它的字符个数；字典的 len 是它的关键字的个数；列表或序列的 len 是元素的个数。对于类实例，定义 __len__ 方法，接着自已编写长度的计算，然后调用 len(instance)，Python 将替你调用你的 __len__ 专用方法。</div><div>6.__delitem__ 在调用 del instance[key] 时调用 ，你可能记得它作为从字典中删除单个元素的方法。当你在类实例中使用 del 时，Python 替你调用 __delitem__ 专用方法。</div><div></div><div>&nbsp; &nbsp;专用方法意味着任何类 可以像字典一样保存键-值对，只要定义 __setitem__ 方法。任何类可以表现得像一个序列，只要定义 __getitem__ 方法。任何定义了 __cmp__ 方法的类可以用 == 进行比较。并且如果你的类表现为拥有类似长度的东西，不要定义 GetLength 方法，而定义 __len__ 方法，并使用 len(instance)。</div><img src ="http://www.cppblog.com/sunrise/aggbug/169270.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunrise/" target="_blank">SunRise_at</a> 2012-03-28 14:43 <a href="http://www.cppblog.com/sunrise/archive/2012/03/28/169270.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>python汉字编码</title><link>http://www.cppblog.com/sunrise/archive/2012/03/26/169049.html</link><dc:creator>SunRise_at</dc:creator><author>SunRise_at</author><pubDate>Mon, 26 Mar 2012 12:41:00 GMT</pubDate><guid>http://www.cppblog.com/sunrise/archive/2012/03/26/169049.html</guid><wfw:comment>http://www.cppblog.com/sunrise/comments/169049.html</wfw:comment><comments>http://www.cppblog.com/sunrise/archive/2012/03/26/169049.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/sunrise/comments/commentRss/169049.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunrise/services/trackbacks/169049.html</trackback:ping><description><![CDATA[<div>
<p>&nbsp;</p><div>&nbsp; &nbsp;在处理一段文本的时候，首先了解文本的汉字编码。在windows下默认的文件编码是GBK，而</div><div>linux的一般是utf-8.</div><div>想要查看文件的编码方式，使用vim打开文本，然后在命令行模式下输入：set fileencoding,回车</div><div>即可。</div><div>&nbsp; &nbsp;Python中有两种默认的字符串：str和unicode。在Python中一定要注意区分&#8220;Unicode字符串&#8221;</div><div>和&#8220;unicode对象&#8221;的区别。后面所有的&#8220;unicode字符串&#8221;指的都是python里的&#8220;unicode对象&#8221;</div><div>。</div><div>事实上在Python中并没有&#8220;Unicode字符串&#8221;这样的东西，只有&#8220;unicode&#8221;对象。一个传统意义上</div><div>的unicode字符串完全可以用str对象表示。只是这时候它仅仅是一个字节流，除非解码为unicode</div><div>对象，没有任何实际的意义。</div><div>&nbsp; 函数 decode( char_set )可以实现 其它编码到 Unicode 的转换，函数 encode( char_set )实</div><div>现 Unicode 到其它编码方式的转换。&nbsp;</div><div>&nbsp; 比如 ("你好").decode( "GB2312") 将得到 u'\u4f60\u597d'，即 "你"和&#8220;好"的 Unicode 码</div><div>分别是 0x4f60 和 0x597d</div><div>再用 (u'\u4f60\u597d').encode("UTF-8") 将得到 '\xe4\xbd\xa0\xe5\xa5\xbd'，它是 &nbsp;&#8220;你好</div><div>&#8221;的UTF-8编码结果。</div><div>python中使用 unicode的关键：unicode是一个类，函数unicode(str,"utf8")从utf8编码（当然也</div><div>可以是别的编码）的字符串str生成 unicode类的对象，而函数unc.encode("utf8")将unicode类的</div><div>对象unc转换为（编码为）utf8编码（当然也可以是别的编码）的字符串。于是，编写unicode相关</div><div>程序，需要做的事情是</div><div>&nbsp; &nbsp; * 获取数据（字符串）时，用unicode(str, "utf8")生成unicode对象</div><div>&nbsp; &nbsp; * 在程序中仅使用unicode对象，对程序中出现的字符串常量都以u"字符串"的形式书写</div><div>&nbsp; &nbsp; * 输出时，可将unicode对象转换为任意编码输出，使用str.encode("some_encoding")</div><div><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->&gt;&gt;&gt;&nbsp;unicode(<span style="color: #800000; ">"</span><span style="color: #800000; ">你好</span><span style="color: #800000; ">"</span>,&nbsp;<span style="color: #800000; ">"</span><span style="color: #800000; ">utf8</span><span style="color: #800000; ">"</span>)<br />u<span style="color: #800000; ">'</span><span style="color: #800000; ">\u4f60\u597d</span><span style="color: #800000; ">'</span><br />&gt;&gt;&gt;&nbsp;x&nbsp;=&nbsp;_<br />&gt;&gt;&gt;&nbsp;type(x)<br />&gt;&gt;&gt;&nbsp;type(<span style="color: #800000; ">"</span><span style="color: #800000; ">你好</span><span style="color: #800000; ">"</span>)<br />&gt;&gt;&gt;&nbsp;x.encode(<span style="color: #800000; ">"</span><span style="color: #800000; ">utf8</span><span style="color: #800000; ">"</span>)<br /><span style="color: #800000; ">'</span><span style="color: #800000; ">\xe4\xbd\xa0\xe5\xa5\xbd</span><span style="color: #800000; ">'</span><br />&gt;&gt;&gt;&nbsp;x.encode(<span style="color: #800000; ">"</span><span style="color: #800000; ">gbk</span><span style="color: #800000; ">"</span>)<br /><span style="color: #800000; ">'</span><span style="color: #800000; ">\xc4\xe3\xba\xc3</span><span style="color: #800000; ">'</span><br />&gt;&gt;&gt;&nbsp;x.encode(<span style="color: #800000; ">"</span><span style="color: #800000; ">gb2312</span><span style="color: #800000; ">"</span>)<br /><span style="color: #800000; ">'</span><span style="color: #800000; ">\xc4\xe3\xba\xc3</span><span style="color: #800000; ">'</span><br />&gt;&gt;&gt;&nbsp;<span style="color: #0000FF; ">print</span>&nbsp;x<br />你好<br />&gt;&gt;&gt;&nbsp;<span style="color: #0000FF; ">print</span>&nbsp;x.encode(<span style="color: #800000; ">"</span><span style="color: #800000; ">utf8</span><span style="color: #800000; ">"</span>)<br />你好<br />&gt;&gt;&gt;&nbsp;<span style="color: #0000FF; ">print</span>&nbsp;x.encode(<span style="color: #800000; ">"</span><span style="color: #800000; ">gbk</span><span style="color: #800000; ">"</span>)<br />???</div></div><div>还有一种就是对于特殊字符的处理：<br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->&nbsp;<span style="color: #0000FF; ">try</span>:<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">print</span>&nbsp;i.decode(<span style="color: #800000; ">'</span><span style="color: #800000; ">unicode_escape</span><span style="color: #800000; ">'</span>).encode(<span style="color: #800000; ">'</span><span style="color: #800000; ">utf8</span><span style="color: #800000; ">'</span>)<br />&nbsp;&nbsp;<span style="color: #0000FF; ">except</span>:<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">print</span>&nbsp;i</div></div><p>&nbsp;</p></div><img src ="http://www.cppblog.com/sunrise/aggbug/169049.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunrise/" target="_blank">SunRise_at</a> 2012-03-26 20:41 <a href="http://www.cppblog.com/sunrise/archive/2012/03/26/169049.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>(转)分析：Python在Linux平台上的发展前景</title><link>http://www.cppblog.com/sunrise/archive/2012/03/23/168716.html</link><dc:creator>SunRise_at</dc:creator><author>SunRise_at</author><pubDate>Fri, 23 Mar 2012 05:17:00 GMT</pubDate><guid>http://www.cppblog.com/sunrise/archive/2012/03/23/168716.html</guid><wfw:comment>http://www.cppblog.com/sunrise/comments/168716.html</wfw:comment><comments>http://www.cppblog.com/sunrise/archive/2012/03/23/168716.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunrise/comments/commentRss/168716.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunrise/services/trackbacks/168716.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;<a href='http://www.cppblog.com/sunrise/archive/2012/03/23/168716.html'>阅读全文</a><img src ="http://www.cppblog.com/sunrise/aggbug/168716.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunrise/" target="_blank">SunRise_at</a> 2012-03-23 13:17 <a href="http://www.cppblog.com/sunrise/archive/2012/03/23/168716.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>python中的字符串处理</title><link>http://www.cppblog.com/sunrise/archive/2012/03/19/168300.html</link><dc:creator>SunRise_at</dc:creator><author>SunRise_at</author><pubDate>Mon, 19 Mar 2012 02:34:00 GMT</pubDate><guid>http://www.cppblog.com/sunrise/archive/2012/03/19/168300.html</guid><wfw:comment>http://www.cppblog.com/sunrise/comments/168300.html</wfw:comment><comments>http://www.cppblog.com/sunrise/archive/2012/03/19/168300.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunrise/comments/commentRss/168300.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunrise/services/trackbacks/168300.html</trackback:ping><description><![CDATA[<p dir="ltr"><span style="font-size: 12pt">&nbsp;&nbsp;&nbsp;&nbsp; 1.原始字符串</span><br /><span style="font-size: 12pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Python中的原始字符串以r开头，使用原始字符串可以避免字符串中转义字符带来的问题，例如写路径时 path = 'c:\noway'，此时用 print path，其结果为：</span><br /><span style="font-size: 12pt">&nbsp;c:</span><br /><span style="font-size: 12pt">oway</span><br /><span style="font-size: 12pt">&nbsp;&nbsp;&nbsp; 如果用原始字符串 path = r'c:\noway'，则 print path，其结果为：c:\noway</span><br /><span style="font-size: 12pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2.python中没有字符的概念，字符即长度为1的字串。</span><br /><span style="font-size: 12pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.字符串之间的转换：</span><br /><span style="font-size: 12pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1） 字符串和数字之间的转换：</span><br /><span style="font-size: 12pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int（x [,radix])</span><br /><span style="font-size: 12pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long(x [,radix])</span><br /><span style="font-size: 12pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; float(x);</span><br /><span style="font-size: 12pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; round(num [,digit])</span><br /><span style="font-size: 12pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;complex(real [,imaginary])：转换为复数</span><br /><span style="font-size: 12pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ord(ch):转换为ascii码</span><br /><br /><span style="font-size: 12pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2）数字和字符串之间的转换：</span><br /><span style="font-size: 12pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 chr(x) unichr(x):将ascii码或者unicode转换为字符</span><br /><span style="font-size: 12pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2将数字转换为16或者8进制 oct（x） hex（x）</span><br /><span style="font-size: 12pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3 str（obj） 将任何对象转换为字符串</span><br />&nbsp;<br /><span style="font-size: 12pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4.字符串中的处理方法</p>
<blockquote style="margin-right: 0px" dir="ltr">
<p>摘录其整理的表格，供参考：</p>
<p>
<table border="1" cellspacing="0" cellpadding="0" sizset="30" sizcache05186508155417431="0">
<tbody sizset="30" sizcache05186508155417431="0">
<tr>
<td width="30">
<p><strong>类型</strong><strong></strong></p></td>
<td>
<p><strong>方法</strong><strong></strong></p></td>
<td>
<p><strong>注解</strong><strong></strong></p></td></tr>
<tr sizset="30" sizcache05186508155417431="0">
<td width="30">
<p>填充</p></td>
<td>
<p>center(width[, fillchar]), <br />ljust(width[, fillchar]), <br />rjust(width[, fillchar]), <br />zfill(width), <br />expandtabs([tabsize]) </p></td>
<td sizset="30" sizcache05186508155417431="0">
<ul><li>fillchar 参数指定了用以填充的字符，默认为空格</li><li>zfill的z为zero的缩写,顾名思义,是以字符0进行填充，用于数值输出</li><li>expandtabs()的tabsize 参数默认为8。它的功能是把字符串中的制表符（tab）转换为适当数量的空格。 </li></ul></td></tr>
<tr>
<td>
<p>删减</p></td>
<td>
<p>strip([chars]), <br />lstrip([chars]), <br />rstrip([chars]) </p></td>
<td>
<p>chars为指定要去掉的字符,默认为空白字符,它由string.whitespace常量定义</p></td></tr>
<tr>
<td width="30">
<p>变形</p></td>
<td>
<p>lower(),#全部小写 <br />upper(),#全部小写 <br />capitalize(),#首字母大写 <br />swapcase(),#大小写交换 <br />title()#每个单词第一个大写,其他小写</p></td>
<td>
<p>因为title() 函数并不去除字符串两端的空白符也不会把连续的空白符替换为一个空格， <br />所以建议使用string 模块中的capwords(s)函数，它能够去除两端的空白符，再将连续的空白符用一个空格代替。</p>
<p>&gt;&gt;&gt; &#8216; hello world!&#8217;.title()</p>
<p>&#8216; Hello World!&#8217;</p>
<p>&gt;&gt;&gt; string.capwords(&#8216; hello world!&#8217;)</p>
<p>&#8216;Hello World!&#8217;</p></td></tr>
<tr sizset="31" sizcache05186508155417431="0">
<td>
<p>切割</p></td>
<td>
<p>partition(sep), <br />rpartition(sep), <br />splitlines([keepends]), <br />split([sep [,maxsplit]]), <br />rsplit([sep[,maxsplit]]) </p></td>
<td sizset="31" sizcache05186508155417431="0">
<ul><li>partition()函数族是2.5版本新增的方法。它接受一个字符串参数，并返回一个3个元素的 tuple 对象。 <br />如果sep没出现在母串中，返回值是 (sep, &#8216;&#8217;, &#8216;&#8217;)； <br />否则，返回值的第一个元素是 sep 左端的部分，第二个元素是 sep 自身，第三个元素是 sep 右端的部分。</li><li>参数 maxsplit 是分切的次数，即最大的分切次数，所以返回值最多有 maxsplit+1 个元素。</li><li>s.split() 和 s.split(&#8216; &#8216;)的返回值不尽相同</li><li>&gt;&gt;&gt; &#8216; hello world!&#8217;.split()</li><li>['hello', 'world!']</li><li>&gt;&gt;&gt; &#8216; hello world!&#8217;.split(&#8216; &#8216;)</li><li>['', '', 'hello', '', '', 'world!'] </li></ul>
<p>产生差异的原因在于当忽略 sep 参数或sep参数为 None 时与明确给 sep 赋予字符串值时 split() 采用两种不同的算法。 <br />对于前者，split() 先去除字符串两端的空白符，然后以任意长度的空白符串作为界定符分切字符串 <br />即连续的空白符串被当作单一的空白符看待； <br />对于后者则认为两个连续的 sep 之间存在一个空字符串。因此对于空字符串（或空白符串），它们的返回值也是不同的：</p>
<p>&gt;&gt;&gt; &#8221;.split()</p>
<p>[]</p>
<p>&gt;&gt;&gt; &#8221;.split(&#8216; &#8216;)</p>
<p>['']</p></td></tr>
<tr>
<td>
<p>连接</p></td>
<td>
<p>join(seq)</p></td>
<td>
<p>join() 函数的高效率（相对于循环相加而言），使它成为最值得关注的字符串方法之一。 <br />它的功用是将可迭代的字符串序列连接成一条长字符串，如：</p>
<p>&gt;&gt;&gt; conf = {&#8216;host&#8217;:&#8217;127.0.0.1&#8242;,</p>
<p>&#8230; &#8216;db&#8217;:'spam&#8217;,</p>
<p>&#8230; &#8216;user&#8217;:'sa&#8217;,</p>
<p>&#8230; &#8216;passwd&#8217;:'eggs&#8217;}</p>
<p>&gt;&gt;&gt; &#8216;;&#8217;.join("%s=%s"%(k, v) for k, v in conf.iteritems())</p>
<p>&#8216;passswd=eggs;db=spam;user=sa;host=127.0.0.1&#8242;</p></td></tr>
<tr>
<td>
<p>判定</p></td>
<td>
<p>isalnum(), <br />isalpha(), <br />isdigit(), <br />islower(), <br />isupper(), <br />isspace(), <br />istitle(), <br />startswith(prefix[, start[, end]]), <br />endswith(suffix[,start[, end]]) </p></td>
<td>
<p>这些函数都比较简单，顾名知义。需要注意的是*with()函数族可以接受可选的 start, end 参数，善加利用，可以优化性能。 <br />另，自 Py2.5 版本起，*with() 函数族的 prefix 参数可以接受 tuple 类型的实参，当实参中的某人元素能够匹配，即返回 True。</p></td></tr>
<tr>
<td>
<p>查找</p></td>
<td>
<p>count( sub[, start[, end]]), <br />find( sub[, start[, end]]), <br />index( sub[, start[, end]]), <br />rfind( sub[, start[,end]]), <br />rindex( sub[, start[, end]]) </p></td>
<td>
<p>find()函数族找不到时返回-1，index()函数族则抛出ValueError异常。 <br />另，也可以用 in 和 not in 操作符来判断字符串中是否存在某个模板。</p></td></tr>
<tr>
<td>
<p>替换</p></td>
<td>
<p>replace(old, new[,count]), <br />translate(table[,deletechars]) </p></td>
<td>
<p>replace()函数的 count 参数用以指定最大替换次数 <br />translate() 的参数 table 可以由 string.maketrans(frm, to) 生成 <br />translate() 对 unicode 对象的支持并不完备，建议不要使用。</p></td></tr>
<tr>
<td>
<p>编码</p></td>
<td>
<p>encode([encoding[,errors]]), <br />decode([encoding[,errors]]) </p></td>
<td>
<p>这是一对互逆操作的方法，用以编码和解码字符串。因为str是平台相关的，它使用的内码依赖于操作系统环境， <br />而unicode是平台无关的，是Python内部的字符串存储方式。 <br />unicode可以通过编码（encode）成为特定编码的str，而str也可以通过解码（decode）成为unicode。</p></td></tr></tbody></table></p>
<div class="clear"></div>
<p></span>&nbsp;</p></blockquote><img src ="http://www.cppblog.com/sunrise/aggbug/168300.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunrise/" target="_blank">SunRise_at</a> 2012-03-19 10:34 <a href="http://www.cppblog.com/sunrise/archive/2012/03/19/168300.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>    python学习</title><link>http://www.cppblog.com/sunrise/archive/2012/03/15/167975.html</link><dc:creator>SunRise_at</dc:creator><author>SunRise_at</author><pubDate>Thu, 15 Mar 2012 06:15:00 GMT</pubDate><guid>http://www.cppblog.com/sunrise/archive/2012/03/15/167975.html</guid><wfw:comment>http://www.cppblog.com/sunrise/comments/167975.html</wfw:comment><comments>http://www.cppblog.com/sunrise/archive/2012/03/15/167975.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/sunrise/comments/commentRss/167975.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunrise/services/trackbacks/167975.html</trackback:ping><description><![CDATA[<span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">人生中的第一份个工作，NLP软件工程师。在公司接到的第一个任务就是python的学习，用了一周时间把基础看了一下，又是星期一一个明媚的早上，把上个星期学习的东西总结一下。</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp; &nbsp;</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp; 1.python只有简单的对象类型，是一种动态类型化的语言，无须申明变量类型。python中有五种内置简单类型：bool,int,long,float,complex.</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp; 通过python id 可以查看基本pyobject标识的更改方式。同时通过严格的缩进来控制函数的作用域，一但超出作用域就会被销毁，重新创建一个新的对象。</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp; 在 Python 中，浮点值始终是按双精度处理的；因此 Python 的 float 类型对应于 C 类语言中的双精度。</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp; 其中布尔类型 True和False注意首字母要大写,0、空字符串''、空元组()、表[]、空字典{}都被当做False，其他对象都为True.超过整型的数会被自动调整成长整型,复数:z.real,z.imag.</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp; 2.python中的逻辑运算符 not, and, or想当于C中的!,&amp;&amp;,||。其用法也和C中的一样，但所执行的逻辑运算并不返回bool值， 而是返回它们实际进行比较的值之一。</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp; 3.python中的容器类型：tuple,string,unicode,list,set,frozenset,dictionary。list,set和STL中的差不多意义上的理解就行了，dictionary则可跟STL中的map对应，都是映射类型。但是其中tuple,string,unicode,frozenset所存储的数据就不可更改。如果出于某种原因需要更改数据，则需要创建一个新容器来保存新的数据。其余的三种是可变的，但是dictionary中的key是不可变的。</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp; tuple是一个异构容器，创建可以拥用各种数据项，尽管不可以改变，灵活性较差，但在同一环境中使用带来的确实高速，这就是所谓的有得必有失吧。</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp; 4.在python中传建字符串,可以用单引号，双引号，三引号来定义.保证前后都有匹配就OK了.在三重引号中的任何东西都是这个函数的 doc string,doc string 在运行时可作为函数的属性。使用这个据说是非常重要的,因为在python会经常去查看一个函数的属性,它的好坏取决于您书写的 doc string 的好坏。</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp; 字符串有两种类型，str单字节字符序列，unicode 双字节字符序列.当使用 utf-8 编码时，非 unicode 字符中一个汉字的长度是 3，而使用 gb2312 时是 2.额,我们使用的就是utf-8编码,大致了解一点点.我觉得这个str对象跟C++里string的对象很象,str.+字符串方法就好了.需要用的时候查查就OK了.</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp; 5.关于python的控制流只有一句话要说的:如果一个流程控制分支下不做任何事情，记得写一句pass语句，不然Python会报错.</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp; &nbsp; 6.range(起点，终点，差距).用range（）对一个目录进行遍历</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp; a = ['mary', 'had','a',''little','lamb']</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp; for i in range(len(a));</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print i,a[i]</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp; 7.列表内涵的一个著名例子是生成九九乘法表： &nbsp;</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp; s = [(x, y, x*y) for x in range(1, 10) for y in range(1,10) if x&gt;=y] &nbsp;</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp; print s &nbsp;</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp; 列表内涵可分为三部分，最左边是生成每个元素的表达式,中间是for迭代过程，最右边是可以设定if判断的过滤条件.</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp; 8.函数的使用这个这个其实和C的函数差不多,只不过更宽泛.可以返回多个值,用逗号隔开;同时参数类型没用约束,可以输入任意类型的参数数据.</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp; python的参数提供了四种类型 &nbsp;</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp; &nbsp;1). f(arg1,arg2,...) &nbsp;</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp; &nbsp;2). f(arg1=1,arg2=2,..argn=n) 给参数提供了默认值，关键参数 &nbsp;</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp; &nbsp;3). f(*arg) arg 代表了一个tuple，类似参数列表，接收多个参数 &nbsp;</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp; &nbsp;4). f(**arg) 传入的参数在函数内部保存在一个叫arg的dict中，掉用的时候使用f(a1=v1,a2=v2)的形式 &nbsp;</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp;&nbsp; lambda 函数是匿名函数，用来定义没有名字的函数对象,lambda 关键字后就是逗号分隔的形参列表，冒号后面是一个表达式，表达式求值的结果为 lambda 的返回值.</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp; 9.好吧,开始类了.类这个我应该在一块儿停留了很久,因为刚开始的时候看错了,那些常规方法把它当作了是自己实现的,就感觉怎么会这样呢.在看一遍的时候恍然大悟啊,不细心的错.额,不废话了,进入主题.</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp; 1)类的实例化,我很欣赏这样的说法:把类当作一个函数去使用.</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp; 2)类的初始化:__init__在类的实例创建后被立即调用。注意:非结构函数,因为对象在调用 __init__ 时已经被构造出来了.当定义你自已的类方法时，你必须 明确将 self 作为每个方法的第一个参数列出，包括 __init__。当从你的类中调用一个父类的一个方法时，你必须包括 self 参数。但当你从类的外部调用你的类方法时，你不必对 self 参数指定任何值；你完全将其忽略，而 Python 会自动地替你增加实例的引用。</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp; 3)类的静态变量:</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp; class A:</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; y = 2</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp;&nbsp; print A.y</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp;&nbsp; 对象可以直接访问类的静态成员添加一个非静态成员y,但是对象不能为类的静态成员赋值</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp; 4)私有成员:</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp;&nbsp; python中没有严格意义上的私有成员,属性的名字以两个下划线开始.专有的方法是前后都有两个下划线.</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp; 5)类的继承:</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp;&nbsp; 如果有同名函数,子类会覆盖父类的方法.python中不支持重载,支持多继承.</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp; 6)类的自省</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp; 10.数据库部分</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp; 使用的是小型数据库SQLite,和大多数的数据一样有着差不多一样的操作.</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp; 1)先将SQLite作为名为sqlite3的模块导入.</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp; 2)创建一个数据库文件的连接</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp; 3)获得连接的游标,执行SQL语句.</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp; 4)每次修改数据库之后进行提交,而不是在准备关闭时才提交,准备关闭数据库使用close方法.</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp;&nbsp;&nbsp;&nbsp; 以上差不多就是学习python一个星期的总结,尽然花费了一上午的时间.好吧,去陶导那儿领任务,准备开始这个星期了.....以后要早点儿作总结.</span><br style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; " /><span style="color: #7b7d62; font-family: Arial; font-size: 16px; line-height: 26px; text-align: left; ">&nbsp; &nbsp;&nbsp;</span><img src ="http://www.cppblog.com/sunrise/aggbug/167975.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sunrise/" target="_blank">SunRise_at</a> 2012-03-15 14:15 <a href="http://www.cppblog.com/sunrise/archive/2012/03/15/167975.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>