﻿<?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>IT博客-yunshichen-随笔分类-Python2.5/2.6</title><link>http://www.cnitblog.com/yunshichen/category/7212.html</link><description>我相信人生是值得活的,尽管人在一生中必须遭受痛苦,卑劣,残酷,不幸和死亡的折磨,我依然深信如此.但我认为人生不一定要有意义,只是对一些人而言,他们可以使人生有意义.  ---J 赫胥黎</description><language>zh-cn</language><lastBuildDate>Wed, 23 Dec 2015 00:36:07 GMT</lastBuildDate><pubDate>Wed, 23 Dec 2015 00:36:07 GMT</pubDate><ttl>60</ttl><item><title>Python 应用X: 工具集合</title><link>http://www.cnitblog.com/yunshichen/archive/2011/04/28/73428.html</link><dc:creator>Chenyunshi</dc:creator><author>Chenyunshi</author><pubDate>Wed, 27 Apr 2011 18:08:00 GMT</pubDate><guid>http://www.cnitblog.com/yunshichen/archive/2011/04/28/73428.html</guid><wfw:comment>http://www.cnitblog.com/yunshichen/comments/73428.html</wfw:comment><comments>http://www.cnitblog.com/yunshichen/archive/2011/04/28/73428.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.cnitblog.com/yunshichen/comments/commentRss/73428.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/yunshichen/services/trackbacks/73428.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 简介						记录自己做的一些稀奇古怪的程序. 工具一: 查找单词解释原理: http://dict.cn/ 提供了查词的引擎, 输入单词即可以得到解释. 所以这个程序只是做一些体力活. 代码如下: (有朋友反映复制之后出错. 若如此请移步到这里下载这个脚本: http://www.uudisc.com/user/diegoyun/file/4131948Code highlig...&nbsp;&nbsp;<a href='http://www.cnitblog.com/yunshichen/archive/2011/04/28/73428.html'>阅读全文</a><img src ="http://www.cnitblog.com/yunshichen/aggbug/73428.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/yunshichen/" target="_blank">Chenyunshi</a> 2011-04-28 02:08 <a href="http://www.cnitblog.com/yunshichen/archive/2011/04/28/73428.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Subprocess 操作, 如 Linux 下的shell 和 Windows 下的批处理</title><link>http://www.cnitblog.com/yunshichen/archive/2011/04/28/73427.html</link><dc:creator>Chenyunshi</dc:creator><author>Chenyunshi</author><pubDate>Wed, 27 Apr 2011 16:24:00 GMT</pubDate><guid>http://www.cnitblog.com/yunshichen/archive/2011/04/28/73427.html</guid><wfw:comment>http://www.cnitblog.com/yunshichen/comments/73427.html</wfw:comment><comments>http://www.cnitblog.com/yunshichen/archive/2011/04/28/73427.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/yunshichen/comments/commentRss/73427.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/yunshichen/services/trackbacks/73427.html</trackback:ping><description><![CDATA[
		<br />
		<h2>
				<font color="#006600" size="5">
						<b>基本命令</b>
				</font>
				<br />
		</h2>东拼西凑查了好久, 才总算把这个命令补完全. 不废话, 直接上代码. <br /><br /><div style="background-color: rgb(238, 238, 238); font-size: 13px; border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> exec_command(cmdlist,working_dir,view_result</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">False):<br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> isinstance(cmdlist, str):<br />        cmdlist </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> [cmdlist]<br />        <br />    cmd_seperator </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> platform<br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> platform.system() </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">Windows</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">:<br />        cmd_seperator </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">&amp;&amp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />    <br />    runCmd</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">""</span><span style="color: rgb(0, 0, 0);">    <br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> cmd </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> cmdlist:<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> runCmd</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(128, 0, 0);">""</span><span style="color: rgb(0, 0, 0);">:<br />            runCmd </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> runCmd </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> cmd<br />        </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">:<br />            runCmd </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> runCmd </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> cmd_seperator </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> cmd<br />    <br />    p</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">subprocess.Popen(runCmd,shell</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">True,stdout</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">subprocess.PIPE,stdin</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">subprocess.PIPE,stderr</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">subprocess.PIPE,cwd</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">working_dir)<br />    out,err </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> p.communicate()<br />    result </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> {</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">out</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">:out,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">err</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">:err,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">returncode</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">:str(p.returncode),</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">pid</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">:str(p.pid)}<br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> view_result:<br />        </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">===== Printed result: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">--&gt; Run cmd: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> runCmd<br />        </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">--&gt; returncode: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> result[</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">returncode</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">]<br />        </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">--&gt; out: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> result[</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">out</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">]<br />        </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">--&gt; err: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> result[</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">err</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">]<br />        <br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> result<br /><br />exec_command(["ls -l","uname -a"],".",True)<br /></span></div><br /><hr size="2" width="100%" /><h2><font color="#006600">扩展功能1: CVS 操作</font><br /></h2>这个功能无甚神秘. 首先要安装有cvs, 其次要设置cvsroot. 最后, 你要时不时查查cvs的命令. <br /><br />我承认拼凑这个程序还是费了不少时间, 所以有记下来的必要. <br /><br /><div style="background-color: rgb(238, 238, 238); font-size: 13px; border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">!/usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br />#</span><span style="color: rgb(0, 128, 0);"> -*- coding: utf-8 -*- </span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(128, 0, 0);"><br />Created on Apr 28, 2011<br /><br />@author: yunshichen@gmail.com<br /></span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> string<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> subprocess<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> platform<br /><br /></span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> exec_command(cmdlist,working_dir,view_result</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">False):<br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> isinstance(cmdlist, str):<br />        cmdlist </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> [cmdlist]<br />        <br />    cmd_seperator </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> platform.system() </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">Windows</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">:<br />        cmd_seperator </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">&amp;&amp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />    <br />    runCmd</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">""</span><span style="color: rgb(0, 0, 0);">    <br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> cmd </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> cmdlist:<br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> runCmd</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(128, 0, 0);">""</span><span style="color: rgb(0, 0, 0);">:<br />            runCmd </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> runCmd </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> cmd<br />        </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">:<br />            runCmd </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> runCmd </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> cmd_seperator </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> cmd<br />    <br />    p</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">subprocess.Popen(runCmd,shell</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">True,stdout</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">subprocess.PIPE,stdin</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">subprocess.PIPE,stderr</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">subprocess.PIPE,cwd</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">working_dir)<br />    out,err </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> p.communicate()<br />    result </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> {</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">out</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">:out,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">err</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">:err,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">returncode</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">:str(p.returncode),</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">pid</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">:str(p.pid)}<br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> view_result:<br />        </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">===== Printed result: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">--&gt; Run cmd: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> runCmd<br />        </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">--&gt; returncode: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> result[</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">returncode</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">]<br />        </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">--&gt; out: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> result[</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">out</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">]<br />        </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">--&gt; err: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> result[</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">err</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">]<br />        <br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> result<br /><br /></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> DPY_CVS():<br />    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">__init__</span><span style="color: rgb(0, 0, 0);">(self,cvs_cmd,cvs_root,is_debug</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">True):<br />        self.cvs_cmd </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> cvs_cmd<br />        self.cvs_root </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> cvs_root<br />    <br />    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> checkout(self,working_dir,proj,opts</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">None):<br />        </span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(128, 0, 0);"><br />        opts holds optional for checkout command. For example:<br />        <br />        {'r': diego_test_tag,'d':my_local_path}<br />        </span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(0, 0, 0);"><br />        cmd_set_cvs_root </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">set CVSROOT=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> self.cvs_root<br />        cmd_checkout </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> self.cvs_cmd </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);"> -q checkout ${optional_str} -P </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">proj<br />        <br />        str</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">""</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(opts </span><span style="color: rgb(0, 0, 0);">&lt;&gt;</span><span style="color: rgb(0, 0, 0);"> None):<br />            </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> key </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> opts:<br />                str </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> str </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);"> -</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">key </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> opts[key]<br />        <br />        cmd_checkout </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> string.Template(cmd_checkout).substitute({</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">optional_str</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">:str})<br />        <br />        self.result </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> exec_command([cmd_set_cvs_root,cmd_checkout],working_dir)<br />        <br />    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> tagFiles(self,working_dir,tagName,pathlist):<br />        <br />        cmd_set_cvs_root </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">set CVSROOT=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> self.cvs_root<br />        cmd_tag_a_file </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> self.cvs_cmd </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);"> tag </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">tagName</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);"> ${cvs_file}</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br />        <br />        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> isinstance(pathlist, str):<br />            pathlist </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> [pathlist]<br />            <br />        cmdlist </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> [cmd_set_cvs_root]<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> fpath </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> pathlist:<br />            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> fpath.startswith(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">/</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">):<br />                fpath </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> fpath[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">:]<br />            cmd_tag </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> string.Template(cmd_tag_a_file).substitute({</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">cvs_file</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">:fpath})<br />            cmdlist.append(cmd_tag)<br />        <br />        self.result </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> exec_command(cmdlist,working_dir)<br />        <br />    </span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> viewResult(self):<br />        r </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> self.result<br />        </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">\n\n\n------------------ CVS run result: -----------</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">---&gt;returncode: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> r[</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">returncode</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">]<br />        </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">out:\n</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> r[</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">out</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">]<br />        </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">err:\n</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> r[</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">err</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">]<br />        </span></div><br /><br /><br /><br /><br /><img src ="http://www.cnitblog.com/yunshichen/aggbug/73427.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/yunshichen/" target="_blank">Chenyunshi</a> 2011-04-28 00:24 <a href="http://www.cnitblog.com/yunshichen/archive/2011/04/28/73427.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Python 数据库操作</title><link>http://www.cnitblog.com/yunshichen/archive/2011/02/14/72790.html</link><dc:creator>Chenyunshi</dc:creator><author>Chenyunshi</author><pubDate>Sun, 13 Feb 2011 16:02:00 GMT</pubDate><guid>http://www.cnitblog.com/yunshichen/archive/2011/02/14/72790.html</guid><wfw:comment>http://www.cnitblog.com/yunshichen/comments/72790.html</wfw:comment><comments>http://www.cnitblog.com/yunshichen/archive/2011/02/14/72790.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/yunshichen/comments/commentRss/72790.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/yunshichen/services/trackbacks/72790.html</trackback:ping><description><![CDATA[
		<br />打算把python连接db的例子都总结下来. <br /><br /><font color="#006600"><b><font size="5">python连接sqllite</font></b></font><br /><br /><div style="background-color: rgb(238, 238, 238); font-size: 13px; border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">!/usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br />#</span><span style="color: rgb(0, 128, 0);"> -*- coding: utf-8 -*- </span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(128, 0, 0);"><br />Created on 2/13/2011<br /><br />@author: yunshichen@gmail.com<br /></span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(128, 0, 0);"><br /><br />tutorial link: http://docs.python.org/py3k/library/sqlite3.html<br /><br />SQLite is a C library that provides a lightweight disk-based database that doesn’t require a separate server process and allows accessing the database using a nonstandard variant of the SQL query language. Some applications can use SQLite for internal data storage. It’s also possible to prototype an application using SQLite and then port the code to a larger database such as PostgreSQL or Oracle.<br /><br />sqlite3 was written by Gerhard Häring and provides a SQL interface compliant with the DB-API 2.0 specification described by PEP 249.<br /><br />To use the module, you must first create a Connection object that represents the database. Here the data will be stored in the /tmp/example file:<br /><br /></span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> sqlite3<br /><br />DB_FILE </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">/tmp/example</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> listData():<br />    conn </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> sqlite3.connect(DB_FILE)<br />    c </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> conn.cursor()<br />    <br />    </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">Lists data<img src="http://www.cnitblog.com/images/dot.gif" />.\n</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />    sql </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">select * from stocks order by price</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />    c.execute(sql)<br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> row </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> c:<br />        </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);">(row)<br />        <br />    conn.commit()<br />    c.close()<br />    <br /><br /></span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> clearTableData():<br />    conn </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> sqlite3.connect(DB_FILE)<br />    c </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> conn.cursor()<br />    <br />    sql </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">delete from stocks</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />    c.execute(sql)<br />    <br />    </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">All data are deleted<img src="http://www.cnitblog.com/images/dot.gif" />.\n</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />    conn.commit()<br />    c.close()<br /><br /></span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> insertData():<br />    conn </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> sqlite3.connect(DB_FILE)<br />    c </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> conn.cursor()<br />    <br />    insertData </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">insert into stocks values ('2006-01-05','BUY','RHAT',100,35.14)</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />    c.execute(insertData)<br />    <br />    </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> Never do this -- insecure!</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">    symbol = 'IBM'</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">    c.execute("<img src="http://www.cnitblog.com/images/dot.gif" /> where symbol = '%s'" % symbol)</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> Do this instead</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">    t = (symbol,)</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">    c.execute('select * from stocks where symbol=?', t)</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);"><br />    insertSql </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">insert into stocks values (?,?,?,?,?)</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> t </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> [(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">2006-03-28</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">BUY</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">IBM</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">1000</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">45.00</span><span style="color: rgb(0, 0, 0);">),<br />              (</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">2006-04-05</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">BUY</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">MSOFT</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">1000</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">72.00</span><span style="color: rgb(0, 0, 0);">),<br />              (</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">2006-04-06</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">SELL</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">IBM</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">500</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">53.00</span><span style="color: rgb(0, 0, 0);">),<br />             ]:<br />        c.execute(insertSql, t)<br /><br />    </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">Set data into table stocks<img src="http://www.cnitblog.com/images/dot.gif" />.\n</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />    conn.commit()<br />    c.close()    <br /><br /></span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> initTable():<br />    conn </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> sqlite3.connect(DB_FILE)<br />    c </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> conn.cursor()<br />    <br />    createTable </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">create table stocks (date text, trans text, symbol text, qty real, price real)</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />    c.execute(createTable)<br />    </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">Created table stocks<img src="http://www.cnitblog.com/images/dot.gif" />.\n</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />    <br />    conn.commit()<br />    c.close()<br />    <br /><br /></span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(128, 0, 0);"><br />    运行例子:   ./sqllitetest.py -c init<br /></span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">__name__</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">__main__</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">:<br />    <br />    </span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> optparse </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> OptionParser<br />    parser </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> OptionParser()<br />    <br />    parser.add_option(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">-c</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">--command</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, action</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">store</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,<br />                  dest</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">cmd</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,<br />                  default</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">init</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,<br />                  type</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">string</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,<br />                  help</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">Runs functions based on commands. Available commands are init,list,data,deleteAll </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">) <br />    <br />    options, args </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> parser.parse_args()<br />    <br />    cmd </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> options.cmd<br />    <br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(cmd</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">init</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">):<br />        initTable()<br />        <br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(cmd</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">deleteAll</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">):<br />        clearTableData()<br />        <br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(cmd</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">data</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">):<br />        insertData()<br />        <br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(cmd</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">list</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">):<br />        listData()<br />        <br />    <br />    <br />    <br />    <br />    <br />    </span></div><br /><img src ="http://www.cnitblog.com/yunshichen/aggbug/72790.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/yunshichen/" target="_blank">Chenyunshi</a> 2011-02-14 00:02 <a href="http://www.cnitblog.com/yunshichen/archive/2011/02/14/72790.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>python 小程序, 查找todo选项, 促进自己更好的管理时间</title><link>http://www.cnitblog.com/yunshichen/archive/2011/02/06/72760.html</link><dc:creator>Chenyunshi</dc:creator><author>Chenyunshi</author><pubDate>Sun, 06 Feb 2011 13:42:00 GMT</pubDate><guid>http://www.cnitblog.com/yunshichen/archive/2011/02/06/72760.html</guid><wfw:comment>http://www.cnitblog.com/yunshichen/comments/72760.html</wfw:comment><comments>http://www.cnitblog.com/yunshichen/archive/2011/02/06/72760.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cnitblog.com/yunshichen/comments/commentRss/72760.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/yunshichen/services/trackbacks/72760.html</trackback:ping><description><![CDATA[
		<b>想法&amp;需求</b>
		<br />
		<br />1&gt;在看代码的时候, 总会想: 这个api很好, 这段代码用法很好. 总结下来. 但当看完书的时候就忘了.<br />2&gt;有时候会无聊, 无所事事. 浪费了一段时间后才想起, 其实可以做XX, YY. <br /><br />解决办法<br /><br />1&gt;在看代码的时候用注释方式做标记, 记录下来. <br />2&gt;用小程序遍历这些代码, 找出这些标记. <br />3&gt;将这些项记录到excel里. 无聊的时候就打开这个excel看看有什么东西做. <br /><br />todo的例子(用一些ide可以方便生成大部分注释)<br /><br />------------------------------<div id=":5q"><wbr>-------------------------- todo sample begin<br />
