﻿<?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</title><link>http://www.cppblog.com/sunrise/</link><description>每天不断学习，才能不断提升自己。</description><language>zh-cn</language><lastBuildDate>Sun, 05 Apr 2026 17:47:36 GMT</lastBuildDate><pubDate>Sun, 05 Apr 2026 17:47:36 GMT</pubDate><ttl>60</ttl><item><title>解决MySQL 1366 Incorrect string value</title><link>http://www.cppblog.com/sunrise/archive/2013/04/16/199476.html</link><dc:creator>SunRise_at</dc:creator><author>SunRise_at</author><pubDate>Tue, 16 Apr 2013 03:34:00 GMT</pubDate><guid>http://www.cppblog.com/sunrise/archive/2013/04/16/199476.html</guid><wfw:comment>http://www.cppblog.com/sunrise/comments/199476.html</wfw:comment><comments>http://www.cppblog.com/sunrise/archive/2013/04/16/199476.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunrise/comments/commentRss/199476.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunrise/services/trackbacks/199476.html</trackback:ping><description><![CDATA[引起该错误的原因是mysql和插入数据的字符集不统一，所以我把字符集都统一成了utf8。<br />刚开始的时候设置了我的myslq如下：<br /><img src="http://www.cppblog.com/images/cppblog_com/sunrise/1.png" width="340" height="200" alt="" /><br />所以一直以为是我的程序问题，然后一直改啊改啊，最后才发现mysql的数据库建的字符集还是没有修改然后执行如下操作就OK了：<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 />-->mysql<span style="color: #808080; ">&gt;</span>&nbsp;<span style="color: #0000FF; ">use</span>&nbsp;designer<br /><span style="color: #0000FF; ">Database</span>&nbsp;changed<br />mysql<span style="color: #808080; ">&gt;</span>&nbsp;<span style="color: #0000FF; ">alter</span>&nbsp;<span style="color: #0000FF; ">database</span>&nbsp;designer&nbsp;<span style="font-weight: bold;">character</span>&nbsp;<span style="color: #0000FF; ">set</span>&nbsp;utf<span style="color: #808080; ">-</span><span style="color: #800000; font-weight: bold; ">8</span>;<br />ERROR&nbsp;<span style="color: #800000; font-weight: bold; ">1115</span>&nbsp;(<span style="color: #800000; font-weight: bold; ">42000</span>):&nbsp;Unknown&nbsp;<span style="font-weight: bold;">character</span>&nbsp;<span style="color: #0000FF; ">set</span>:&nbsp;<span style="color: #FF0000; ">'</span><span style="color: #FF0000; ">utf</span><span style="color: #FF0000; ">'</span><br />mysql<span style="color: #808080; ">&gt;</span>&nbsp;<span style="color: #0000FF; ">alter</span>&nbsp;<span style="color: #0000FF; ">database</span>&nbsp;yourdatabase&nbsp;<span style="font-weight: bold;">character</span>&nbsp;<span style="color: #0000FF; ">set</span>&nbsp;utf8;</div><img src ="http://www.cppblog.com/sunrise/aggbug/199476.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-04-16 11:34 <a href="http://www.cppblog.com/sunrise/archive/2013/04/16/199476.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>jQuery插件QuickPaginate快速分页实现</title><link>http://www.cppblog.com/sunrise/archive/2013/03/14/198418.html</link><dc:creator>SunRise_at</dc:creator><author>SunRise_at</author><pubDate>Thu, 14 Mar 2013 06:54:00 GMT</pubDate><guid>http://www.cppblog.com/sunrise/archive/2013/03/14/198418.html</guid><wfw:comment>http://www.cppblog.com/sunrise/comments/198418.html</wfw:comment><comments>http://www.cppblog.com/sunrise/archive/2013/03/14/198418.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunrise/comments/commentRss/198418.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunrise/services/trackbacks/198418.html</trackback:ping><description><![CDATA[<p style="margin: 0px; padding: 0px; color: #454545; font-family: tahoma, helvetica, arial; background-color: #ffffff;">介绍一个分页插件，那么什么叫QuickPaginate快速分页，这个插件实现就是将需要显示的内容用分页的方法显示，但不是真正意义上的分页， 因为QuickPaginate快速分页不需要与服务器端交互，所有的内容已经全部输送到客户端，每次显示部分信息，用分页的形式进行浏览，效果图如下：<br /><br /><strong>使用说明</strong><br />一，需要<a href="http://designcss.org/www.jquery.com" target="_blank" style="color: #1e375a;">jQuery库文件</a>和<a href="http://plugins.jquery.com/project/quick_paginate" target="_blank" style="color: #1e375a;">QuickPaginate库文件</a><br />二，可自定义分页CSS显示效果，如：qp_prev和qp_next</p><p style="margin: 0px; padding: 0px; color: #454545; font-family: tahoma, helvetica, arial; background-color: #ffffff;"><strong>使用实例</strong><br />一，包含文件部分</p><ol style="margin: 0px; padding: 0px; list-style: none; color: #454545; font-family: tahoma, helvetica, arial; background-color: #ffffff;"><li style="margin: 0px; padding: 0px;">&lt;script src="jquery.js" type="text/javascript" &gt;&lt;/script&gt;</li><li style="margin: 0px; padding: 0px;">&lt;script src="jquery.quickpaginate.js" type="text/javascript" &gt;&lt;/script&gt;</li></ol><p style="margin: 0px; padding: 0px; color: #454545; font-family: tahoma, helvetica, arial; background-color: #ffffff;">二，HTML部分<br />(1)jQuery插件QuickPaginate实现<strong>文本</strong>分页功能</p><ol style="margin: 0px; padding: 0px; list-style: none; color: #454545; font-family: tahoma, helvetica, arial; background-color: #ffffff;"><li style="margin: 0px; padding: 0px;">&lt;ul id="<strong>biuuu_city_list</strong>"&gt;</li><li style="margin: 0px; padding: 0px;">&lt;li&gt;北京&lt;/li&gt;</li><li style="margin: 0px; padding: 0px;">&lt;li&gt;上海&lt;/li&gt;</li><li style="margin: 0px; padding: 0px;">&lt;li&gt;广州&lt;/li&gt;</li><li style="margin: 0px; padding: 0px;">&lt;li&gt;杭州&lt;/li&gt;</li><li style="margin: 0px; padding: 0px;">&lt;li&gt;长沙&lt;/li&gt;</li><li style="margin: 0px; padding: 0px;">&lt;li&gt;合肥&lt;/li&gt;</li><li style="margin: 0px; padding: 0px;">&lt;li&gt;宁夏&lt;/li&gt;</li><li style="margin: 0px; padding: 0px;">&lt;li&gt;成都&lt;/li&gt;</li><li style="margin: 0px; padding: 0px;">&lt;li&gt;西安&lt;/li&gt;</li><li style="margin: 0px; padding: 0px;">&lt;li&gt;南昌&lt;/li&gt;</li><li style="margin: 0px; padding: 0px;">&lt;/ul&gt;</li><li style="margin: 0px; padding: 0px;">&lt;div id="<strong>biuuu_city</strong>"&gt;&lt;/div&gt;</li></ol><p style="margin: 0px; padding: 0px; color: #454545; font-family: tahoma, helvetica, arial; background-color: #ffffff;">其中ID为biuuu_city_list的DIV是要实现分页的内容，ID为biuuu_city是显示分页内容，具体看效果图</p><p style="margin: 0px; padding: 0px; color: #454545; font-family: tahoma, helvetica, arial; background-color: #ffffff;">(2)jQuery插件QuickPaginate实现<strong>图片</strong>分页功能</p><ol style="margin: 0px; padding: 0px; list-style: none; color: #454545; font-family: tahoma, helvetica, arial; background-color: #ffffff;"><li style="margin: 0px; padding: 0px;">&lt;div id="<strong>images</strong>"&gt;</li><li style="margin: 0px; padding: 0px;">&lt;img src="demo_images/demo1.jpg" width="200" height="133" alt="biuuu1" /&gt;</li><li style="margin: 0px; padding: 0px;">&lt;img src="demo_images/demo2.jpg" width="200" height="133" alt="biuuu2" /&gt;</li><li style="margin: 0px; padding: 0px;">&lt;img src="demo_images/demo3.jpg" width="200" height="133" alt="biuuu3" /&gt;</li><li style="margin: 0px; padding: 0px;">&lt;/div&gt;</li><li style="margin: 0px; padding: 0px;">&lt;div id="<strong>images_counter</strong>"&gt;&lt;/div&gt;</li></ol><p style="margin: 0px; padding: 0px; color: #454545; font-family: tahoma, helvetica, arial; background-color: #ffffff;">三，javascript部分(调用jQuery插件QuickPaginate)</p><ol style="margin: 0px; padding: 0px; list-style: none; color: #454545; font-family: tahoma, helvetica, arial; background-color: #ffffff;"><li style="margin: 0px; padding: 0px;">&lt;script type="text/javascript"&gt;</li><li style="margin: 0px; padding: 0px;">$(function(){</li><li style="margin: 0px; padding: 0px;">$("#<strong>biuuu_city_list li</strong>").<strong>quickpaginate</strong>({&nbsp;<strong>perpage</strong>: 4,&nbsp;<strong>pager&nbsp;</strong>: $("#<strong>biuuu_city</strong>") });</li><li style="margin: 0px; padding: 0px;">});</li><li style="margin: 0px; padding: 0px;">$(function(){</li><li style="margin: 0px; padding: 0px;">$("<strong>#images img</strong>").<strong>quickpaginate</strong>({&nbsp;<strong>perpage</strong>: 1,&nbsp;<strong>showcounter</strong>: false,&nbsp;<strong>pager&nbsp;</strong>: $("<strong>#images_counter</strong>") });</li><li style="margin: 0px; padding: 0px;">});</li><li style="margin: 0px; padding: 0px;">&lt;/script&gt;</li></ol><p style="margin: 0px; padding: 0px; color: #454545; font-family: tahoma, helvetica, arial; background-color: #ffffff;"><strong>jQuery插件QuickPaginate参数详解</strong><br />perpage表示每页显示数，默认值为6，如上文本实例每页显示4个城市，图片实例每页显示1张图片<br />pager表示QuickPaginate生成分页HTML代码显示的对象，默认为null，如上：$("#biuuu_city")和$("#images_counter")<br />showcounter表示是否显示统计，默认为true<br />prev表示上一页CSS样式名，默认为qp_next，具体请看QuickPaginate生成的代码图<br />next表示下一页CSS样式名，默认为qp_prev<br />pagenumber表示当前页CSS样式名，默认为qp_pagenumber<br />totalnumber表示总页数CSS样式名，默认为qp_totalnumber<br />counter表示统计CSS样式名，默认为qp_counter</p><p style="margin: 0px; padding: 0px; color: #454545; font-family: tahoma, helvetica, arial; background-color: #ffffff;">注意事项<br />注意默认的QuickPaginate是UTF8编码，同时需要修改部分QuickPaginate的JS库代码，如将prev修改为中文上一页，next修改为中文下一页，其实就是分页样式，上面的参数中大部分都是定义某个部分的样式，因为样式也是非常重要的。</p><p style="margin: 0px; padding: 0px; color: #454545; font-family: tahoma, helvetica, arial; background-color: #ffffff;">以上实例可知使用jQuery插件QuickPaginate快速分页非常简单，只需要指定内容，指定每页显示数，显示样式，即可实现快速分页功能，在实际应用中，可对菜单、图片等进行分页，非常具有实用性，值得推荐。</p><p style="margin: 0px; padding: 0px; color: #454545; font-family: tahoma, helvetica, arial; background-color: #ffffff;"><a href="http://projects.allmarkedup.com/jquery_quick_paginate/" target="_blank" style="color: #1e375a;">点我下载jQuery插件QuickPaginate快速分页程序</a><br /><a href="http://projects.allmarkedup.com/jquery_quick_paginate/demo.html" target="_blank" style="color: #1e375a;">点我查看jQuery插件QuickPaginate快速分面演示</a></p><img src ="http://www.cppblog.com/sunrise/aggbug/198418.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-14 14:54 <a href="http://www.cppblog.com/sunrise/archive/2013/03/14/198418.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><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>UnicodeDecodeError: 'utf8' codec can't decode bytes in position 18-19: unexpected end of data</title><link>http://www.cppblog.com/sunrise/archive/2013/02/21/197977.html</link><dc:creator>SunRise_at</dc:creator><author>SunRise_at</author><pubDate>Thu, 21 Feb 2013 08:39:00 GMT</pubDate><guid>http://www.cppblog.com/sunrise/archive/2013/02/21/197977.html</guid><wfw:comment>http://www.cppblog.com/sunrise/comments/197977.html</wfw:comment><comments>http://www.cppblog.com/sunrise/archive/2013/02/21/197977.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/sunrise/comments/commentRss/197977.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunrise/services/trackbacks/197977.html</trackback:ping><description><![CDATA[<div>本来是为了省事处理数据的时候批量插入，结果插入的时候一直不能decode。<br />python代码：
<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">a</span><span style="color: #000000">=</span><span style="color: #000000">open(r</span><span style="color: #800000">"</span><span style="color: #800000">testdata.txt</span><span style="color: #800000">"</span><span style="color: #000000">).read(</span><span style="color: #000000">20</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(a)<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;</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: #0000ff">print</span><span style="color: #000000">&nbsp;list(a.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;</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></div>testdata数据：
<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">0 </span><span style="color: #000000">1</span><span style="color: #000000"> test 这件衣服设计的正好看，正是我想要的 </span><span style="color: #000000">2013</span><span style="color: #000000">/</span><span style="color: #000000">2</span><span style="color: #000000">/</span><span style="color: #000000">23</span><span style="color: #000000"> zh <br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />0 </span><span style="color: #000000">2</span><span style="color: #000000"> happys 这件衣服设计的正好看，正是我想要的 </span><span style="color: #000000">2013</span><span style="color: #000000">/</span><span style="color: #000000">11</span><span style="color: #000000">/</span><span style="color: #000000">21</span><span style="color: #000000"> zh </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: #800000">'</span><span style="color: #800000">\xef</span><span style="color: #800000">'</span><span style="color: #000000">, </span><span style="color: #800000">'</span><span style="color: #800000">\xbb</span><span style="color: #800000">'</span><span style="color: #000000">, </span><span style="color: #800000">'</span><span style="color: #800000">\xbf</span><span style="color: #800000">'</span><span style="color: #000000">, </span><span style="color: #800000">'</span><span style="color: #800000">0</span><span style="color: #800000">'</span><span style="color: #000000">, </span><span style="color: #800000">'</span><span style="color: #800000">\t</span><span style="color: #800000">'</span><span style="color: #000000">, </span><span style="color: #800000">'</span><span style="color: #800000">1</span><span style="color: #800000">'</span><span style="color: #000000">, </span><span style="color: #800000">'</span><span style="color: #800000">\t</span><span style="color: #800000">'</span><span style="color: #000000">, </span><span style="color: #800000">'</span><span style="color: #800000">t</span><span style="color: #800000">'</span><span style="color: #000000">, </span><span style="color: #800000">'</span><span style="color: #800000">e</span><span style="color: #800000">'</span><span style="color: #000000">, </span><span style="color: #800000">'</span><span style="color: #800000">s</span><span style="color: #800000">'</span><span style="color: #000000">, </span><span style="color: #800000">'</span><span style="color: #800000">t</span><span style="color: #800000">'</span><span style="color: #000000">, </span><span style="color: #800000">'</span><span style="color: #800000">\t</span><span style="color: #800000">'</span><span style="color: #000000">, </span><span style="color: #800000">'</span><span style="color: #800000">\xe8</span><span style="color: #800000">'</span><span style="color: #000000">, </span><span style="color: #800000">'</span><span style="color: #800000">\xbf</span><span style="color: #800000">'</span><span style="color: #000000">, </span><span style="color: #800000">'</span><span style="color: #800000">\x99</span><span style="color: #800000">'</span><span style="color: #000000">, </span><span style="color: #800000">'</span><span style="color: #800000">\xe4</span><span style="color: #800000">'</span><span style="color: #000000">, </span><span style="color: #800000">'</span><span style="color: #800000">\xbb</span><span style="color: #800000">'</span><span style="color: #000000">, </span><span style="color: #800000">'</span><span style="color: #800000">\xb6</span><span style="color: #800000">'</span><span style="color: #000000">, </span><span style="color: #800000">'</span><span style="color: #800000">\xe8</span><span style="color: #800000">'</span><span style="color: #000000">, </span><span style="color: #800000">'</span><span style="color: #800000">\xa1</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: #000000">==========================================================================================</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="" />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">E:/designerpage/test.py</span><span style="color: #800000">"</span><span style="color: #000000">, line </span><span style="color: #000000">4</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="" /> </span><span style="color: #0000ff">print</span><span style="color: #000000"> list(a.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="" /> File </span><span style="color: #800000">"</span><span style="color: #800000">D:\Python27\lib\encodings\utf_8.py</span><span style="color: #800000">"</span><span style="color: #000000">, line </span><span style="color: #000000">16</span><span style="color: #000000">, </span><span style="color: #0000ff">in</span><span style="color: #000000"> decode<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"> codecs.utf_8_decode(input, errors, True)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />UnicodeDecodeError: </span><span style="color: #800000">'</span><span style="color: #800000">utf8</span><span style="color: #800000">'</span><span style="color: #000000"> codec can</span><span style="color: #800000">'</span><span style="color: #800000">t decode bytes in position 18-19: unexpected end of data</span></div>出现错误的原因是因为utf8格式一般是'\xef', '\xbb', '\xbf'三个一起出现，然后解析的，而在某处不能够成三个一组的时候就会出现这样的错误。如果将20改为18则可以正常执行。<br />当然如果是在python2.6里是可以运行的，python2.7就会出现如上错误。无比坑爹的bug,查了好久。<br /></div><img src ="http://www.cppblog.com/sunrise/aggbug/197977.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-02-21 16:39 <a href="http://www.cppblog.com/sunrise/archive/2013/02/21/197977.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>access violation at address　解决之法</title><link>http://www.cppblog.com/sunrise/archive/2013/02/21/197973.html</link><dc:creator>SunRise_at</dc:creator><author>SunRise_at</author><pubDate>Thu, 21 Feb 2013 03:04:00 GMT</pubDate><guid>http://www.cppblog.com/sunrise/archive/2013/02/21/197973.html</guid><wfw:comment>http://www.cppblog.com/sunrise/comments/197973.html</wfw:comment><comments>http://www.cppblog.com/sunrise/archive/2013/02/21/197973.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunrise/comments/commentRss/197973.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunrise/services/trackbacks/197973.html</trackback:ping><description><![CDATA[<p style="line-height: 19px; margin-top: 10px; margin-bottom: 10px; color: #4b4b4b; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; background-color: #ffffff;">右击&#8220;我的电脑&#8221;。单击&#8220;属性&#8221;。</p><p style="line-height: 19px; margin-top: 10px; margin-bottom: 10px; color: #4b4b4b; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; background-color: #ffffff;">在&#8220;系统属性&#8221;中单击&#8220;高级&#8221;。</p><p style="line-height: 19px; margin-top: 10px; margin-bottom: 10px; color: #4b4b4b; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; background-color: #ffffff;">在&#8220;性能&#8221;中单击&#8220;设置&#8221;。</p><p style="line-height: 19px; margin-top: 10px; margin-bottom: 10px; color: #4b4b4b; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; background-color: #ffffff;">在&#8220;性能选项&#8221;中单击&#8220;数据执行保护&#8221;。</p><p style="line-height: 19px; margin-top: 10px; margin-bottom: 10px; color: #4b4b4b; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; background-color: #ffffff;">单击&#8220;添加&#8221;。选择要运行的程序。</p><img src ="http://www.cppblog.com/sunrise/aggbug/197973.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-02-21 11:04 <a href="http://www.cppblog.com/sunrise/archive/2013/02/21/197973.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>解决UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3问题</title><link>http://www.cppblog.com/sunrise/archive/2013/01/17/197338.html</link><dc:creator>SunRise_at</dc:creator><author>SunRise_at</author><pubDate>Thu, 17 Jan 2013 05:45:00 GMT</pubDate><guid>http://www.cppblog.com/sunrise/archive/2013/01/17/197338.html</guid><wfw:comment>http://www.cppblog.com/sunrise/comments/197338.html</wfw:comment><comments>http://www.cppblog.com/sunrise/archive/2013/01/17/197338.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sunrise/comments/commentRss/197338.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sunrise/services/trackbacks/197338.html</trackback:ping><description><![CDATA[<p>在文件的前两行加入：</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">import</span><span style="color: #000000">&nbsp;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">)</span></div>
<p><br />&nbsp;</p><img src ="http://www.cppblog.com/sunrise/aggbug/197338.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 13:45 <a href="http://www.cppblog.com/sunrise/archive/2013/01/17/197338.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>