﻿<?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>BlogJava-weidagang2046的专栏-文章分类-Python</title><link>http://www.blogjava.net/weidagang2046/category/5029.html</link><description>物格而后知致</description><language>zh-cn</language><lastBuildDate>Fri, 02 Mar 2007 06:38:12 GMT</lastBuildDate><pubDate>Fri, 02 Mar 2007 06:38:12 GMT</pubDate><ttl>60</ttl><item><title>怎样把一个二维数组写入文件</title><link>http://www.blogjava.net/weidagang2046/articles/28662.html</link><dc:creator>weidagang2046</dc:creator><author>weidagang2046</author><pubDate>Thu, 19 Jan 2006 07:48:00 GMT</pubDate><guid>http://www.blogjava.net/weidagang2046/articles/28662.html</guid><wfw:comment>http://www.blogjava.net/weidagang2046/comments/28662.html</wfw:comment><comments>http://www.blogjava.net/weidagang2046/articles/28662.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/weidagang2046/comments/commentRss/28662.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/weidagang2046/services/trackbacks/28662.html</trackback:ping><description><![CDATA[发信人: ilovecpp (cpp), 信区: Python<BR>标 &nbsp;题: Re: 怎样把一个二维数组写入文件?<BR>发信站: 水木社区 (Tue Jan 17 23:39:50 2006), 站内<BR><BR>【 在 petre (petre) 的大作中提到: 】<BR><SPAN class=f006>: 比如</SPAN><BR><SPAN class=f006>: A=[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]</SPAN><BR><SPAN class=f006>: 写入文件后文件内容是</SPAN><BR><SPAN class=f006>: 1 2 3 4</SPAN><BR><SPAN class=f006>: 5 6 7 8</SPAN><BR><SPAN class=f006>: 9 10 11 12</SPAN><BR><SPAN class=f006>: 13 14 15 16</SPAN><BR><SPAN class=f006>: 就是不能带"[ ]"</SPAN><BR><SPAN class=f006>: 每行占一行</SPAN><BR><SPAN class=f006>: 急着用，不熟悉python</SPAN><BR><SPAN class=f006>: //bow</SPAN><BR><BR>print &gt;&gt;open('a.txt', 'w'), '\n'.join([' '.join(map(str, i)) for i in A])<img src ="http://www.blogjava.net/weidagang2046/aggbug/28662.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/weidagang2046/" target="_blank">weidagang2046</a> 2006-01-19 15:48 <a href="http://www.blogjava.net/weidagang2046/articles/28662.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Osh</title><link>http://www.blogjava.net/weidagang2046/articles/21331.html</link><dc:creator>weidagang2046</dc:creator><author>weidagang2046</author><pubDate>Thu, 24 Nov 2005 11:06:00 GMT</pubDate><guid>http://www.blogjava.net/weidagang2046/articles/21331.html</guid><wfw:comment>http://www.blogjava.net/weidagang2046/comments/21331.html</wfw:comment><comments>http://www.blogjava.net/weidagang2046/articles/21331.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/weidagang2046/comments/commentRss/21331.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/weidagang2046/services/trackbacks/21331.html</trackback:ping><description><![CDATA[<P>In a Unix shell, you can run a series of commands, connecting the output of one command with the input to the next command using pipes. The information passed from one command to the next is normally organized into lines of text. Tools such as awk and sed are designed to extract and transform information from these lines. It would be much simpler if the data passed between commands was organized into objects. Osh is based on this idea. Osh is implemented in Python, so much of osh will be familiar if you already know Python. 
<P>Osh is not a shell. It is an executable implementing a language that supports the composition of commands through piping. The objects piped from one command to the next can be Python primitives (numbers, lists, maps, etc.), other useful types such as dates/times, or database rows; or represent various OS resources such as files, directories, and processes. Conversions between objects and strings simplify integration with the Unix environment. The commands included with osh manipulate objects, access databases, and execute commands remotely, including parallel execution on nodes in a cluster. In a single osh invocation, all commands run in a single process. Multithreading is used only when executing commands on a cluster. 
<P><B>Example:</B> Suppose you have a cluster named fred, with nodes fred1, fred2, fred3. Each node has a database tracking work requests with a table named request. You can find the total number of open requests in each database as follows: <PRE><B><FONT color=#0066ff>[jao@zack]</FONT> osh @fred [ sql "select count(*) from request where state = 'open'" ] ^ out</B>
('fred1', 1)
('fred2', 0)
('fred3', 5)
</PRE>
<UL>
<LI><TT><B>osh:</B></TT> Invokes the osh executable. 
<LI><TT><B>@fred [ ... ]:</B></TT> Specifies that the following command, delimited by [...] should be run on each node of the cluster named fred. (The osh configuration file, .oshrc, specifies how to connect to the nodes of the cluster.) 
<LI><TT><B>sql "select count(*) from request where state = 'open'":</B></TT> Sql is an osh command that submits a query to a relational database. The query output is returned as a set of tuples. 
<LI><TT><B>^ out:</B></TT> ^ is the osh operator for piping objects from one command to the next In this case, the input objects are tuples resulting from execution of a SQL query on each node of the cluster. The out command renders each object as a string and prints it to stdout. 
<LI>Each output row identifies the node of origination (e.g. fred1, fred2), and contains a tuple from the database on that node. So ('fred3', 5) means that the database on node fred3 has 5 open requests. </LI></UL>
<P><B>Example, continued:</B> Now suppose you want to find the total number of open requests across the cluster. You can pipe the tuples into an aggregation command: <PRE><FONT color=#0066ff>[jao@zack]</FONT> osh @fred [ sql "select count(*) from request where state = 'open'" ] ^ <B>agg 0 'total, node, count: total + count' $</B>
6
</PRE>
<UL>
<LI><TT><B>agg:</B></TT> agg is the aggregation command. Tuples from across the cluster are piped into the agg command, which will accumulate results from all inputs. 
<LI><TT><B>0:</B></TT> This use of agg will maintain a total, which is initialized to 0. 
<LI><TT><B>'total, node, count: total + count':</B></TT> This specifies an aggregation function. <TT>total</TT> is the running total, which was initialized to 0. <TT>node</TT> and <TT>count</TT> come from the sql command executed on each node of the cluster. <TT>total + count</TT> accumulates the counts from each node. 
<LI><TT><B>$:</B></TT> An alternative to <TT>^ out</TT> that can be used at the end of a command only. 
<LI><TT><B>6:</B></TT> The total of the counts from across the cluster. </LI></UL>
<P><B>More information:</B> <BR><BR>License: GPL <BR><A href="http://geophile.com/osh/history.html">Release history</A> <BR>Tutorial (TBD) <BR><A href="http://geophile.com/osh/userguide.html">User guide</A> <BR><A href="http://geophile.com/osh/osh-0.6.1.tar.gz">Download</A> <BR><A href="http://geophile.com/osh/similar.html">Software with similar goals</A> </P>
<HR>