#todo_begin:<br />
#item: 配置一个方便的调试 spring3 + hibernate 3 的教程<br />
#estimate: 4-8h<br />
#file: /media/dev/utopism2008/<wbr>projects/python/dpytools/src/<wbr>findtodo/FindToDo.py<br />
#todo_end:<br /><br />
#todo_begin:<br />
#item: 总结xlrt 和 xlwt 用法. 特别是 nrows 和 写已有的excel<br />
#estimate: 4h<br />
#file: /media/dev/utopism2008/<wbr>projects/python/dpytools/src/<wbr>findtodo/FindToDo.py<br />
#todo_end:<br />
------------------------------<wbr>-------------------------- todo sample end<br /><br />程序源码:<br /><br /><div style="background-color: rgb(238, 238, 238); font-size: 13px; border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);">  1</span> <span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">! /usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">  2</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(128, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">  3</span> <span style="color: rgb(128, 0, 0);">Created on Feb 6, 2011<br /></span><span style="color: rgb(0, 128, 128);">  4</span> <span style="color: rgb(128, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">  5</span> <span style="color: rgb(128, 0, 0);">1&gt;Find todo string in every file read. <br /></span><span style="color: rgb(0, 128, 128);">  6</span> <span style="color: rgb(128, 0, 0);">2&gt;Set this content into excel file. Every weekend I can check this file and make next plan based on these items. <br /></span><span style="color: rgb(0, 128, 128);">  7</span> <span style="color: rgb(128, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">  8</span> <span style="color: rgb(128, 0, 0);">@author: yunshichen@gamil.com<br /></span><span style="color: rgb(0, 128, 128);">  9</span> <span style="color: rgb(128, 0, 0);"></span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 10</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> -*- coding: utf8 -*- </span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 11</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 12</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> os<br /></span><span style="color: rgb(0, 128, 128);"> 13</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> ConfigParser<br /></span><span style="color: rgb(0, 128, 128);"> 14</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 15</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> xlrd <br /></span><span style="color: rgb(0, 128, 128);"> 16</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> xlutils.copy<br /></span><span style="color: rgb(0, 128, 128);"> 17</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> xlwt<br /></span><span style="color: rgb(0, 128, 128);"> 18</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 19</span> <span style="color: rgb(0, 0, 0);">ITEM_KEY </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">#item:</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 20</span> <span style="color: rgb(0, 0, 0);">EST_KEY </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">#estimate:</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 21</span> <span style="color: rgb(0, 0, 0);">FILE_PATH_KEY </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">#file:</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 22</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 23</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(128, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 24</span> <span style="color: rgb(128, 0, 0);">A typical sample of todo item in file:<br /></span><span style="color: rgb(0, 128, 128);"> 25</span> <span style="color: rgb(128, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 26</span> <span style="color: rgb(128, 0, 0);">#todo_begin:<br /></span><span style="color: rgb(0, 128, 128);"> 27</span> <span style="color: rgb(128, 0, 0);">#item: To summarize usage of Qt4 widget<br /></span><span style="color: rgb(0, 128, 128);"> 28</span> <span style="color: rgb(128, 0, 0);">#estimate: 5m<br /></span><span style="color: rgb(0, 128, 128);"> 29</span> <span style="color: rgb(128, 0, 0);">#file: /media/dev/utopism2008/projects/python/dpytools/src/findtodo/FindToDo.py<br /></span><span style="color: rgb(0, 128, 128);"> 30</span> <span style="color: rgb(128, 0, 0);">#todo_end:<br /></span><span style="color: rgb(0, 128, 128);"> 31</span> <span style="color: rgb(128, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 32</span> <span style="color: rgb(128, 0, 0);"></span><span style="color: rgb(128, 0, 0);">'''</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 33</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> findTodo(fpath,alist):<br /></span><span style="color: rgb(0, 128, 128);"> 34</span> <span style="color: rgb(0, 0, 0);">    todo_begin </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">todo_begin</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 35</span> <span style="color: rgb(0, 0, 0);">    todo_end </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">todo_end</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 36</span> <span style="color: rgb(0, 0, 0);">    isFinding </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> False<br /></span><span style="color: rgb(0, 128, 128);"> 37</span> <span style="color: rgb(0, 0, 0);">    fread </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> open(fpath,</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">r</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);"> 38</span> <span style="color: rgb(0, 0, 0);">    aitem </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> None<br /></span><span style="color: rgb(0, 128, 128);"> 39</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> line </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> fread:<br /></span><span style="color: rgb(0, 128, 128);"> 40</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);"> 41</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> todo_begin </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> line:<br /></span><span style="color: rgb(0, 128, 128);"> 42</span> <span style="color: rgb(0, 0, 0);">            isFinding </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> True<br /></span><span style="color: rgb(0, 128, 128);"> 43</span> <span style="color: rgb(0, 0, 0);">            aitem </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> {}<br /></span><span style="color: rgb(0, 128, 128);"> 44</span> <span style="color: rgb(0, 0, 0);">            </span><span style="color: rgb(0, 0, 255);">continue</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 45</span> <span style="color: rgb(0, 0, 0);">                <br /></span><span style="color: rgb(0, 128, 128);"> 46</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> todo_end </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> line:<br /></span><span style="color: rgb(0, 128, 128);"> 47</span> <span style="color: rgb(0, 0, 0);">            isFinding </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> False<br /></span><span style="color: rgb(0, 128, 128);"> 48</span> <span style="color: rgb(0, 0, 0);">            </span><span style="color: rgb(0, 0, 255);">continue</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 49</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);"> 50</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">( isFinding </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> False):<br /></span><span style="color: rgb(0, 128, 128);"> 51</span> <span style="color: rgb(0, 0, 0);">            </span><span style="color: rgb(0, 0, 255);">continue</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 52</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);"> 53</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> mapKeyValue(aitem,ITEM_KEY,line):<br /></span><span style="color: rgb(0, 128, 128);"> 54</span> <span style="color: rgb(0, 0, 0);">            </span><span style="color: rgb(0, 0, 255);">continue</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 55</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);"> 56</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> mapKeyValue(aitem,EST_KEY,line):<br /></span><span style="color: rgb(0, 128, 128);"> 57</span> <span style="color: rgb(0, 0, 0);">            </span><span style="color: rgb(0, 0, 255);">continue</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 58</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);"> 59</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> mapKeyValue(aitem,FILE_PATH_KEY,line):<br /></span><span style="color: rgb(0, 128, 128);"> 60</span> <span style="color: rgb(0, 0, 0);">            </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> FILE_PATH_KEY </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> line:<br /></span><span style="color: rgb(0, 128, 128);"> 61</span> <span style="color: rgb(0, 0, 0);">                alist.append(aitem)<br /></span><span style="color: rgb(0, 128, 128);"> 62</span> <span style="color: rgb(0, 0, 0);">                aitem </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> None<br /></span><span style="color: rgb(0, 128, 128);"> 63</span> <span style="color: rgb(0, 0, 0);">                </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">--Find todo item in </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> fpath </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">\n</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 64</span> <span style="color: rgb(0, 0, 0);">            </span><span style="color: rgb(0, 0, 255);">continue</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 65</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);"> 66</span> <span style="color: rgb(0, 0, 0);">    <br /></span><span style="color: rgb(0, 128, 128);"> 67</span> <span style="color: rgb(0, 0, 0);">    fread.close()<br /></span><span style="color: rgb(0, 128, 128);"> 68</span> <span style="color: rgb(0, 0, 0);">    <br /></span><span style="color: rgb(0, 128, 128);"> 69</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);"> 70</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> mapKeyValue(item,key,line):<br /></span><span style="color: rgb(0, 128, 128);"> 71</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> key </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> line:        <br /></span><span style="color: rgb(0, 128, 128);"> 72</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">test</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 73</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">key is:</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> key </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">\n</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 74</span> <span style="color: rgb(0, 0, 0);">        item[key] </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> line[len(key):]<br /></span><span style="color: rgb(0, 128, 128);"> 75</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> True<br /></span><span style="color: rgb(0, 128, 128);"> 76</span> <span style="color: rgb(0, 0, 0);">    <br /></span><span style="color: rgb(0, 128, 128);"> 77</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> False<br /></span><span style="color: rgb(0, 128, 128);"> 78</span> <span style="color: rgb(0, 0, 0);">    <br /></span><span style="color: rgb(0, 128, 128);"> 79</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 80</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> findTodoFromFolder(alist,folder):<br /></span><span style="color: rgb(0, 128, 128);"> 81</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">  parent, dirnames, filenames  </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);">  os.walk(folder):<br /></span><span style="color: rgb(0, 128, 128);"> 82</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> fname </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> filenames:<br /></span><span style="color: rgb(0, 128, 128);"> 83</span> <span style="color: rgb(0, 0, 0);">            findTodo(os.path.join(parent, fname),alist)<br /></span><span style="color: rgb(0, 128, 128);"> 84</span> <span style="color: rgb(0, 0, 0);">            <br /></span><span style="color: rgb(0, 128, 128);"> 85</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);"> 86</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> mainLogic():<br /></span><span style="color: rgb(0, 128, 128);"> 87</span> <span style="color: rgb(0, 0, 0);">    configFileName </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">config.txt</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 88</span> <span style="color: rgb(0, 0, 0);">    cf </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> ConfigParser.ConfigParser();<br /></span><span style="color: rgb(0, 128, 128);"> 89</span> <span style="color: rgb(0, 0, 0);">    cf.read(configFileName)<br /></span><span style="color: rgb(0, 128, 128);"> 90</span> <span style="color: rgb(0, 0, 0);">    <br /></span><span style="color: rgb(0, 128, 128);"> 91</span> <span style="color: rgb(0, 0, 0);">    flist </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> cf.get(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">path</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">folder_list</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">).split(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">,</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);"> 92</span> <span style="color: rgb(0, 0, 0);">    itemlist </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> []<br /></span><span style="color: rgb(0, 128, 128);"> 93</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> folder  </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> flist:<br /></span><span style="color: rgb(0, 128, 128);"> 94</span> <span style="color: rgb(0, 0, 0);">        findTodoFromFolder(itemlist,folder)<br /></span><span style="color: rgb(0, 128, 128);"> 95</span> <span style="color: rgb(0, 0, 0);">    <br /></span><span style="color: rgb(0, 128, 128);"> 96</span> <span style="color: rgb(0, 0, 0);">    excelPath </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> cf.get(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">path</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">excel_path</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);"> 97</span> <span style="color: rgb(0, 0, 0);">    <br /></span><span style="color: rgb(0, 128, 128);"> 98</span> <span style="color: rgb(0, 0, 0);">    mapItemIntoExcel(itemlist,excelPath)<br /></span><span style="color: rgb(0, 128, 128);"> 99</span> <span style="color: rgb(0, 0, 0);">    <br /></span><span style="color: rgb(0, 128, 128);">100</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">\n---- OK -------- \n</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">101</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">102</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> createCellStype():<br /></span><span style="color: rgb(0, 128, 128);">103</span> <span style="color: rgb(0, 0, 0);">    font </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> xlwt.Font()<br /></span><span style="color: rgb(0, 128, 128);">104</span> <span style="color: rgb(0, 0, 0);">    font.wrap </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> xlwt.Alignment.WRAP_AT_RIGHT<br /></span><span style="color: rgb(0, 128, 128);">105</span> <span style="color: rgb(0, 0, 0);">    style </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> xlwt.XFStyle()<br /></span><span style="color: rgb(0, 128, 128);">106</span> <span style="color: rgb(0, 0, 0);">    style.font </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> font<br /></span><span style="color: rgb(0, 128, 128);">107</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> style<br /></span><span style="color: rgb(0, 128, 128);">108</span> <span style="color: rgb(0, 0, 0);">    <br /></span><span style="color: rgb(0, 128, 128);">109</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">110</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> mapItemIntoExcel(itemlist,excelPath):<br /></span><span style="color: rgb(0, 128, 128);">111</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);">:<br /></span><span style="color: rgb(0, 128, 128);">112</span> <span style="color: rgb(0, 0, 0);">                <br /></span><span style="color: rgb(0, 128, 128);">113</span> <span style="color: rgb(0, 0, 0);">        rb </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> xlrd.open_workbook(excelPath,formatting_info</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">True,on_demand</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">True)<br /></span><span style="color: rgb(0, 128, 128);">114</span> <span style="color: rgb(0, 0, 0);">        wb </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> xlutils.copy.copy(rb)<br /></span><span style="color: rgb(0, 128, 128);">115</span> <span style="color: rgb(0, 0, 0);">        sheet </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> wb.get_sheet(</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 128, 128);">116</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">117</span> <span style="color: rgb(0, 0, 0);">        row_index </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> len(sheet.rows)<br /></span><span style="color: rgb(0, 128, 128);">118</span> <span style="color: rgb(0, 0, 0);">        row_index </span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">119</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">120</span> <span style="color: rgb(0, 0, 0);">        cellStyle </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> createCellStype()<br /></span><span style="color: rgb(0, 128, 128);">121</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> item </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> itemlist:<br /></span><span style="color: rgb(0, 128, 128);">122</span> <span style="color: rgb(0, 0, 0);">            sheet.row(row_index).write(xlwt.Utils.col_by_name(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">A</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">),unicode(item[ITEM_KEY]),cellStyle )<br /></span><span style="color: rgb(0, 128, 128);">123</span> <span style="color: rgb(0, 0, 0);">            sheet.row(row_index).write(xlwt.Utils.col_by_name(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">D</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">),item[EST_KEY] ,cellStyle)<br /></span><span style="color: rgb(0, 128, 128);">124</span> <span style="color: rgb(0, 0, 0);">            sheet.row(row_index).write(xlwt.Utils.col_by_name(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">F</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">),</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">N</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">  ,cellStyle)<br /></span><span style="color: rgb(0, 128, 128);">125</span> <span style="color: rgb(0, 0, 0);">            sheet.row(row_index).write(xlwt.Utils.col_by_name(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">H</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">),item[FILE_PATH_KEY]  ,cellStyle)<br /></span><span style="color: rgb(0, 128, 128);">126</span> <span style="color: rgb(0, 0, 0);">            row_index </span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">127</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">128</span> <span style="color: rgb(0, 0, 0);">        wb.save(excelPath)<br /></span><span style="color: rgb(0, 128, 128);">129</span> <span style="color: rgb(0, 0, 0);">        <br /></span><span style="color: rgb(0, 128, 128);">130</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">finally</span><span style="color: rgb(0, 0, 0);">:<br /></span><span style="color: rgb(0, 128, 128);">131</span> <span style="color: rgb(0, 0, 0);">        rb.release_resources    <br /></span><span style="color: rgb(0, 128, 128);">132</span> <span style="color: rgb(0, 0, 0);">    <br /></span><span style="color: rgb(0, 128, 128);">133</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 128);">__name__</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">__main__</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">:<br /></span><span style="color: rgb(0, 128, 128);">134</span> <span style="color: rgb(0, 0, 0);">    mainLogic();<br /></span><span style="color: rgb(0, 128, 128);">135</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">136</span> <span style="color: rgb(0, 0, 0);"></span></div><br /><br />程序和配置文件请在这里下载:  <a target="_blank" title="http://www.uudisc.com/user/diegoyun/file/3880743" href="http://www.uudisc.com/user/diegoyun/file/3880743">http://www.uudisc.com/user/diegoyun/file/3880743</a><br /><br /></wbr></wbr></wbr></wbr></wbr></wbr></div><br />如何运行<br />
1&gt; sudo apt-get install python-xlrt python-xlwt<br />
2&gt; 到这个页面下载 xlutis : <a href="http://pypi.python.org/pypi/xlutils#downloads" target="_blank">http://pypi.python.org/pypi/<wbr>xlutils#downloads</wbr></a>   .<br />
然后 sudo easy_install xlutils   该功能需要安装python 的 setuptools 功能. 如果没装,<br />
ubuntu 会提示你安装.<br />
3&gt; 配置config.txt, 指定目录和excel路径<br />
4&gt; 运行 FindToDo.py<br /><br /><br /><br /><br /><img src ="http://www.cnitblog.com/yunshichen/aggbug/72760.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/yunshichen/" target="_blank">Chenyunshi</a> 2011-02-06 21:42 <a href="http://www.cnitblog.com/yunshichen/archive/2011/02/06/72760.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Python 读写 Excel</title><link>http://www.cnitblog.com/yunshichen/archive/2010/03/16/64661.html</link><dc:creator>Chenyunshi</dc:creator><author>Chenyunshi</author><pubDate>Mon, 15 Mar 2010 17:20:00 GMT</pubDate><guid>http://www.cnitblog.com/yunshichen/archive/2010/03/16/64661.html</guid><wfw:comment>http://www.cnitblog.com/yunshichen/comments/64661.html</wfw:comment><comments>http://www.cnitblog.com/yunshichen/archive/2010/03/16/64661.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cnitblog.com/yunshichen/comments/commentRss/64661.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/yunshichen/services/trackbacks/64661.html</trackback:ping><description><![CDATA[
		<br />基本上, 这个网页已经说明一切了: <a target="_blank" title="http://pypi.python.org/pypi/xlrd" href="http://pypi.python.org/pypi/xlrd">http://pypi.python.org/pypi/xlrd</a><br /><br />等有时间再把这个页面写漂亮,现在先记一些代码.<br /><br /><h2><font color="#336600"><b>读Excel</b></font></h2>先建个simple.xls<br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> xlrd </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> open_workbook<br /><br />wb </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> open_workbook(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">simple.xls</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">rb</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> s </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> wb.sheets():<br />    </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Sheet:</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,s.name<br />    </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> row </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> range(s.nrows):<br />        values</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">[]<br />        </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> col </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> range(s.ncols):<br />            values.append(s.cell(row,col).value)<br />        </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">,</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">.join(values)<br />    </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"><br /><br />        <br /></span></div><br /><hr size="2" width="100%" /><h2><font><b><font color="#336600"><b>写Excel</b></font></b></font></h2><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> tempfile </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> TemporaryFile<br /></span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> xlwt </span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> Workbook<br /><br />book </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> Workbook()<br />sheet1 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> book.add_sheet(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Sheet 1</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br />book.add_sheet(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Sheet 2</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br />sheet1.write(0,0,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">A1</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br />sheet1.write(0,</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">B1</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /><br />row1 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> sheet1.row(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br />row1.write(0,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">A2</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br />row1.write(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">B2</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /><br />sheet1.col(0).width </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">10000</span><span style="color: rgb(0, 0, 0);"><br /><br />sheet2 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> book.get_sheet(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br />sheet2.row(0).write(0,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Sheet 2 A1</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br />sheet2.row(0).write(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Sheet 2 B1</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br />sheet2.flush_row_data()<br /><br />sheet2.write(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,0,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">Sheet 2 A3</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br />sheet2.col(0).width </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">5000</span><span style="color: rgb(0, 0, 0);"><br />sheet2.col(0).hidden </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> True<br /><br />book.save(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">simple2.xls</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br />book.save(TemporaryFile())<br /></span></div><br /><br /><br /><img src ="http://www.cnitblog.com/yunshichen/aggbug/64661.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/yunshichen/" target="_blank">Chenyunshi</a> 2010-03-16 01:20 <a href="http://www.cnitblog.com/yunshichen/archive/2010/03/16/64661.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>python实用技巧 : Filtering os.walk</title><link>http://www.cnitblog.com/yunshichen/archive/2010/03/07/64528.html</link><dc:creator>Chenyunshi</dc:creator><author>Chenyunshi</author><pubDate>Sun, 07 Mar 2010 08:42:00 GMT</pubDate><guid>http://www.cnitblog.com/yunshichen/archive/2010/03/07/64528.html</guid><wfw:comment>http://www.cnitblog.com/yunshichen/comments/64528.html</wfw:comment><comments>http://www.cnitblog.com/yunshichen/archive/2010/03/07/64528.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/yunshichen/comments/commentRss/64528.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/yunshichen/services/trackbacks/64528.html</trackback:ping><description><![CDATA[
		<br />
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(128, 0, 0);">'''</span>
				<span style="color: rgb(128, 0, 0);">
						<br />Created on Mar 7, 2010<br /><br />@author: Diego<br /><br />需求: 得到某个目录下, 符合过滤条件的文件夹/文件.<br />实现: 将os.walk再次包装.<br /><br />TODO: 不知道本程序的做法, 和传统的逐个目录列举的方法, 哪个效率更高. 待测试.<br /><br /></span>
				<span style="color: rgb(128, 0, 0);">'''</span>
				<span style="color: rgb(0, 0, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 255);">import</span>
				<span style="color: rgb(0, 0, 0);"> os<br /></span>
				<span style="color: rgb(0, 0, 255);">import</span>
				<span style="color: rgb(0, 0, 0);"> os.path<br /><br />os.path.sep</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">/</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">
						<br />path </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">/media/dev/project/google_codes/srgjs</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">
						<br />EXCLUDE_DIR_LIST </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> [</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">.SVN</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">,</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">CVS</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">]<br />EXCLUDE_FILE_LIST </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> [</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">.CVSIGNORE</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">]<br /><br /><br /></span>
				<span style="color: rgb(0, 0, 255);">def</span>
				<span style="color: rgb(0, 0, 0);"> is_parent_exclude(parentPath,excludeDirList):<br />    ss</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">parentPath.split(</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">/</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">);<br />    </span>
				<span style="color: rgb(0, 0, 255);">for</span>
				<span style="color: rgb(0, 0, 0);"> s </span>
				<span style="color: rgb(0, 0, 255);">in</span>
				<span style="color: rgb(0, 0, 0);"> ss:<br />        </span>
				<span style="color: rgb(0, 0, 255);">if</span>
				<span style="color: rgb(0, 0, 0);">(s.upper() </span>
				<span style="color: rgb(0, 0, 255);">in</span>
				<span style="color: rgb(0, 0, 0);"> excludeDirList):<br />            </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> True<br />    <br />    </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> False<br /><br /></span>
				<span style="color: rgb(0, 0, 255);">def</span>
				<span style="color: rgb(0, 0, 0);"> filter_walk(targetDirectory,excludeDirList,excludeFileExtList):<br />    dirList</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">[]<br />    fileList</span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);">[]<br />    </span>
				<span style="color: rgb(0, 0, 255);">for</span>
				<span style="color: rgb(0, 0, 0);"> (parent, dirs, files) </span>
				<span style="color: rgb(0, 0, 255);">in</span>
				<span style="color: rgb(0, 0, 0);"> os.walk(targetDirectory):<br />        <br />        </span>
				<span style="color: rgb(0, 0, 255);">for</span>
				<span style="color: rgb(0, 0, 0);"> d </span>
				<span style="color: rgb(0, 0, 255);">in</span>
				<span style="color: rgb(0, 0, 0);"> dirs:<br />            </span>
				<span style="color: rgb(0, 0, 255);">if</span>
				<span style="color: rgb(0, 0, 0);">(d.upper() </span>
				<span style="color: rgb(0, 0, 255);">in</span>
				<span style="color: rgb(0, 0, 0);"> excludeDirList):<br />                </span>
				<span style="color: rgb(0, 0, 255);">continue</span>
				<span style="color: rgb(0, 0, 0);">
						<br />            <br />            </span>
				<span style="color: rgb(0, 128, 0);">#</span>
				<span style="color: rgb(0, 128, 0);">To check if one of the parent dir should be excluded.</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">            </span>
				<span style="color: rgb(0, 0, 255);">if</span>
				<span style="color: rgb(0, 0, 0);">(is_parent_exclude(parent,excludeDirList)):<br />                </span>
				<span style="color: rgb(0, 0, 255);">continue</span>
				<span style="color: rgb(0, 0, 0);">
						<br />            <br />            dirList.append(parent</span>
				<span style="color: rgb(0, 0, 0);">+</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">/</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">+</span>
				<span style="color: rgb(0, 0, 0);">d)<br />            <br />        <br />        </span>
				<span style="color: rgb(0, 0, 255);">for</span>
				<span style="color: rgb(0, 0, 0);"> f </span>
				<span style="color: rgb(0, 0, 255);">in</span>
				<span style="color: rgb(0, 0, 0);"> files:<br />            </span>
				<span style="color: rgb(0, 0, 255);">if</span>
				<span style="color: rgb(0, 0, 0);">(f.upper() </span>
				<span style="color: rgb(0, 0, 255);">in</span>
				<span style="color: rgb(0, 0, 0);"> excludeFileExtList):<br />                </span>
				<span style="color: rgb(0, 0, 255);">continue</span>
				<span style="color: rgb(0, 0, 0);">
						<br />            </span>
				<span style="color: rgb(0, 128, 0);">#</span>
				<span style="color: rgb(0, 128, 0);">To check if one of the parent dir should be excluded.</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">            </span>
				<span style="color: rgb(0, 0, 255);">if</span>
				<span style="color: rgb(0, 0, 0);">(is_parent_exclude(parent,excludeDirList)):<br />                </span>
				<span style="color: rgb(0, 0, 255);">continue</span>
				<span style="color: rgb(0, 0, 0);">
						<br />            <br />            fileList.append(parent</span>
				<span style="color: rgb(0, 0, 0);">+</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(128, 0, 0);">/</span>
				<span style="color: rgb(128, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">+</span>
				<span style="color: rgb(0, 0, 0);">f)<br />    <br />    </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> (dirList,fileList)            <br /><br /></span>
				<span style="color: rgb(0, 128, 0);">#</span>
				<span style="color: rgb(0, 128, 0);">test</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">dirs,files </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> filter_walk(path,EXCLUDE_DIR_LIST,EXCLUDE_FILE_LIST)<br /><br /></span>
				<span style="color: rgb(0, 0, 255);">for</span>
				<span style="color: rgb(0, 0, 0);"> d </span>
				<span style="color: rgb(0, 0, 255);">in</span>
				<span style="color: rgb(0, 0, 0);"> dirs:<br />    </span>
				<span style="color: rgb(0, 0, 255);">print</span>
				<span style="color: rgb(0, 0, 0);"> d<br /><br /></span>
				<span style="color: rgb(0, 0, 255);">for</span>
				<span style="color: rgb(0, 0, 0);"> f </span>
				<span style="color: rgb(0, 0, 255);">in</span>
				<span style="color: rgb(0, 0, 0);"> files:<br />    </span>
				<span style="color: rgb(0, 0, 255);">print</span>
				<span style="color: rgb(0, 0, 0);"> f<br /><br /><br /></span>
		</div>
		<br />
<img src ="http://www.cnitblog.com/yunshichen/aggbug/64528.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/yunshichen/" target="_blank">Chenyunshi</a> 2010-03-07 16:42 <a href="http://www.cnitblog.com/yunshichen/archive/2010/03/07/64528.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Python2.5/2.6实用教程:基础篇</title><link>http://www.cnitblog.com/yunshichen/archive/2008/11/21/51775.html</link><dc:creator>Chenyunshi</dc:creator><author>Chenyunshi</author><pubDate>Fri, 21 Nov 2008 09:53:00 GMT</pubDate><guid>http://www.cnitblog.com/yunshichen/archive/2008/11/21/51775.html</guid><wfw:comment>http://www.cnitblog.com/yunshichen/comments/51775.html</wfw:comment><comments>http://www.cnitblog.com/yunshichen/archive/2008/11/21/51775.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/yunshichen/comments/commentRss/51775.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/yunshichen/services/trackbacks/51775.html</trackback:ping><description><![CDATA[
		<h2>介绍</h2>
		<hr size="2" width="100%" />
		<br />本文方便有经验的程序员进入Python世界.本文适用于python2.5/2.6版本.<br /><br />Deparecated:Python3 和 python2.x语法不同.如果你正在用python3,请参考新教程:<a target="_blank" title="Python3入门" href="/yunshichen/archive/2009/04/01/55924.html">http://www.cnitblog.com/yunshichen/archive/2009/04/01/55924.html</a><br /><br /><h3><font color="#006400">起步走</font><br /></h3><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">! /usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);"><br />a</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);"><br />b</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);"><br />c="test"<br />c</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">a</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">b<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">execution result: %i</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">c</span></div><br />知识点<br /><ul><li>Python是动态语言,变量不须预先声明.</li><li>打印语句采用C风格<br /></li></ul><h3><font color="#006400">字符串和数字</font></h3>但有趣的是,在javascript里我们会理想当然的将字符串和数字连接,因为是动态语言嘛.但在Python里有点诡异,如下:<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">! /usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);"><br />a</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);"><br />b</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">test</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />c</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">a</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">b<br /></span></div><br />运行这行程序会出错,提示你字符串和数字不能连接,于是只好用内置函数进行转换<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">! /usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);"><br />a</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);"><br />b</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">test</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />c</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">str(a)</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">b<br />d</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">1111</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />e</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">a</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">int(d)<br /></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">How to print multiply values</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">c is %s,e is %i</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);"> (c,e)</span></div>知识点:<br /><ul><li>用int和str函数将字符串和数字进行转换</li><li>打印以#开头,而不是习惯的//</li><li>打印多个参数的方式<br /></li></ul><h3><font><font><font><font color="#006400">国际化</font></font></font></font></h3>写腻了英文注释,我们要用中文!<br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">! /usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br />#</span><span style="color: rgb(0, 128, 0);"> -*- coding: utf8 -*- </span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">上帝重返人间:马拉多纳出任阿根廷国家足球队主帅.</span><span style="color: rgb(128, 0, 0);">"</span></div>知识点:<br /><ul><li>加上字符集即可使用中文<br /></li></ul><h3><font><font color="#006400">列表</font></font></h3>列表类似Javascript的数组,方便易用<br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">! /usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br />#</span><span style="color: rgb(0, 128, 0);"> -*- coding: utf8 -*- </span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">定义元组</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">word</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">c</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">d</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">e</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">f</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">g</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br /><br /></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">如何通过索引访问元组里的元素</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">a</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">word[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">a is: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">a<br />b</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">word[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">b is: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> b </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> index 1 and 2 elements of word.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">c</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">word[:</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">c is: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> c </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> index 0 and 1 elements of word.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">d</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">word[0:]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">d is: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> d </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> All elements of word.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">元组可以合并</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">e</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">word[:</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">word[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">:]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">e is: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> e </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> All elements of word.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">f</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">word[</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">f is: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> f </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> The last elements of word.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">g</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">word[</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">g is: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> g </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> index 3 and 4 elements of word.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">h</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">word[</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">:]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">h is: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> h </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> The last two elements.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">word[:</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">i is: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> i </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> Everything except the last two characters</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">l</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">len(word)<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">Length of word is: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> str(l)<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">Adds new element</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />word.append(</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">h</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> word<br /><br /></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">删除元素</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 255);">del</span><span style="color: rgb(0, 0, 0);"> word[0]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> word<br /></span><span style="color: rgb(0, 0, 255);">del</span><span style="color: rgb(0, 0, 0);"> word[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> word</span></div><br />知识点:<br /><ul><li>列表长度是动态的,可任意添加删除元素.</li><li>用索引可以很方便访问元素,甚至返回一个子列表</li><li>更多方法请参考Python的文档<br /></li></ul><h3><font><font color="#006400">字典</font></font></h3><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">! /usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);"><br />x</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">{</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">aaa</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">bbb</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">c</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">12</span><span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> x[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> x[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> x[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">c</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br /><br /></span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> key </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> x:<br />    </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">Key is %s and value is %s</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,(key,x[key])<br />    <br />keys</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">x.items();<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> keys[0]<br />keys[0]</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">ddd</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> keys[0]</span></div><br />知识点:<br /><ul><li>将他当Java的Map来用即可.<br /></li></ul><br /><h3><font><font><font><font><font><font><font><font color="#006400">字符串</font></font></font></font></font></font></font></font></h3>比起C/C++,Python处理字符串的方式实在太让人感动了.把字符串当列表来用吧.<br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">word</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">abcdefg</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br />a</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">word[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">a is: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">a<br />b</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">word[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">b is: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">b </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> index 1 and 2 elements of word.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">c</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">word[:</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">c is: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">c </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> index 0 and 1 elements of word.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">d</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">word[0:]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">d is: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">d </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> All elements of word.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">e</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">word[:</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">]</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">word[</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">:]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">e is: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">e </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> All elements of word.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">f</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">word[</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">f is: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">f </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> The last elements of word.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">g</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">word[</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">g is: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">g </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> index 3 and 4 elements of word.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">h</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">word[</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">:]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">h is: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">h </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> The last two elements.</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">word[:</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">i is: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">i </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> Everything except the last two characters</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">l</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">len(word)<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">Length of word is: </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> str(l)</span></div><br />不过要注意Asc和Unicode字符串的区别:<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">! /usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br />#</span><span style="color: rgb(0, 128, 0);"> -*- coding: utf8 -*- </span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);"><br />s</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">raw_input(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">输入你的中文名,按回车继续<img src="http://www.cnitblog.com/images/dot.gif" /></span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">你的名字是  : </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">s;<br /><br />l</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">len(s)<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">你中文名字的长度是:</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">str(l);<br />a</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">unicode(s,</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">utf8</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)<br />l</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">len(a)<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">对不起,刚才计算错误.我们应该用utf8来计算中文字符串的长度, \</span><span style="color: rgb(128, 0, 0);"><br /></span><span style="color: rgb(0, 0, 0);">你名字的长度应该是:</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">+str(l);</span></div>知识点:<br /><ul><li>用unicode函数进行转码<br /></li></ul><h3><font><font><font><font color="#006400">条件和循环语句</font></font></font></font></h3><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">! /usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">x</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">int(raw_input(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">Please enter an integer:</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">))<br /></span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> x</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">0:<br />    x</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">0<br />    </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">Negative changed to zero</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">elif</span><span style="color: rgb(0, 0, 0);"> x</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">0:<br />    </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">Zero</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">:<br />    </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">More</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /><br /><br /></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> Loops List</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">a </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> [</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">cat</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">window</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">defenestrate</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> x </span><span style="color: rgb(0, 0, 255);">in</span><span style="color: rgb(0, 0, 0);"> a:<br />    </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> x, len(x)</span></div><br />知识点:<br /><ul><li>条件和循环语句</li><li>如何得到控制台输入<br /></li></ul><h3><font><font><font><font><font><font><font><font color="#006400">函数</font></font></font></font></font></font></font></font></h3><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">! /usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br />#</span><span style="color: rgb(0, 128, 0);"> -*- coding: utf8 -*- </span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> sum(a,b):<br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> a</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">b<br /><br /><br />func </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> sum<br />r </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> func(</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> r<br /><br /></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> 提供默认值</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 255);">def</span><span style="color: rgb(0, 0, 0);"> add(a,b</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">):<br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> a</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">b<br />r</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">add(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> r<br />r</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">add(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> r</span></div><br />一个好用的函数<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">! /usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br />#</span><span style="color: rgb(0, 128, 0);"> -*- coding: utf8 -*- </span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> The range() function</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">a </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">range(</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> a<br />a </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> range(</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">7</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> a<br />a </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> range(</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">7</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> a<br />a </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> range(</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">11</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> The 3rd parameter stands for step</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> a</span></div><br />

知识点:<br /><ul><li>Python 不用{}来控制程序结构,他强迫你用缩进来写程序,使代码清晰.</li><li>定义函数方便简单</li><li>方便好用的range函数<br /></li></ul><h3><font color="#006400">异常处理</font></h3><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">! /usr/bin/python</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">s</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">raw_input(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">Input your age:</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> s </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(128, 0, 0);">""</span><span style="color: rgb(0, 0, 0);">:<br />    </span><span style="color: rgb(0, 0, 255);">raise</span><span style="color: rgb(0, 0, 0);"> Exception(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">Input must no be empty.</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)<br /><br /></span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);">:<br />    i</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">int(s)<br /></span><span style="color: rgb(0, 0, 255);">except</span><span style="color: rgb(0, 0, 0);"> ValueError:<br />    </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">Could not convert data to an integer.</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">except</span><span style="color: rgb(0, 0, 0);">:<br />    </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">Unknown exception!</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">: </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> It is useful for code that must be executed if the try clause does not raise an exception</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">You are %d</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);"> i,</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);"> years old</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">finally</span><span style="color: rgb(0, 0, 0);">: </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> Clean up action</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">Goodbye!</span><span style="color: rgb(128, 0, 0);">"</span></div><br /><br /><br /><br /><img src ="http://www.cnitblog.com/yunshichen/aggbug/51775.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/yunshichen/" target="_blank">Chenyunshi</a> 2008-11-21 17:53 <a href="http://www.cnitblog.com/yunshichen/archive/2008/11/21/51775.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Python:文件操作技巧(File operation)</title><link>http://www.cnitblog.com/yunshichen/archive/2008/08/11/47900.html</link><dc:creator>Chenyunshi</dc:creator><author>Chenyunshi</author><pubDate>Mon, 11 Aug 2008 07:18:00 GMT</pubDate><guid>http://www.cnitblog.com/yunshichen/archive/2008/08/11/47900.html</guid><wfw:comment>http://www.cnitblog.com/yunshichen/comments/47900.html</wfw:comment><comments>http://www.cnitblog.com/yunshichen/archive/2008/08/11/47900.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cnitblog.com/yunshichen/comments/commentRss/47900.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/yunshichen/services/trackbacks/47900.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Python相关文件操作函数实例和说明&nbsp;&nbsp;<a href='http://www.cnitblog.com/yunshichen/archive/2008/08/11/47900.html'>阅读全文</a><img src ="http://www.cnitblog.com/yunshichen/aggbug/47900.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/yunshichen/" target="_blank">Chenyunshi</a> 2008-08-11 15:18 <a href="http://www.cnitblog.com/yunshichen/archive/2008/08/11/47900.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>备份文件的python脚本</title><link>http://www.cnitblog.com/yunshichen/archive/2008/08/08/47768.html</link><dc:creator>Chenyunshi</dc:creator><author>Chenyunshi</author><pubDate>Thu, 07 Aug 2008 16:48:00 GMT</pubDate><guid>http://www.cnitblog.com/yunshichen/archive/2008/08/08/47768.html</guid><wfw:comment>http://www.cnitblog.com/yunshichen/comments/47768.html</wfw:comment><comments>http://www.cnitblog.com/yunshichen/archive/2008/08/08/47768.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cnitblog.com/yunshichen/comments/commentRss/47768.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/yunshichen/services/trackbacks/47768.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 备份文件的python脚本，顺便学习学习语法&nbsp;&nbsp;<a href='http://www.cnitblog.com/yunshichen/archive/2008/08/08/47768.html'>阅读全文</a><img src ="http://www.cnitblog.com/yunshichen/aggbug/47768.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/yunshichen/" target="_blank">Chenyunshi</a> 2008-08-08 00:48 <a href="http://www.cnitblog.com/yunshichen/archive/2008/08/08/47768.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>实例教程:1小时学会Python</title><link>http://www.cnitblog.com/yunshichen/archive/2008/05/09/43527.html</link><dc:creator>Chenyunshi</dc:creator><author>Chenyunshi</author><pubDate>Thu, 08 May 2008 18:23:00 GMT</pubDate><guid>http://www.cnitblog.com/yunshichen/archive/2008/05/09/43527.html</guid><wfw:comment>http://www.cnitblog.com/yunshichen/comments/43527.html</wfw:comment><comments>http://www.cnitblog.com/yunshichen/archive/2008/05/09/43527.html#Feedback</comments><slash:comments>7</slash:comments><wfw:commentRss>http://www.cnitblog.com/yunshichen/comments/commentRss/43527.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/yunshichen/services/trackbacks/43527.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 适合有经验的程序员快速学会Python&nbsp;&nbsp;<a href='http://www.cnitblog.com/yunshichen/archive/2008/05/09/43527.html'>阅读全文</a><img src ="http://www.cnitblog.com/yunshichen/aggbug/43527.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/yunshichen/" target="_blank">Chenyunshi</a> 2008-05-09 02:23 <a href="http://www.cnitblog.com/yunshichen/archive/2008/05/09/43527.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>