<ADDRESS><A href="mailto:jao@geophile.com">jao@geophile.com</A> <BR><BR>from: <A href="http://geophile.com/osh/">http://geophile.com/osh/</A></ADDRESS><img src ="http://www.blogjava.net/weidagang2046/aggbug/21331.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/weidagang2046/" target="_blank">weidagang2046</a> 2005-11-24 19:06 <a href="http://www.blogjava.net/weidagang2046/articles/21331.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Regular Expression HOWTO</title><link>http://www.blogjava.net/weidagang2046/articles/20675.html</link><dc:creator>weidagang2046</dc:creator><author>weidagang2046</author><pubDate>Sun, 20 Nov 2005 11:21:00 GMT</pubDate><guid>http://www.blogjava.net/weidagang2046/articles/20675.html</guid><wfw:comment>http://www.blogjava.net/weidagang2046/comments/20675.html</wfw:comment><comments>http://www.blogjava.net/weidagang2046/articles/20675.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/weidagang2046/comments/commentRss/20675.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/weidagang2046/services/trackbacks/20675.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: A.M. Kuchlingamk@amk.caAbstract:This document is an introductory tutorial to using regular expressions in Python with the re module. It provides a gentler introduction than the corresponding...&nbsp;&nbsp;<a href='http://www.blogjava.net/weidagang2046/articles/20675.html'>阅读全文</a><img src ="http://www.blogjava.net/weidagang2046/aggbug/20675.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/weidagang2046/" target="_blank">weidagang2046</a> 2005-11-20 19:21 <a href="http://www.blogjava.net/weidagang2046/articles/20675.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>String Manipulation </title><link>http://www.blogjava.net/weidagang2046/articles/20523.html</link><dc:creator>weidagang2046</dc:creator><author>weidagang2046</author><pubDate>Fri, 18 Nov 2005 16:31:00 GMT</pubDate><guid>http://www.blogjava.net/weidagang2046/articles/20523.html</guid><wfw:comment>http://www.blogjava.net/weidagang2046/comments/20523.html</wfw:comment><comments>http://www.blogjava.net/weidagang2046/articles/20523.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/weidagang2046/comments/commentRss/20523.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/weidagang2046/services/trackbacks/20523.html</trackback:ping><description><![CDATA[<A class=iAs style="PADDING-BOTTOM: 1px; COLOR: blue; BORDER-BOTTOM: blue 0.07em solid; BACKGROUND-COLOR: transparent; TEXT-DECORATION: underline" href="http://www.devshed.com/#" target=_blank>Programmers</A> need to know how to manipulate strings for a variety of purposes, regardless of the <A class=iAs style="PADDING-BOTTOM: 1px; COLOR: blue; BORDER-BOTTOM: blue 0.07em solid; BACKGROUND-COLOR: transparent; TEXT-DECORATION: underline" href="http://www.devshed.com/#" target=_blank>programming language</A> they are working in. This article will explain the various methods used to manipulate strings in Python.<B>Introduction</B> 
<P></P>
<P>String manipulation is very useful and very widely used in every language. Often, programmers are required to break down strings and examine them closely. For example, in my articles on <A class=iAs style="PADDING-BOTTOM: 1px; COLOR: blue; BORDER-BOTTOM: blue 0.07em solid; BACKGROUND-COLOR: transparent; TEXT-DECORATION: underline" href="http://www.devshed.com/#" target=_blank>IRC</A> (<A href="http://www.devshed.com/c/a/Python/Python-and-IRC/" rel=nofollow>http://www.devshed.com/c/a/Python/Python-and-IRC/</A>&nbsp;and <A href="http://www.devshed.com/c/a/Python/Basic-IRC-Tasks/" rel=nofollow>http://www.devshed.com/c/a/Python/Basic-IRC-Tasks/</A>), I used the <I>split</I> method to break down commands to make working with them easier. In my article on sockets (<A href="http://www.devshed.com/c/a/Python/Sockets-in-Python/" rel=nofollow>http://www.devshed.com/c/a/Python/Sockets-in-Python/</A>), I used <A class=iAs style="PADDING-BOTTOM: 1px; COLOR: blue; BORDER-BOTTOM: blue 0.07em solid; BACKGROUND-COLOR: transparent; TEXT-DECORATION: underline" href="http://www.devshed.com/#" target=_blank>regular expressions</A> to look through a <A class=iAs style="PADDING-BOTTOM: 1px; COLOR: blue; BORDER-BOTTOM: blue 0.07em solid; BACKGROUND-COLOR: transparent; TEXT-DECORATION: underline" href="http://www.devshed.com/#" target=_blank>website</A> and extract a currency exchange rate.</P>
<P>This article will take a look at the various methods of manipulating strings, covering things from basic methods to regular expressions in Python. String manipulation is a skill that every Python programmer should be familiar with.</P>
<P><B>String Methods</B></P>
<P>The most basic way to manipulate strings is through the methods that are build into them. We can perform a limited number of tasks to strings through these methods. Open up the Python interactive interpreter. Let's create a string and play around with it a bit.</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; test = 'This is just a simple string.'</FONT></P></BLOCKQUOTE>
<P>Let's take a fast detour&nbsp;and use the <I>len</I> function. It can be used to find the length of a string. I'm not sure why it's a function rather than a method, but that's a whole nother issue:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; len ( test )<BR></FONT><FONT face="courier new, courier, mono">29</FONT></P></BLOCKQUOTE>
<P>All right, now let's get back to those methods I was talking about. Let's take our string and replace a word using the <I>replace</I> method:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; test = test.replace ( 'simple', 'short' )<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; testa<BR></FONT><FONT face="courier new, courier, mono">'This is just a short string.'</FONT></P></BLOCKQUOTE>
<P>Now let's count the number of times a given word, or, in this case, character, appears in a string:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; test.count ( 'r' )<BR></FONT><FONT face="courier new, courier, mono">2</FONT></P></BLOCKQUOTE>
<P>We can find characters or words, too:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; test.find ( 'r' )<BR></FONT><FONT face="courier new, courier, mono">18<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; test [ 18 ]<BR></FONT><FONT face="courier new, courier, mono">'r'<BR><BR></P>
<P>Splitting a string is something I find myself doing often. The <I>split</I> method is used for this:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; test.split()<BR></FONT><FONT face="courier new, courier, mono">['This', 'is', 'just', 'a', 'short', 'string.']</FONT></P></BLOCKQUOTE>
<P>We can choose the point that we split it at:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; test.split ( 'a' )<BR></FONT><FONT face="courier new, courier, mono">['This is just ', ' short string.']</FONT></P></BLOCKQUOTE>
<P>Rejoining our split string can be done using the <I>join</I> method:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; ' some '.join ( test.split ( 'a' ) )<BR></FONT><FONT face="courier new, courier, mono">'This is just&nbsp; some&nbsp; short string.'</FONT></P></BLOCKQUOTE>
<P>We can play around with the case of letters in our string, too. Let's make it all upper case:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; test.upper()<BR></FONT><FONT face="courier new, courier, mono">'THIS IS JUST A SHORT STRING.'</FONT></P></BLOCKQUOTE>
<P>Now let's make it lowercase:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; test.lower()<BR></FONT><FONT face="courier new, courier, mono">'this is just a short string.'</FONT></P></BLOCKQUOTE>
<P>Let's capitalize only the first letter of the lowercase string:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; test.lower().capitalize()<BR></FONT><FONT face="courier new, courier, mono">'This is just a short string.'</FONT></P></BLOCKQUOTE>
<P>We can also use the <I>title</I> method. This capitalizes the first letter in each word:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; test.title()<BR></FONT><FONT face="courier new, courier, mono">'This Is Just A Short String.'</FONT></P></BLOCKQUOTE>
<P>Trading case is possible:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; test.swapcase()<BR></FONT><FONT face="courier new, courier, mono">'tHIS IS JUST A SHORT STRING.'</FONT></P></BLOCKQUOTE>
<P>We can run a number of tests on strings using a few methods. Let's check to see whether a given string is all upper case:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; 'UPPER'.isupper()<BR></FONT><FONT face="courier new, courier, mono">True<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; 'UpPEr'.isupper()<BR></FONT><FONT face="courier new, courier, mono">False</FONT></P></BLOCKQUOTE>
<P>Likewise, we can check to see whether&nbsp;a string contains only lower case characters:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; 'lower'.islower()<BR></FONT><FONT face="courier new, courier, mono">True<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; 'Lower'.islower()<BR></FONT><FONT face="courier new, courier, mono">False</FONT></P></BLOCKQUOTE>
<P>Checking whether a string looks like a title is simple, too:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; 'This Is A Title'.istitle()<BR></FONT><FONT face="courier new, courier, mono">True<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; 'This is A title'.istitle()<BR></FONT><FONT face="courier new, courier, mono">False<BR><BR></P>
<P>We can check whether a string is alphanumeric:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; 'aa44'.isalnum()<BR></FONT><FONT face="courier new, courier, mono">True<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; 'a$44'.isalnum()<BR></FONT><FONT face="courier new, courier, mono">False</FONT></P></BLOCKQUOTE>
<P>It is also possible to check whether a string contains only letters:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; 'letters'.isalpha()<BR></FONT><FONT face="courier new, courier, mono">True<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; 'letters4'.isalpha()<BR></FONT><FONT face="courier new, courier, mono">False</FONT></P></BLOCKQUOTE>
<P>Here's how you check whether a string contains only numbers:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; '306090'.isdigit()<BR></FONT><FONT face="courier new, courier, mono">True<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; '30-60-90 Triangle'.isdigit()<BR></FONT><FONT face="courier new, courier, mono">False</FONT></P></BLOCKQUOTE>
<P>We can also check whether a string only contains spaces:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; '&nbsp;&nbsp; '.isspace()<BR></FONT><FONT face="courier new, courier, mono">True<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; ''.isspace()<BR></FONT><FONT face="courier new, courier, mono">False</FONT></P></BLOCKQUOTE>
<P>Speaking of spaces, we can add spaces on either side of a string. Let's add spaces to the right of a string:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; 'A string.'.ljust ( 15 )<BR></FONT><FONT face="courier new, courier, mono">'A string.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '</FONT></P></BLOCKQUOTE>
<P>To add spaces to the left of a string, the <I>rjust</I> method is used:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; 'A string.'.rjust ( 15 )<BR></FONT><FONT face="courier new, courier, mono">'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; A string.'</FONT></P></BLOCKQUOTE>
<P>The center method is used to center a string in spaces:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; 'A string.'.center ( 15 )<BR></FONT><FONT face="courier new, courier, mono">'&nbsp;&nbsp; A string.&nbsp;&nbsp; '</FONT></P></BLOCKQUOTE>
<P>We can strip spaces on either side of a string:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; 'String.'.rjust ( 15 ).strip()<BR></FONT><FONT face="courier new, courier, mono">'String.'<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; 'String.'.ljust ( 15 ).rstrip()<BR></FONT><FONT face="courier new, courier, mono">'String.'<BR><BR></P>
<P>Regular expressions are a very powerful tool in any language. They allow patterns to be matched against strings. Actions such as replacement can be performed on the string if the regular expression pattern matches. Python's module for regular expressions is the <I>re</I> module. Open the Python interactive interpreter, and let's take a closer look at regular expressions and the <I>re</I> module:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; import re</FONT></P></BLOCKQUOTE>
<P>Let's create a simple string we can use to play around with:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; test = 'This is for testing regular expressions in Python.'</FONT></P></BLOCKQUOTE>
<P>I spoke of matching special patterns with regular expressions, but let's start with matching a simple string just to get used to regular expressions. There are two methods for matching patterns in strings in the <I>re</I> module: <I>search</I> and <I>match</I>. Let's take a look at <I>search</I> first. It works like this:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; result = re.search ( 'This', test )</FONT></P></BLOCKQUOTE>
<P>We can extract the results using the <I>group</I> method:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; result.group ( 0 )<BR></FONT><FONT face="courier new, courier, mono">'This'</FONT></P></BLOCKQUOTE>
<P>You're probably wondering about the <I>group</I> method right now and why we pass zero to it. It's simple, and I'll explain. You see, patterns can be organized into groups, like this:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; result = re.search ( '(Th)(is)', test )</FONT></P></BLOCKQUOTE>
<P>There are two groups surrounded by parenthesis. We can extract them using the <I>group</I> method:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; result.group ( 1 )<BR></FONT><FONT face="courier new, courier, mono">'Th'<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result.group ( 2 )<BR></FONT><FONT face="courier new, courier, mono">'is'</FONT></P></BLOCKQUOTE>
<P>Passing zero to the method returns both of the groups:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; result.group ( 0 )<BR></FONT><FONT face="courier new, courier, mono">'This'</FONT></P></BLOCKQUOTE>
<P>The benefit of groups will become more clear once we work our way into actual patterns. First, though, let's take a look at the match function. It works similarly, but there is a crucial difference:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; result =&nbsp; re.match ( 'This', test )<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; print result<BR></FONT><FONT face="courier new, courier, mono">&lt;_sre.SRE_Match object at 0x00994250&gt;<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; print result.group ( 0 )<BR></FONT><FONT face="courier new, courier, mono">'This'<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result = re.match ( 'regular', test )<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; print result<BR></FONT><FONT face="courier new, courier, mono">None</FONT></P></BLOCKQUOTE>
<P>Notice that <I>None</I> was returned, even though “regular” is in the string. If you haven't figured it out, the <I>match</I> method matches patterns at the beginning of the string, and the <I>search</I> function examines the whole string. You might be wondering if it's possible, then, to make the <I>match</I> method match “regular,” since it's not at the beginning of the string. The answer is yes. It's possible to match it, and that brings us into patterns.</P>
<P>The character “.” will match any character. We can get the <I>match</I> method to match “regular” by putting a period for every letter before it. Let's split this up into two groups as well. One will contain the periods, and one will contain “regular”:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; result = re.match ( '(....................)(regular)', test )<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result.group ( 0 )<BR></FONT><FONT face="courier new, courier, mono">'This is for testing regular'<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result.group ( 1 )<BR></FONT><FONT face="courier new, courier, mono">'This is for testing '<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result.group ( 2 )<BR></FONT><FONT face="courier new, courier, mono">'regular'</FONT></P></BLOCKQUOTE>
<P>Aha! We matched it! However, it's ridiculous to have to type in all those periods. The good news is that we don't have to do that. Take a look at this and remember that there are twenty characters before “regular”:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; result = re.match ( '(.{20})(regular)', test )<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result.group ( 0 )<BR></FONT><FONT face="courier new, courier, mono">'This is for testing regular'<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result.group ( 1 )<BR></FONT><FONT face="courier new, courier, mono">'This is for testing '<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result.group ( 2 )<BR></FONT><FONT face="courier new, courier, mono">'regular'</FONT></P></BLOCKQUOTE>
<P>That's a lot easier. Now let's look at a few more patterns. Here's how you can use brackets in a more advanced way:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; result = re.match ( '(.{10,20})(regular)', test )<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result.group ( 0 )<BR></FONT><FONT face="courier new, courier, mono">'This is for testing regular'<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result = re.match ( '(.{10,20})(testing)', test )<BR></FONT><FONT face="courier new, courier, mono">'This is for testing'</FONT></P></BLOCKQUOTE>
<P>By entering two arguments, so to speak, you can match any number of characters in a range. In this case, that range is 10-20. Sometimes, however, this can cause undesired behavior. Take a look at this string:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; anotherTest = 'a cat, a dog, a goat, a person'</FONT></P></BLOCKQUOTE>
<P>Let's match a range of characters:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; result = re.match ( '(.{5,20})(,)', anotherTest )<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result.group ( 1 )<BR></FONT><FONT face="courier new, courier, mono">'a cat, a dog, a goat'</FONT></P></BLOCKQUOTE>
<P>What if we only want “a cat” though? This can be done with appending “?” to the end of the brackets:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; result = re.match ( '(.{5,20}?)(,)', anotherTest )<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result.group ( 1 )<BR></FONT><FONT face="courier new, courier, mono">'a cat'</FONT></P></BLOCKQUOTE>
<P>Appending a question mark to something makes it match as few characters as possible. A question mark that does that, though, is not to be confused with this pattern:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; anotherTest = '012345'<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result = re.match ( '01?', anotherTest )<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result.group ( 0 )<BR></FONT><FONT face="courier new, courier, mono">'01'<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result = re.match ( '0123456?', anotherTest )<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result.group ( 0 )<BR></FONT><FONT face="courier new, courier, mono">'012345'</FONT></P></BLOCKQUOTE>
<P>As you can see with the example, the character before a question mark is optional. Next&nbsp;is the “*” pattern. It matches one or more of the characters it follows, like this:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; anotherTest = 'Just a silly string.'<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result = re.match ( '(.*)(a)(.*)(string)', anotherTest )<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result.group ( 0 )<BR></FONT><FONT face="courier new, courier, mono">'Just a silly string'</FONT></P></BLOCKQUOTE>
<P>However, take a look at this:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; anotherTest = 'Just a silly string. A very silly string.'<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result = re.match ( '(.*)(a)(.*)(string)', anotherTest )<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result.group ( 0 )<BR></FONT><FONT face="courier new, courier, mono">'Just a silly string. A very silly string'</FONT></P></BLOCKQUOTE>
<P>What if, however, we want to only match the first sentence? If you've been following along closely, you'll know that “?” will, again, do the trick:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; result = re.match ( '(.*?)(a)(.*?)(string)', anotherTest )<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result.group ( 0 )<BR></FONT><FONT face="courier new, courier, mono">'Just a silly string'</FONT></P></BLOCKQUOTE>
<P>As I mentioned earlier, though, “*” doesn't have to match anything:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; result = re.match ( '(.*?)(01)', anotherTest )<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result.group ( 0 )<BR></FONT><FONT face="courier new, courier, mono">'01'</FONT></P></BLOCKQUOTE>
<P>What if we want to skip past the first two characters? This is possible by using “+”, which is similar to “*”, except that it matches at least one character:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; result = re.match ( '(.+?)(01)', anotherTest )<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result.group ( 0 )<BR></FONT><FONT face="courier new, courier, mono">'0101'</FONT></P></BLOCKQUOTE>
<P>We can also match a range of characters. For example, we can match only the first four letters of the alphabet:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; anotherTest = 'a101'<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result = re.match ( '[a-d]', anotherTest )<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; print result<BR></FONT><FONT face="courier new, courier, mono">&lt;_sre.SRE_Match object at 0x00B47B10&gt;<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; anotherTest = 'q101'<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result = re.match ( '[a-d]', anotherTest )<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; print result<BR></FONT><FONT face="courier new, courier, mono">None</FONT></P></BLOCKQUOTE>
<P>We can also match one of a few patterns using “|”::</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; testA = 'a'<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; testB = 'b'<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result = re.match ( '(a|b)', testA )<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; print result<BR></FONT><FONT face="courier new, courier, mono">&lt;_sre.SRE_Match object at 0x00B46D60&gt;<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result = re.match ( '(a|b)', testB )<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; print result<BR></FONT><FONT face="courier new, courier, mono">&lt;_sre.SRE_Match object at 0x00B46E60&gt;</FONT></P></BLOCKQUOTE>
<P>Finally, there are a number of special sequences. “\A” matches at the start of a string. “\Z” matches at the end of a string. “\d” matches a digit. “\D” matches anything but a digit. “\s” matches whitespace. “\S” matches anything but whitespace.</P>
<P><FONT face="Verdana, Arial, Helvetica, sans-serif">We can name our groups:</FONT></P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; nameTest = 'hot sauce'<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result = re.match ( '(?P&lt;one&gt;hot)', nameTest )<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result.group ( 'one' )<BR></FONT><FONT face="courier new, courier, mono">'hot'</FONT></P></BLOCKQUOTE>
<P>We can compile patterns to use them multiple times with the <I>re</I> module, too:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; ourPattern = re.compile ( '(.*?)(the)' )<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; testString = 'This is the dog and the cat.'<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result = ourPattern.match ( testString )<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; result.group ( 0 )<BR></FONT><FONT face="courier new, courier, mono">'This is the'</FONT></P></BLOCKQUOTE>
<P>Of course, you can do more than match and extract substrings. You can replace things, too:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="courier new, courier, mono">&gt;&gt;&gt; someString = 'I have a dream.'<BR></FONT><FONT face="courier new, courier, mono">&gt;&gt;&gt; re.sub ( 'dream', 'dog', someString )<BR></FONT><FONT face="courier new, courier, mono">'I have a dog.'</FONT></P></BLOCKQUOTE>
<P>On a final note, you should not use regular expressions to match or replace simple strings.</P>
<P><B>Conclusion</B></P>
<P>Now you have a basic knowledge of string manipulation in Python behind you. As I explained at the very beginning of the article, string manipulation is necessary to many applications, both large and small. It is used frequently, and a basic knowledge of it is critical.<BR><BR>from:http://www.devshed.com/c/a/Python/String-Manipulation/</P></FONT></BLOCKQUOTE></FONT></BLOCKQUOTE></FONT></BLOCKQUOTE><img src ="http://www.blogjava.net/weidagang2046/aggbug/20523.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/weidagang2046/" target="_blank">weidagang2046</a> 2005-11-19 00:31 <a href="http://www.blogjava.net/weidagang2046/articles/20523.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>