<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3580963069839202358</id><updated>2024-10-04T19:07:56.151-07:00</updated><category term="python newbie"/><category term="python split"/><category term="python file open"/><category term="python for loop"/><category term="python list"/><category term="python readlines"/><category term="python startswith"/><category term="python strip"/><category term="python functions"/><category term="python if"/><category term="python write file"/><category term="python join"/><category term="python rstrip"/><category term="python command line"/><category term="python dictionaries"/><category term="python len"/><category term="python sys module"/><category term="python os module"/><category term="python lambda"/><category term="python range"/><category term="python string methods"/><category term="python time module"/><category term="python break"/><category term="python enumerate"/><category term="python exceptions"/><category term="python file close"/><category term="python file read"/><category term="python str"/><category term="python strftime function"/><category term="python ValueError"/><category term="python argv"/><category term="python capitalize"/><category term="python continue"/><category term="python create directory"/><category term="python datetime module"/><category term="python dictionaries iteritems"/><category term="python dictionary append"/><category term="python dictionary setdefault"/><category term="python elif"/><category term="python factorial"/><category term="python file objects"/><category term="python file readline"/><category term="python filter"/><category term="python float"/><category term="python getopt"/><category term="python glob"/><category term="python has_key"/><category term="python ip address"/><category term="python isdigit"/><category term="python list append"/><category term="python list pop"/><category term="python list sort"/><category term="python lowercase"/><category term="python map"/><category term="python math module"/><category term="python readline"/><category term="python recursive function"/><category term="python reduce"/><category term="python regular expression"/><category term="python rename"/><category term="python replace"/><category term="python socket module"/><category term="python stdin"/><category term="python swapcase"/><category term="python system"/><category term="python timedelta"/><category term="python titlecase"/><category term="python tuple"/><category term="python uppercase"/><category term="python2.6"/><title type='text'>Python basics for newbies</title><subtitle type='html'>Python basics with practical examples</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://pythonstarter.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://pythonstarter.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>43</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3580963069839202358.post-5567484092048986142</id><published>2012-06-28T06:14:00.000-07:00</published><updated>2012-06-28T06:14:40.983-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python list"/><category scheme="http://www.blogger.com/atom/ns#" term="python newbie"/><category scheme="http://www.blogger.com/atom/ns#" term="python range"/><title type='text'>Python - unpack arguments from list with star operator</title><content type='html'>We can use *-operator (&lt;a href=&quot;http://docs.python.org/tutorial/controlflow.html#unpacking-argument-lists&quot;&gt;star operator&lt;/a&gt;) to unpack the arguments out of a list or tuple. Here is an example with python built in &lt;a href=&quot;http://docs.python.org/library/functions.html#range&quot;&gt;range()&lt;/a&gt; function :
&lt;pre&gt;
&gt;&gt;&gt; mylist = [3, 10]
&gt;&gt;&gt; mylist
[3, 10]
&gt;&gt;&gt; range(mylist[0], mylist[1])
[3, 4, 5, 6, 7, 8, 9]
&gt;&gt;&gt; range(*mylist)
[3, 4, 5, 6, 7, 8, 9]
&gt;&gt;&gt; 
&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://pythonstarter.blogspot.com/feeds/5567484092048986142/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3580963069839202358/5567484092048986142?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/5567484092048986142'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/5567484092048986142'/><link rel='alternate' type='text/html' href='http://pythonstarter.blogspot.com/2012/06/python-unpack-arguments-from-list-with.html' title='Python - unpack arguments from list with star operator'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3580963069839202358.post-2064691685599339074</id><published>2011-01-07T07:16:00.000-08:00</published><updated>2011-01-07T07:58:08.018-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python enumerate"/><category scheme="http://www.blogger.com/atom/ns#" term="python file open"/><category scheme="http://www.blogger.com/atom/ns#" term="python join"/><category scheme="http://www.blogger.com/atom/ns#" term="python list"/><category scheme="http://www.blogger.com/atom/ns#" term="python list pop"/><category scheme="http://www.blogger.com/atom/ns#" term="python startswith"/><title type='text'>Python merge line with line above</title><content type='html'>&lt;span style=&quot;font-weight: bold;&quot;&gt;Input file:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; cat file.txt&lt;br /&gt;500:120:100:X&lt;br /&gt;:100:120&lt;br /&gt;200:900:125&lt;br /&gt;120:120&lt;br /&gt;:900&lt;br /&gt;120:345&lt;br /&gt;12:900:1234:34&lt;br /&gt;:90&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Required:&lt;/span&gt; Join the lines which startswith : with the previous line.&lt;br /&gt;i.e. required output is:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;500:120:100:X:100:120&lt;br /&gt;200:900:125&lt;br /&gt;120:120:900&lt;br /&gt;120:345&lt;br /&gt;12:900:1234:34:90&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The python script to achieve this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;data=open(&quot;file.txt&quot;).read().split(&quot;\n&quot;)&lt;br /&gt;for i,line in enumerate(data):&lt;br /&gt;    if line.startswith(&quot;:&quot;):&lt;br /&gt;        data[i-1]= data[i-1]+line&lt;br /&gt;        data.pop(i)&lt;br /&gt;print &#39;\n&#39;.join(data),&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Executing it:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; python merge-lines.py&lt;br /&gt;500:120:100:X:100:120&lt;br /&gt;200:900:125&lt;br /&gt;120:120:900&lt;br /&gt;120:345&lt;br /&gt;12:900:1234:34:90&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The solution using UNIX Awk can be found &lt;a href=&quot;http://unstableme.blogspot.com/2008/07/merging-lines-using-awk-bash.html&quot;&gt;here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;More about python enumerate function can be found &lt;a href=&quot;http://docs.python.org/library/functions.html#enumerate&quot;&gt;here&lt;/a&gt;. Mentioned below is a small example on python enumerate function&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&gt;&gt;&gt; for i, student in enumerate([&#39;Alex&#39;, &#39;Ryan&#39;, &#39;Deb&#39;]):&lt;br /&gt;...     print i, student&lt;br /&gt;...&lt;br /&gt;0 Alex&lt;br /&gt;1 Ryan&lt;br /&gt;2 Deb&lt;br /&gt;&gt;&gt;&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Related Posts:&lt;/span&gt;&lt;br /&gt;- &lt;a href=&quot;http://pythonstarter.blogspot.com/2009/10/python-print-section-of-file-using-line.html&quot;&gt;Print section of file using line number - Python&lt;/a&gt;&lt;br /&gt;- &lt;a href=&quot;http://pythonstarter.blogspot.com/2009/11/print-line-next-to-pattern-in-python.html&quot;&gt;Print line next to pattern using Python&lt;/a&gt;&lt;br /&gt;- &lt;a href=&quot;http://pythonstarter.blogspot.com/2009/10/print-line-above-pattern-in-python.html&quot;&gt;Print line above pattern using Python&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;New learning:&lt;/span&gt;&lt;br /&gt;Python list pop method:&lt;br /&gt;list.pop([i])&lt;br /&gt;Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list.</content><link rel='replies' type='application/atom+xml' href='http://pythonstarter.blogspot.com/feeds/2064691685599339074/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3580963069839202358/2064691685599339074?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/2064691685599339074'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/2064691685599339074'/><link rel='alternate' type='text/html' href='http://pythonstarter.blogspot.com/2011/01/python-merge-line-with-line-above.html' title='Python merge line with line above'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3580963069839202358.post-7182693277792389913</id><published>2010-12-26T21:20:00.000-08:00</published><updated>2010-12-26T21:28:48.291-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python exceptions"/><category scheme="http://www.blogger.com/atom/ns#" term="python file close"/><category scheme="http://www.blogger.com/atom/ns#" term="python for loop"/><category scheme="http://www.blogger.com/atom/ns#" term="python join"/><category scheme="http://www.blogger.com/atom/ns#" term="python list"/><category scheme="http://www.blogger.com/atom/ns#" term="python list append"/><category scheme="http://www.blogger.com/atom/ns#" term="python readlines"/><category scheme="http://www.blogger.com/atom/ns#" term="python rstrip"/><category scheme="http://www.blogger.com/atom/ns#" term="python split"/><category scheme="http://www.blogger.com/atom/ns#" term="python startswith"/><category scheme="http://www.blogger.com/atom/ns#" term="python ValueError"/><title type='text'>Python list append example - divide by two</title><content type='html'>Input file:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; cat file.txt&lt;br /&gt;h1|u|1&lt;br /&gt;h2|5|1|1&lt;br /&gt;rec1|1239400800|Sat|fan1|AX|2|10035|-|2|50&lt;br /&gt;rec2|1239400800|Sat|fan1|AX|2|-|-|2|17&lt;br /&gt;rec5|1239400801|Sat|fan3|AY|5|10035|-|2|217&lt;br /&gt;rec8|1239400804|Sat|fan5|AX|2|5|-|2|970&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Required Output:&lt;br /&gt;- Lines starting with &quot;h1&quot; or &quot;h2&quot;, no action required, just print.&lt;br /&gt;- Lines starting with &quot;rec&quot;, divide the values starting from 6th field by 2.&lt;br /&gt;&lt;br /&gt;Required output is:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;h1|u|1&lt;br /&gt;h2|5|1|1&lt;br /&gt;rec1|1239400800|Sat|fan1|AX|1|5017|-|1|25&lt;br /&gt;rec2|1239400800|Sat|fan1|AX|1|-|-|1|8&lt;br /&gt;rec5|1239400801|Sat|fan3|AY|2|5017|-|1|108&lt;br /&gt;rec8|1239400804|Sat|fan5|AX|1|2|-|1|485&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The python script:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;fp = open(&quot;file.txt&quot;, &quot;rU&quot;)&lt;br /&gt;lines = fp.readlines()&lt;br /&gt;fp.close()&lt;br /&gt;&lt;br /&gt;for line in lines:&lt;br /&gt;       if line.startswith(&quot;h1&quot;):&lt;br /&gt;               print line,&lt;br /&gt;       if line.startswith(&quot;h2&quot;):&lt;br /&gt;               print line,&lt;br /&gt;       if line.startswith(&quot;rec&quot;):&lt;br /&gt;               f=line.split(&quot;|&quot;)&lt;br /&gt;               r = f[5:]&lt;br /&gt;               l = []&lt;br /&gt;               for each in r:&lt;br /&gt;                       try:&lt;br /&gt;                               l.append(str(int(each)/2))&lt;br /&gt;                       except ValueError:&lt;br /&gt;                               l.append(each)&lt;br /&gt;&lt;br /&gt;               t = &quot;|&quot;.join(f[0:5]) + &quot;|&quot; + &quot;|&quot;.join(l)&lt;br /&gt;               print t.rstrip()&lt;br /&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://pythonstarter.blogspot.com/feeds/7182693277792389913/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3580963069839202358/7182693277792389913?isPopup=true' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/7182693277792389913'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/7182693277792389913'/><link rel='alternate' type='text/html' href='http://pythonstarter.blogspot.com/2010/12/python-list-append-example-divide-by.html' title='Python list append example - divide by two'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3580963069839202358.post-4839063256504981381</id><published>2010-12-08T21:05:00.000-08:00</published><updated>2010-12-08T21:12:22.597-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python argv"/><category scheme="http://www.blogger.com/atom/ns#" term="python dictionaries"/><category scheme="http://www.blogger.com/atom/ns#" term="python file open"/><category scheme="http://www.blogger.com/atom/ns#" term="python join"/><category scheme="http://www.blogger.com/atom/ns#" term="python list"/><category scheme="http://www.blogger.com/atom/ns#" term="python split"/><category scheme="http://www.blogger.com/atom/ns#" term="python startswith"/><category scheme="http://www.blogger.com/atom/ns#" term="python strip"/><category scheme="http://www.blogger.com/atom/ns#" term="python sys module"/><title type='text'>Python - Replace based on another file</title><content type='html'>&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; cat main.txt&lt;br /&gt;P|34|90&lt;br /&gt;T|12&lt;br /&gt;R|0|1291870414|ip1|890&lt;br /&gt;R|1|1291870415|ip5|690&lt;br /&gt;R|2|1291870415|ip1|899&lt;br /&gt;R|3|1291870412|ip2|896&lt;br /&gt;R|4|1291870418|ip3|999&lt;br /&gt;R|5|1291870419|ip5|191&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; cat lookup.txt&lt;br /&gt;ip7|172.17.4.8&lt;br /&gt;ip1|172.17.4.3&lt;br /&gt;ip5|172.17.4.9&lt;br /&gt;ip4|172.17.4.2&lt;br /&gt;ip3|172.17.4.1&lt;br /&gt;ip2|172.17.4.6&lt;br /&gt;ip6|172.17.4.7&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Required Output:&lt;br /&gt;Replace the 4th field (pipe delimited) of the &#39;R&#39; lines of &#39;main.txt&#39; with the corresponding lookup value from &#39;lookup.txt&#39;  i.e. &#39;ip1&#39; to be replaced with &#39;172.17.4.3&#39;, &#39;ip2&#39; with &#39;172.17.4.6&#39; etc.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;P|34|90&lt;br /&gt;T|12&lt;br /&gt;R|0|1291870414|172.17.4.3|890&lt;br /&gt;R|1|1291870415|172.17.4.9|690&lt;br /&gt;R|2|1291870415|172.17.4.3|899&lt;br /&gt;R|3|1291870412|172.17.4.6|896&lt;br /&gt;R|4|1291870418|172.17.4.1|999&lt;br /&gt;R|5|1291870419|172.17.4.9|191&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The python script:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;import sys&lt;br /&gt;d={}&lt;br /&gt;for line in open(&quot;lookup.txt&quot;):&lt;br /&gt;    line=line.strip().split(&quot;|&quot;)&lt;br /&gt;    d[line[0]]=line[-1]&lt;br /&gt;for line in open(sys.argv[1]):&lt;br /&gt;    if line.startswith(&#39;P&#39;):&lt;br /&gt;        print line,&lt;br /&gt;    if line.startswith(&#39;T&#39;):&lt;br /&gt;        print line,&lt;br /&gt;    if line.startswith(&#39;R&#39;):&lt;br /&gt;        line=line.strip().split(&quot;|&quot;)&lt;br /&gt;        print &#39;|&#39;.join(line[0:3])+&#39;|&#39;+d[line[3]]+&#39;|&#39;+&#39;|&#39;.join(line[4:])&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Executing it:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; python replace-from-file.py main.txt&lt;br /&gt;P|34|90&lt;br /&gt;T|12&lt;br /&gt;R|0|1291870414|172.17.4.3|890&lt;br /&gt;R|1|1291870415|172.17.4.9|690&lt;br /&gt;R|2|1291870415|172.17.4.3|899&lt;br /&gt;R|3|1291870412|172.17.4.6|896&lt;br /&gt;R|4|1291870418|172.17.4.1|999&lt;br /&gt;R|5|1291870419|172.17.4.9|191&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Related Posts:&lt;br /&gt;- &lt;a href=&quot;http://pythonstarter.blogspot.com/2009/05/lookup-file-operation-using-python.html&quot;&gt;Lookup file operation using Python&lt;/a&gt;&lt;br /&gt;- &lt;a href=&quot;http://pythonstarter.blogspot.com/2009/05/lookup-file-in-python-using-dictionary.html&quot;&gt;Lookup file in python using Dictionary&lt;/a&gt;&lt;br /&gt;- &lt;a href=&quot;http://pythonstarter.blogspot.com/2010/06/simple-python-file-lookup-function-for.html&quot;&gt;Simple python file lookup function&lt;/a&gt;&lt;br /&gt;- &lt;a href=&quot;http://pythonstarter.blogspot.com/2009/09/find-text-string-in-file-in-python.html&quot;&gt;Find text string in file in Python&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://pythonstarter.blogspot.com/feeds/4839063256504981381/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3580963069839202358/4839063256504981381?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/4839063256504981381'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/4839063256504981381'/><link rel='alternate' type='text/html' href='http://pythonstarter.blogspot.com/2010/12/python-replace-based-on-another-file.html' title='Python - Replace based on another file'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3580963069839202358.post-8581139899190871397</id><published>2010-11-17T20:23:00.000-08:00</published><updated>2010-11-17T20:29:07.528-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python file open"/><category scheme="http://www.blogger.com/atom/ns#" term="python file read"/><category scheme="http://www.blogger.com/atom/ns#" term="python for loop"/><category scheme="http://www.blogger.com/atom/ns#" term="python lambda"/><category scheme="http://www.blogger.com/atom/ns#" term="python list"/><category scheme="http://www.blogger.com/atom/ns#" term="python list sort"/><category scheme="http://www.blogger.com/atom/ns#" term="python newbie"/><category scheme="http://www.blogger.com/atom/ns#" term="python strip"/><title type='text'>Python sort file based on last field</title><content type='html'>Input file:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; cat file.txt&lt;br /&gt;IN,90,453&lt;br /&gt;US,12,1,120&lt;br /&gt;NZ,89,200&lt;br /&gt;WI,500&lt;br /&gt;TS,12,124&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Required output: Sort the above comma delimited file based on the last field (column). i.e. required output:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;US,12,1,120&lt;br /&gt;TS,12,124&lt;br /&gt;NZ,89,200&lt;br /&gt;IN,90,453&lt;br /&gt;WI,500&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Solution:&lt;br /&gt;The solution using Awk in UNIX bash shell is &lt;a href=&quot;http://unstableme.blogspot.com/2010/11/sort-file-based-on-last-field-unix.html&quot;&gt;here&lt;/a&gt;. And here is the python one:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; python&lt;br /&gt;Python 2.5.2 (r252:60911, Jan 20 2010, 21:48:48)&lt;br /&gt;[GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)] on linux2&lt;br /&gt;Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.&lt;br /&gt;&gt;&gt;&gt; d_list = [line.strip() for line in open(&quot;file.txt&quot;)]&lt;br /&gt;&gt;&gt;&gt; d_list&lt;br /&gt;[&#39;IN,90,453&#39;, &#39;US,12,1,120&#39;, &#39;NZ,89,200&#39;, &#39;WI,500&#39;, &#39;TS,12,124&#39;]&lt;br /&gt;&gt;&gt;&gt; d_list.sort(key = lambda line: line.split(&quot;,&quot;)[-1])&lt;br /&gt;&gt;&gt;&gt; d_list&lt;br /&gt;[&#39;US,12,1,120&#39;, &#39;TS,12,124&#39;, &#39;NZ,89,200&#39;, &#39;IN,90,453&#39;, &#39;WI,500&#39;]&lt;br /&gt;&gt;&gt;&gt; for line in d_list:&lt;br /&gt;...     print line&lt;br /&gt;...&lt;br /&gt;US,12,1,120&lt;br /&gt;TS,12,124&lt;br /&gt;NZ,89,200&lt;br /&gt;IN,90,453&lt;br /&gt;WI,500&lt;br /&gt;&gt;&gt;&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Some notes:&lt;br /&gt;&lt;span style=&quot;font-style: italic; font-weight: bold;&quot;&gt;Accessing last element of a list in python:&lt;/span&gt;&lt;br /&gt;A negative index accesses elements from the end of the list counting backwards. The last element of any non-empty list is always list[-1].</content><link rel='replies' type='application/atom+xml' href='http://pythonstarter.blogspot.com/feeds/8581139899190871397/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3580963069839202358/8581139899190871397?isPopup=true' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/8581139899190871397'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/8581139899190871397'/><link rel='alternate' type='text/html' href='http://pythonstarter.blogspot.com/2010/11/python-sort-file-based-on-last-field.html' title='Python sort file based on last field'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3580963069839202358.post-2526282191088718897</id><published>2010-08-30T22:39:00.000-07:00</published><updated>2010-08-30T22:44:14.234-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python newbie"/><category scheme="http://www.blogger.com/atom/ns#" term="python2.6"/><title type='text'>bsddb185 sunaudiodev - Python 2.6 Ubuntu installation</title><content type='html'>If you are arriving on this page looking for the solution of following error message during python2.6 installation (make) on your Ubuntu:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Failed to find the necessary bits to build these modules:&lt;br /&gt;bsddb185           sunaudiodev                       &lt;br /&gt;To find the necessary bits, look in setup.py in detect_modules() for the module&#39;s name.&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;then here is the solution:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; wget http://www.lysium.de/sw/python2.6-disable-old-modules.patch&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; patch -p1 &lt; python2.6-disable-old-modules.patch&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;For a complete guide to install python 2.6 on your Ubuntu, you can check this &lt;a href=&quot;http://www.lysium.de/blog/index.php?/archives/229-Installing-Python-2.6-on-Ubuntu-8.04.html&quot;&gt;page&lt;/a&gt;, its really useful.</content><link rel='replies' type='application/atom+xml' href='http://pythonstarter.blogspot.com/feeds/2526282191088718897/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3580963069839202358/2526282191088718897?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/2526282191088718897'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/2526282191088718897'/><link rel='alternate' type='text/html' href='http://pythonstarter.blogspot.com/2010/08/bsddb185-sunaudiodev-python-26-ubuntu.html' title='bsddb185 sunaudiodev - Python 2.6 Ubuntu installation'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3580963069839202358.post-6685414737934203301</id><published>2010-07-09T03:12:00.000-07:00</published><updated>2010-07-09T03:16:55.329-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python dictionaries"/><category scheme="http://www.blogger.com/atom/ns#" term="python list"/><category scheme="http://www.blogger.com/atom/ns#" term="python newbie"/><category scheme="http://www.blogger.com/atom/ns#" term="python readlines"/><category scheme="http://www.blogger.com/atom/ns#" term="python write file"/><title type='text'>Python - Remove duplicate lines from file</title><content type='html'>Objective :  Remove duplicate lines from a file (print first occurrence) which appeared exactly twice.&lt;br /&gt;&lt;br /&gt;Input file:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; cat file.txt&lt;br /&gt;begin&lt;br /&gt;ip 172.17.4.53&lt;br /&gt;line 172.17.4.52&lt;br /&gt;pl 172.17.4.51&lt;br /&gt;pl 172.17.4.51&lt;br /&gt;new 172.17.4.52&lt;br /&gt;line 172.17.4.52&lt;br /&gt;pl 172.17.4.51&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Required: Remove duplicate lines from the above file i.e. print only the first occurrence of the lines which appeared exactly twice and for lines those appear more than twice or appeared only once, no action required.&lt;br /&gt;&lt;br /&gt;i.e. Required output should look like this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;begin&lt;br /&gt;ip 172.17.4.53&lt;br /&gt;line 172.17.4.52&lt;br /&gt;pl 172.17.4.51&lt;br /&gt;pl 172.17.4.51&lt;br /&gt;new 172.17.4.52&lt;br /&gt;pl 172.17.4.51&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The python script &#39;remove-duplicate.py&#39; :&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;d = {}&lt;br /&gt;&lt;br /&gt;fp = open(&quot;file.txt.nodup&quot;,&quot;w&quot;)&lt;br /&gt;text_file = open(&quot;file.txt&quot;, &quot;r&quot;)&lt;br /&gt;lines = text_file.readlines()&lt;br /&gt;for line in lines:&lt;br /&gt;    if not line in d.keys():&lt;br /&gt;        d[line] = 0&lt;br /&gt;    d[line] = d[line] + 1&lt;br /&gt;&lt;br /&gt;for line in lines:&lt;br /&gt;    if d[line] == 0:&lt;br /&gt;        continue&lt;br /&gt;    elif d[line] == 2:&lt;br /&gt;        fp.write(line)&lt;br /&gt;        d[line] = 0&lt;br /&gt;    else:&lt;br /&gt;        fp.write(line)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Executing it:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; python remove-duplicate.py&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; cat file.txt.nodup&lt;br /&gt;begin&lt;br /&gt;ip 172.17.4.53&lt;br /&gt;line 172.17.4.52&lt;br /&gt;pl 172.17.4.51&lt;br /&gt;pl 172.17.4.51&lt;br /&gt;new 172.17.4.52&lt;br /&gt;pl 172.17.4.51&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://pythonstarter.blogspot.com/feeds/6685414737934203301/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3580963069839202358/6685414737934203301?isPopup=true' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/6685414737934203301'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/6685414737934203301'/><link rel='alternate' type='text/html' href='http://pythonstarter.blogspot.com/2010/07/python-remove-duplicate-lines-from-file.html' title='Python - Remove duplicate lines from file'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3580963069839202358.post-8005636894191459405</id><published>2010-06-27T20:02:00.000-07:00</published><updated>2010-06-27T20:13:08.524-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python for loop"/><category scheme="http://www.blogger.com/atom/ns#" term="python newbie"/><category scheme="http://www.blogger.com/atom/ns#" term="python os module"/><category scheme="http://www.blogger.com/atom/ns#" term="python readlines"/><category scheme="http://www.blogger.com/atom/ns#" term="python split"/><category scheme="http://www.blogger.com/atom/ns#" term="python startswith"/><category scheme="http://www.blogger.com/atom/ns#" term="python sys module"/><title type='text'>Simple python file lookup function for newbie</title><content type='html'>Config file &#39;&lt;span style=&quot;font-style: italic;&quot;&gt;ip-mapping.txt&lt;/span&gt;&#39; is a file of the following format:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; cat /home/testusr/work/ip-mapping.txt&lt;br /&gt;#id:ip1,ip2,ip3&lt;br /&gt;200:172.17.4.12,172.17.4.14,172.17.4.10&lt;br /&gt;205:172.17.4.14,172.17.4.14,172.17.4.11&lt;br /&gt;210:172.17.4.12,172.17.4.18,172.17.4.18&lt;br /&gt;208:172.17.4.11,172.17.4.10,172.17.4.19&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Required: Create a simple python function which will accept an &#39;id&#39; and will return &#39;ip1&#39; from the list of ips.&lt;br /&gt;&lt;br /&gt;The python script:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;import os,sys&lt;br /&gt;&lt;br /&gt;config = &#39;/home/testusr/work/ip-mapping.txt&#39;&lt;br /&gt;if not os.path.exists(config):&lt;br /&gt;    print config+&#39; file not present&#39;&lt;br /&gt;    sys.exit()&lt;br /&gt;&lt;br /&gt;def getip(id):&lt;br /&gt;   all = open(config).readlines()&lt;br /&gt;   for line in all:&lt;br /&gt;        if line.startswith(&#39;#&#39;):&lt;br /&gt;            continue&lt;br /&gt;        f=line.split(&quot;:&quot;)&lt;br /&gt;        if f[0]==id:&lt;br /&gt;                return f[1].split(&#39;,&#39;)[0]&lt;br /&gt;&lt;br /&gt;ip=getip(&#39;205&#39;)&lt;br /&gt;print ip&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Executing it:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; python get-ip.py&lt;br /&gt;172.17.4.14&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;I am sure there will be much better solutions to this problem, please comment, really appreciated.&lt;br /&gt;&lt;br /&gt;The description about Exit function of &#39;sys&#39; module (&lt;a href=&quot;http://docs.python.org/py3k/library/sys.html#sys.exit&quot;&gt;source&lt;/a&gt;) :&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;sys.exit([arg])&lt;br /&gt;Exit from Python. This is implemented by raising the SystemExit exception, so cleanup actions&lt;br /&gt;specified by finally clauses of try statements are honored, and it is possible to intercept the exit&lt;br /&gt;attempt at an outer level. The optional argument arg can be an integer giving the exit status&lt;br /&gt;(defaulting to zero), or another type of object. If it is an integer, zero is considered “successful&lt;br /&gt;termination” and any nonzero value is considered “abnormal termination” by shells and the&lt;br /&gt;like. Most systems require it to be in the range 0-127, and produce undefined results otherwise.&lt;br /&gt;&lt;br /&gt;Some systems have a convention for assigning specific meanings to specific exit codes, but these&lt;br /&gt;are generally underdeveloped; Unix programs generally use 2 for command line syntax errors&lt;br /&gt;and 1 for all other kind of errors. If another type of object is passed, None is equivalent to&lt;br /&gt;passing zero, and any other object is printed to sys.stderr and results in an exit code of 1. In&lt;br /&gt;particular, sys.exit(&quot;some error message&quot;) is a quick way to exit a program when an error occurs.&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Related posts on lookup on file using python:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;http://pythonstarter.blogspot.com/2009/05/lookup-file-in-python-using-dictionary.html&quot;&gt;Lookup file in python using dictionary&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://pythonstarter.blogspot.com/2009/09/find-text-string-in-file-in-python.html&quot;&gt;Find text string in file using python&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://pythonstarter.blogspot.com/2009/05/lookup-file-operation-using-python.html&quot;&gt;Lookup file operation in python&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;</content><link rel='replies' type='application/atom+xml' href='http://pythonstarter.blogspot.com/feeds/8005636894191459405/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3580963069839202358/8005636894191459405?isPopup=true' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/8005636894191459405'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/8005636894191459405'/><link rel='alternate' type='text/html' href='http://pythonstarter.blogspot.com/2010/06/simple-python-file-lookup-function-for.html' title='Simple python file lookup function for newbie'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3580963069839202358.post-2732937876418889122</id><published>2010-01-31T20:02:00.000-08:00</published><updated>2010-01-31T20:08:26.889-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python len"/><category scheme="http://www.blogger.com/atom/ns#" term="python list"/><category scheme="http://www.blogger.com/atom/ns#" term="python newbie"/><category scheme="http://www.blogger.com/atom/ns#" term="python range"/><category scheme="http://www.blogger.com/atom/ns#" term="python split"/><category scheme="http://www.blogger.com/atom/ns#" term="python startswith"/><category scheme="http://www.blogger.com/atom/ns#" term="python sys module"/><title type='text'>Python - count instances without a specific line</title><content type='html'>&lt;span style=&quot;font-weight: bold;&quot;&gt;Input file:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; cat data.txt&lt;br /&gt;&lt;span style=&quot;color: rgb(153, 51, 153);&quot;&gt;k:begin:0&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(153, 51, 153);&quot;&gt;i:0:66&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(153, 51, 153);&quot;&gt;i:1:76&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(153, 51, 153);&quot;&gt;t:1:143&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(153, 51, 153);&quot;&gt;k:end:0&lt;/span&gt;&lt;br /&gt;k:begin:7&lt;br /&gt;i:0:55&lt;br /&gt;i:1:65&lt;br /&gt;i:2:57&lt;br /&gt;k:end:7&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 102, 0);&quot;&gt;k:begin:2&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 102, 0);&quot;&gt;i:0:10&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 102, 0);&quot;&gt;i:1:0&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 102, 0);&quot;&gt;t:1:10&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 102, 0);&quot;&gt;k:end:7&lt;/span&gt;&lt;br /&gt;k:begin:2&lt;br /&gt;i:0:46&lt;br /&gt;t:0:46&lt;br /&gt;k:end:7&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;k:begin:9&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;i:0:66&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;i:1:56&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;i:2:46&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;i:3:26&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;k:end:7&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Required:&lt;/span&gt; Count total number of instances (one instance being from a &#39;k:begin&#39; to &#39;k:end&#39; line) which do not have a &#39;t&#39; line associated.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;The python program:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;import sys&lt;br /&gt;count=0&lt;br /&gt;data = open(sys.argv[1]).readlines()&lt;br /&gt;for i in range(len(data)):&lt;br /&gt;       if data[i].startswith(&quot;k:end&quot;) and data[i-1].split(&quot;:&quot;)[0]!=&quot;t&quot;:&lt;br /&gt;               count=count+1&lt;br /&gt;print count&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Executing it:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; python count_no_t.py data.txt&lt;br /&gt;2&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Related post:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;- &lt;a href=&quot;http://pythonstarter.blogspot.com/2009/09/print-last-instance-of-file-python.html&quot;&gt;Print last instance of a  file using Python&lt;/a&gt;&lt;br /&gt;- &lt;a href=&quot;http://pythonstarter.blogspot.com/2009/11/print-line-next-to-pattern-in-python.html&quot;&gt;Print line next to pattern in Python&lt;/a&gt;&lt;br /&gt;- &lt;a href=&quot;http://pythonstarter.blogspot.com/2009/10/print-line-above-pattern-in-python.html&quot;&gt;Print line above pattern in Python&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://pythonstarter.blogspot.com/feeds/2732937876418889122/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3580963069839202358/2732937876418889122?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/2732937876418889122'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/2732937876418889122'/><link rel='alternate' type='text/html' href='http://pythonstarter.blogspot.com/2010/01/python-count-instances-without-specific.html' title='Python - count instances without a specific line'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3580963069839202358.post-524014486316703529</id><published>2009-12-22T19:25:00.000-08:00</published><updated>2009-12-22T19:39:16.998-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python functions"/><category scheme="http://www.blogger.com/atom/ns#" term="python list"/><category scheme="http://www.blogger.com/atom/ns#" term="python newbie"/><category scheme="http://www.blogger.com/atom/ns#" term="python tuple"/><title type='text'>Python convert string to tuple &amp; list</title><content type='html'>Let&#39;s check the use of python &#39;tuple&#39; and &#39;list&#39; in-built functions.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;tuple([iterable]) &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;It returns a &#39;tuple&#39; whose items are the same and in the same order as iterable‘s items. iterable may be a sequence, a container that supports iteration, or an iterator object.&lt;br /&gt;&lt;br /&gt;tuple(&#39;xyz&#39;) returns (&#39;x&#39;, &#39;y&#39;, &#39;z&#39;) and tuple([1, 2, 3]) returns (1, 2, 3)&lt;br /&gt;&lt;br /&gt;e.g.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; cat file.txt&lt;br /&gt;Python Prog&lt;br /&gt;Readline&lt;br /&gt;Programming&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&gt;&gt;&gt; for line in open(&quot;file.txt&quot;):&lt;br /&gt;...     t = tuple(line)&lt;br /&gt;...     print t&lt;br /&gt;...&lt;br /&gt;(&#39;P&#39;, &#39;y&#39;, &#39;t&#39;, &#39;h&#39;, &#39;o&#39;, &#39;n&#39;, &#39; &#39;, &#39;P&#39;, &#39;r&#39;, &#39;o&#39;, &#39;g&#39;, &#39;\n&#39;)&lt;br /&gt;(&#39;R&#39;, &#39;e&#39;, &#39;a&#39;, &#39;d&#39;, &#39;l&#39;, &#39;i&#39;, &#39;n&#39;, &#39;e&#39;, &#39;\n&#39;)&lt;br /&gt;(&#39;P&#39;, &#39;r&#39;, &#39;o&#39;, &#39;g&#39;, &#39;r&#39;, &#39;a&#39;, &#39;m&#39;, &#39;m&#39;, &#39;i&#39;, &#39;n&#39;, &#39;g&#39;, &#39;\n&#39;)&lt;br /&gt;&gt;&gt;&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;list([iterable])&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;It returns a list whose items are the same and in the same order as iterable‘s items. iterable may be either a sequence, a container that supports iteration, or an iterator object. If iterable is already a list, a copy is made and returned, similar to iterable[:]. For instance, list(&#39;xyz&#39;) returns [&#39;x&#39;, &#39;y&#39;, &#39;z&#39;] and list( (1, 2, 3) ) returns [1, 2, 3].&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&gt;&gt;&gt;&lt;br /&gt;&gt;&gt;&gt; for line in open(&quot;file.txt&quot;):&lt;br /&gt;...     l = list(line)&lt;br /&gt;...     print l&lt;br /&gt;...&lt;br /&gt;[&#39;P&#39;, &#39;y&#39;, &#39;t&#39;, &#39;h&#39;, &#39;o&#39;, &#39;n&#39;, &#39; &#39;, &#39;P&#39;, &#39;r&#39;, &#39;o&#39;, &#39;g&#39;, &#39;\n&#39;]&lt;br /&gt;[&#39;R&#39;, &#39;e&#39;, &#39;a&#39;, &#39;d&#39;, &#39;l&#39;, &#39;i&#39;, &#39;n&#39;, &#39;e&#39;, &#39;\n&#39;]&lt;br /&gt;[&#39;P&#39;, &#39;r&#39;, &#39;o&#39;, &#39;g&#39;, &#39;r&#39;, &#39;a&#39;, &#39;m&#39;, &#39;m&#39;, &#39;i&#39;, &#39;n&#39;, &#39;g&#39;, &#39;\n&#39;]&lt;br /&gt;&gt;&gt;&gt;&lt;br /&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://pythonstarter.blogspot.com/feeds/524014486316703529/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3580963069839202358/524014486316703529?isPopup=true' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/524014486316703529'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/524014486316703529'/><link rel='alternate' type='text/html' href='http://pythonstarter.blogspot.com/2009/12/python-convert-string-to-tuple-list.html' title='Python convert string to tuple &amp; list'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3580963069839202358.post-4003448986524747089</id><published>2009-12-19T06:06:00.000-08:00</published><updated>2009-12-19T06:14:57.567-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python file open"/><category scheme="http://www.blogger.com/atom/ns#" term="python for loop"/><category scheme="http://www.blogger.com/atom/ns#" term="python if"/><category scheme="http://www.blogger.com/atom/ns#" term="python newbie"/><category scheme="http://www.blogger.com/atom/ns#" term="python split"/><category scheme="http://www.blogger.com/atom/ns#" term="python startswith"/><category scheme="http://www.blogger.com/atom/ns#" term="python strip"/><category scheme="http://www.blogger.com/atom/ns#" term="python write file"/><title type='text'>Split a file into sub files in python</title><content type='html'>Input file &#39;file.txt&#39; is basically a log file containing running information of certain device interfaces in the following format:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; cat file.txt&lt;br /&gt;debug: on&lt;br /&gt;max allowed connection: 3&lt;br /&gt;tr#45&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;Starting: interface 78e23&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;Fan Status: On&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;Speed: -&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;sl no: 3431212-2323-90&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;vendor: aledaia&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;Stopping: interface 78e23&lt;/span&gt;&lt;br /&gt;tr#90&lt;br /&gt;newdebug received&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;Starting: interface 78e24&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;Fan Status: Off&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;Speed: 5670&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;sl no: 3431212-2323-90&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;vendor: aledaia&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;Stopping: interface 78e24&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;Starting: interface 68e73&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;Fan Status: On&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;Speed: 1200&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;sl no: 3431212-2323-90&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;vendor: aledaia&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;Stopping: interface 68e73&lt;/span&gt;&lt;br /&gt;tr#99&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Required:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Split the above file into sub-files such that&lt;br /&gt;- Each sub file conatins information of an interface (basically information from &#39;Starting&#39; and &#39;Stopping&#39; of the interface)&lt;br /&gt;- Sub-file name should be of the format:  interface-name_someSLno.txt&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;The python script:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;flag=0;c=0&lt;br /&gt;for line in open(&quot;file.txt&quot;):&lt;br /&gt;    line=line.strip()&lt;br /&gt;    if line.startswith(&quot;Stopping&quot;):&lt;br /&gt;        flag=0&lt;br /&gt;        o.close()&lt;br /&gt;    if line.startswith(&quot;Starting&quot;):&lt;br /&gt;        interface=line.split(&quot; &quot;)[2]&lt;br /&gt;        flag=1;c=c+1&lt;br /&gt;        o=open(interface+&quot;_&quot;+str(c)+&quot;.txt&quot;,&quot;w&quot;)&lt;br /&gt;    if flag and not line.startswith(&quot;Starting&quot;):&lt;br /&gt;        print &gt;&gt;o, line&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Output:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; cat 78e23_1.txt&lt;br /&gt;Fan Status: On&lt;br /&gt;Speed: -&lt;br /&gt;sl no: 3431212-2323-90&lt;br /&gt;vendor: aledaia&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; cat 78e24_2.txt&lt;br /&gt;Fan Status: Off&lt;br /&gt;Speed: 5670&lt;br /&gt;sl no: 3431212-2323-90&lt;br /&gt;vendor: aledaia&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; cat 68e73_3.txt&lt;br /&gt;Fan Status: On&lt;br /&gt;Speed: 1200&lt;br /&gt;sl no: 3431212-2323-90&lt;br /&gt;vendor: aledaia&lt;br /&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://pythonstarter.blogspot.com/feeds/4003448986524747089/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3580963069839202358/4003448986524747089?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/4003448986524747089'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/4003448986524747089'/><link rel='alternate' type='text/html' href='http://pythonstarter.blogspot.com/2009/12/split-file-into-sub-files-in-python.html' title='Split a file into sub files in python'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3580963069839202358.post-3681197858365914830</id><published>2009-12-02T19:38:00.000-08:00</published><updated>2009-12-02T19:48:36.748-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python list"/><category scheme="http://www.blogger.com/atom/ns#" term="python newbie"/><category scheme="http://www.blogger.com/atom/ns#" term="python stdin"/><category scheme="http://www.blogger.com/atom/ns#" term="python sys module"/><title type='text'>Python - print last few characters</title><content type='html'>&lt;span style=&quot;font-weight: bold;&quot;&gt;Input file:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;$ cat file.txt&lt;br /&gt;sldadop233masdsa213313131ada121&lt;br /&gt;sltadop233masdsa813313133cso128&lt;br /&gt;slyadop233masdsa11331313Kada134&lt;br /&gt;slqadop233masdsa31331313tada162&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Required:&lt;/span&gt; Print last 6 characters of each line of the above input file.&lt;br /&gt;&lt;br /&gt;The python script:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; cat extract-last.py&lt;br /&gt;import sys&lt;br /&gt;for line in sys.stdin:&lt;br /&gt;    print &#39;%s&#39; % (line[-7:-1])&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Executing it:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; python extract-last.py &amp;lt; file.txt&lt;br /&gt;ada121&lt;br /&gt;cso128&lt;br /&gt;ada134&lt;br /&gt;ada162&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Things to learn:&lt;/span&gt;&lt;br /&gt;- How to read a file in python from stdin&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Other alternatives in UNIX are:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;#Using bash parameter substitution&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; while read line ; do echo ${line: -6}; done &amp;lt; file.txt&lt;br /&gt;&lt;br /&gt;#Since all lines are of fixed length, we can use &#39;cut&#39; command&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; cut -c26-31 file.txt&lt;br /&gt;&lt;br /&gt;#Using sed&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; sed &#39;s/^.*\(......\)$/\1/&#39; file.txt&lt;br /&gt;&lt;br /&gt;#Using awk&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; awk &#39;{ print substr( $0, length($0) - 5, length($0) ) }&#39;  file.txt&lt;br /&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://pythonstarter.blogspot.com/feeds/3681197858365914830/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3580963069839202358/3681197858365914830?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/3681197858365914830'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/3681197858365914830'/><link rel='alternate' type='text/html' href='http://pythonstarter.blogspot.com/2009/12/python-print-last-few-characters.html' title='Python - print last few characters'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3580963069839202358.post-97605715915470195</id><published>2009-11-30T07:45:00.000-08:00</published><updated>2009-11-30T07:49:12.179-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python file open"/><category scheme="http://www.blogger.com/atom/ns#" term="python filter"/><category scheme="http://www.blogger.com/atom/ns#" term="python for loop"/><category scheme="http://www.blogger.com/atom/ns#" term="python functions"/><category scheme="http://www.blogger.com/atom/ns#" term="python isdigit"/><category scheme="http://www.blogger.com/atom/ns#" term="python lambda"/><category scheme="http://www.blogger.com/atom/ns#" term="python newbie"/><category scheme="http://www.blogger.com/atom/ns#" term="python regular expression"/><title type='text'>Remove all except digits using python</title><content type='html'>&lt;span style=&quot;font-weight: bold;&quot;&gt;Input file:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; cat file.txt&lt;br /&gt;4590:21333 2ewwq13232&lt;br /&gt;12ada1212w1 1&lt;br /&gt;13224 9#09io#&lt;br /&gt;qw2323000 9023&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Required:&lt;/span&gt; From the above file only keep the digits (i.e. remove all other characters except digits)&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Way1:&lt;/span&gt; Using python &lt;a href=&quot;http://docs.python.org/library/re.html&quot;&gt;Regular Expression&lt;/a&gt; special character &#39;\D&#39; which matches any non-digit character (equivalent to the set [^0-9])&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; python&lt;br /&gt;Python 2.5.2 (r252:60911, Jul 22 2009, 15:35:03)&lt;br /&gt;[GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)] on linux2&lt;br /&gt;&gt;&gt;&gt; import re&lt;br /&gt;&gt;&gt;&gt; for line in open(&#39;file.txt&#39;):&lt;br /&gt;...     re.sub(&quot;\D&quot;, &quot;&quot;,line)&lt;br /&gt;...&lt;br /&gt;&#39;459021333213232&#39;&lt;br /&gt;&#39;12121211&#39;&lt;br /&gt;&#39;13224909&#39;&lt;br /&gt;&#39;23230009023&#39;&lt;br /&gt;&gt;&gt;&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Another way :&lt;/span&gt; Using &lt;a href=&quot;http://docs.python.org/library/functions.html#filter&quot;&gt;python filter&lt;/a&gt; built-in function to iterate isdigit() on all lines of the file.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&gt;&gt;&gt;&lt;br /&gt;&gt;&gt;&gt; for line in open(&#39;file.txt&#39;):&lt;br /&gt;...     filter(lambda x: x.isdigit(), line)&lt;br /&gt;...&lt;br /&gt;&#39;459021333213232&#39;&lt;br /&gt;&#39;12121211&#39;&lt;br /&gt;&#39;13224909&#39;&lt;br /&gt;&#39;23230009023&#39;&lt;br /&gt;&gt;&gt;&gt;&lt;br /&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://pythonstarter.blogspot.com/feeds/97605715915470195/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3580963069839202358/97605715915470195?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/97605715915470195'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/97605715915470195'/><link rel='alternate' type='text/html' href='http://pythonstarter.blogspot.com/2009/11/remove-all-except-digits-using-python.html' title='Remove all except digits using python'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3580963069839202358.post-5368123880229281139</id><published>2009-11-25T19:55:00.000-08:00</published><updated>2009-11-25T19:58:12.616-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python file open"/><category scheme="http://www.blogger.com/atom/ns#" term="python for loop"/><category scheme="http://www.blogger.com/atom/ns#" term="python newbie"/><category scheme="http://www.blogger.com/atom/ns#" term="python replace"/><category scheme="http://www.blogger.com/atom/ns#" term="python strip"/><category scheme="http://www.blogger.com/atom/ns#" term="python write file"/><title type='text'>Change file delimiter using Python</title><content type='html'>Input file is comma delimited:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; cat /tmp/file.txt&lt;br /&gt;5232,92338,84545,34,&lt;br /&gt;2233,25644,23233,23,&lt;br /&gt;6211,1212,4343,434,&lt;br /&gt;2434,621171,9121,33,&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Required:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Convert the above comma(,) delimited file to a colon(:) delimited file such that there is no colon at the end of each line.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Python solution:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; python&lt;br /&gt;Python 2.5.2 (r252:60911, Jul 22 2009, 15:35:03)&lt;br /&gt;[GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)] on linux2&lt;br /&gt;Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.&lt;br /&gt;&gt;&gt;&gt; fp = open(&quot;/tmp/file.txt.new&quot;,&quot;w&quot;)&lt;br /&gt;&gt;&gt;&gt; for line in open(&#39;/tmp/file.txt&#39;):&lt;br /&gt;...     fp.write(line.strip()[:-1].replace(&#39;,&#39;,&#39;:&#39;)+&#39;\n&#39;)&lt;br /&gt;...&lt;br /&gt;&gt;&gt;&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Output:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; cat /tmp/file.txt.new&lt;br /&gt;5232:92338:84545:34&lt;br /&gt;2233:25644:23233:23&lt;br /&gt;6211:1212:4343:434&lt;br /&gt;2434:621171:9121:33&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Alternative solutions:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;An alternative using UNIX sed will be:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; sed -e &#39;s/,/:/g&#39; -e &#39;s/:$//g&#39; /tmp/file.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;And a related post using UNIX awk can be found on my bash scripting blog &lt;a href=&quot;http://unstableme.blogspot.com/2008/01/awk-change-field-separator-add-line.html&quot;&gt;here&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://pythonstarter.blogspot.com/feeds/5368123880229281139/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3580963069839202358/5368123880229281139?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/5368123880229281139'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/5368123880229281139'/><link rel='alternate' type='text/html' href='http://pythonstarter.blogspot.com/2009/11/change-file-delimiter-using-python.html' title='Change file delimiter using Python'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3580963069839202358.post-208540712165651125</id><published>2009-11-03T20:09:00.000-08:00</published><updated>2009-11-03T20:15:42.875-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python file open"/><category scheme="http://www.blogger.com/atom/ns#" term="python if"/><category scheme="http://www.blogger.com/atom/ns#" term="python len"/><category scheme="http://www.blogger.com/atom/ns#" term="python newbie"/><category scheme="http://www.blogger.com/atom/ns#" term="python range"/><category scheme="http://www.blogger.com/atom/ns#" term="python readlines"/><category scheme="http://www.blogger.com/atom/ns#" term="python split"/><category scheme="http://www.blogger.com/atom/ns#" term="python startswith"/><category scheme="http://www.blogger.com/atom/ns#" term="python write file"/><title type='text'>Print line next to pattern in python</title><content type='html'>&lt;span style=&quot;font-weight: bold;&quot;&gt;Input file:&lt;/span&gt; &#39;file.txt&#39; contains results of a set of students in the following format (i.e. for any student result precedes the student id)&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; cat file.txt&lt;br /&gt;Result:Pass&lt;br /&gt;id:502&lt;br /&gt;Result:Fail&lt;br /&gt;id:909&lt;br /&gt;Result:Pass&lt;br /&gt;id:503&lt;br /&gt;Result:Pass&lt;br /&gt;id:501&lt;br /&gt;Result:Fail&lt;br /&gt;id:802&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Required:&lt;/span&gt; Print the Ids of the students who have passed the exam.&lt;br /&gt;&lt;br /&gt;The python program:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;fp = open(&quot;passedids.txt&quot;,&quot;w&quot;)&lt;br /&gt;data = open(&quot;file.txt&quot;).readlines()&lt;br /&gt;for i in range(len(data)):&lt;br /&gt;        if data[i].startswith(&quot;Result:Pass&quot;):&lt;br /&gt;                fp.write(data[i+1].split(&quot;:&quot;)[1])&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Executing it:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; python printnext.py&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; cat passedids.txt&lt;br /&gt;502&lt;br /&gt;503&lt;br /&gt;501&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Another python alternative:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;fp=open(&#39;file.txt&#39;,&#39;r&#39;)&lt;br /&gt;previous_line = &quot;&quot;&lt;br /&gt;&lt;br /&gt;for current_line in fp:&lt;br /&gt;    if &#39;Result:Pass&#39; in previous_line:&lt;br /&gt;        print current_line.split(&quot;:&quot;)[1],&lt;br /&gt;    previous_line = current_line&lt;br /&gt;fp.close()&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Executing it:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; python printnext1.py&lt;br /&gt;502&lt;br /&gt;503&lt;br /&gt;501&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Related post:&lt;br /&gt;&lt;br /&gt;- &lt;a href=&quot;http://pythonstarter.blogspot.com/2009/10/print-line-above-pattern-in-python.html&quot;&gt;Print line above pattern in python&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://pythonstarter.blogspot.com/feeds/208540712165651125/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3580963069839202358/208540712165651125?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/208540712165651125'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/208540712165651125'/><link rel='alternate' type='text/html' href='http://pythonstarter.blogspot.com/2009/11/print-line-next-to-pattern-in-python.html' title='Print line next to pattern in python'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3580963069839202358.post-3877223565530639589</id><published>2009-10-31T20:45:00.000-07:00</published><updated>2009-10-31T20:49:00.536-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python enumerate"/><category scheme="http://www.blogger.com/atom/ns#" term="python file open"/><category scheme="http://www.blogger.com/atom/ns#" term="python for loop"/><category scheme="http://www.blogger.com/atom/ns#" term="python if"/><category scheme="http://www.blogger.com/atom/ns#" term="python newbie"/><category scheme="http://www.blogger.com/atom/ns#" term="python readlines"/><category scheme="http://www.blogger.com/atom/ns#" term="python write file"/><title type='text'>Python - print section of file using line number</title><content type='html'>e.g. Print the section of input file &#39;input.txt&#39; between line number 22 and 89.&lt;br /&gt;&lt;br /&gt;Using python enumerate function sequence numbers:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;for i,line in enumerate(open(&quot;file.txt&quot;)):&lt;br /&gt;    if i &amp;gt;= 21 and i &amp;lt; 89 :&lt;br /&gt;        print line,&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;And if you want to write the section to a new file say &#39;/tmp/fileA&#39;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;fp = open(&quot;/tmp/fileA&quot;,&quot;w&quot;)&lt;br /&gt;for i,line in enumerate(open(&quot;file.txt&quot;)):&lt;br /&gt;    if i &amp;gt;= 21 and i &amp;lt; 89 :&lt;br /&gt;        fp.write(line)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Another approach:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;print(&#39;&#39;.join(open(&#39;file.txt&#39;, &#39;r&#39;).readlines()[21:89])),&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;And if you wish to write the section to a new file say &#39;/tmp/fileB&#39;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;fp = open(&quot;/tmp/fileB&quot;,&quot;w&quot;)&lt;br /&gt;fp.write(&#39;&#39;.join(open(&#39;file.txt&#39;, &#39;r&#39;).readlines()[21:89])),&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Read about python enumerate function &lt;a href=&quot;http://docs.python.org/library/functions.html#enumerate&quot;&gt;here&lt;/a&gt; and below is a small example using python enumerate function:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&gt;&gt;&gt; for i, student in enumerate([&#39;Alex&#39;, &#39;Ryan&#39;, &#39;Deb&#39;]):&lt;br /&gt;...     print i, student&lt;br /&gt;...&lt;br /&gt;0 Alex&lt;br /&gt;1 Ryan&lt;br /&gt;2 Deb&lt;br /&gt;&gt;&gt;&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Also &lt;a href=&quot;http://unstableme.blogspot.com/2009/10/extract-range-of-lines-using-sed-awk.html&quot;&gt;find &lt;/a&gt;my other post on Extracting section of a file using line numbers applying awk, sed, Perl, vi editor and UNIX/Linux head and tail command techniques.</content><link rel='replies' type='application/atom+xml' href='http://pythonstarter.blogspot.com/feeds/3877223565530639589/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3580963069839202358/3877223565530639589?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/3877223565530639589'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/3877223565530639589'/><link rel='alternate' type='text/html' href='http://pythonstarter.blogspot.com/2009/10/python-print-section-of-file-using-line.html' title='Python - print section of file using line number'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3580963069839202358.post-3146582387051069538</id><published>2009-10-24T10:39:00.000-07:00</published><updated>2009-10-24T10:55:10.767-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python functions"/><category scheme="http://www.blogger.com/atom/ns#" term="python lambda"/><category scheme="http://www.blogger.com/atom/ns#" term="python newbie"/><category scheme="http://www.blogger.com/atom/ns#" term="python reduce"/><title type='text'>Python - Adding numbers in a list</title><content type='html'>Lets see some of ways in python to add the numbers present in a list.&lt;br /&gt;&lt;br /&gt;Suppose:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&gt;&gt;&gt; numlist = [10,20,5,30]&lt;br /&gt;&gt;&gt;&gt; numlist&lt;br /&gt;[10, 20, 5, 30]&lt;br /&gt;&gt;&gt;&gt; print sum(numlist)&lt;br /&gt;65&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Using python built in function &#39;&lt;a href=&quot;http://docs.python.org/library/functions.html#reduce&quot;&gt;reduce&lt;/a&gt;&#39;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&gt;&gt;&gt; numlist&lt;br /&gt;[10, 20, 5, 30]&lt;br /&gt;&gt;&gt;&gt; def add(x, y): return x + y&lt;br /&gt;...&lt;br /&gt;&gt;&gt;&gt; sum = reduce(add, numlist)&lt;br /&gt;&gt;&gt;&gt; sum&lt;br /&gt;65&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Enhancing the above using python &#39;&lt;a href=&quot;http://diveintopython.org/power_of_introspection/lambda_functions.html&quot;&gt;lambda&#39;&lt;/a&gt; function&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&gt;&gt;&gt; numlist&lt;br /&gt;[10, 20, 5, 30]&lt;br /&gt;&gt;&gt;&gt; reduce(lambda b,a: a+b, numlist)&lt;br /&gt;65&lt;br /&gt;&gt;&gt;&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Or using python for loop:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&gt;&gt;&gt; numlist&lt;br /&gt;[10, 20, 5, 30]&lt;br /&gt;&gt;&gt;&gt; sum = 0&lt;br /&gt;&gt;&gt;&gt; for i in numlist:&lt;br /&gt;...     sum += i&lt;br /&gt;...&lt;br /&gt;&gt;&gt;&gt; sum&lt;br /&gt;65&lt;br /&gt;&gt;&gt;&gt;&lt;br /&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://pythonstarter.blogspot.com/feeds/3146582387051069538/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3580963069839202358/3146582387051069538?isPopup=true' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/3146582387051069538'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/3146582387051069538'/><link rel='alternate' type='text/html' href='http://pythonstarter.blogspot.com/2009/10/python-adding-numbers-in-list.html' title='Python - Adding numbers in a list'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3580963069839202358.post-3977348846992697140</id><published>2009-10-16T10:39:00.000-07:00</published><updated>2009-10-16T10:57:12.960-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python command line"/><category scheme="http://www.blogger.com/atom/ns#" term="python functions"/><category scheme="http://www.blogger.com/atom/ns#" term="python getopt"/><category scheme="http://www.blogger.com/atom/ns#" term="python list"/><category scheme="http://www.blogger.com/atom/ns#" term="python map"/><category scheme="http://www.blogger.com/atom/ns#" term="python newbie"/><category scheme="http://www.blogger.com/atom/ns#" term="python split"/><category scheme="http://www.blogger.com/atom/ns#" term="python str"/><category scheme="http://www.blogger.com/atom/ns#" term="python strftime function"/><category scheme="http://www.blogger.com/atom/ns#" term="python string methods"/><category scheme="http://www.blogger.com/atom/ns#" term="python time module"/><title type='text'>Python - time difference between dates</title><content type='html'>&lt;span style=&quot;font-weight: bold;&quot;&gt;Required: &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Find the time difference between two dates (of following format) in seconds and in hh:mm:ss format.&lt;br /&gt;&lt;br /&gt;e.g.&lt;br /&gt;&lt;br /&gt;date1=&#39;Oct/09/2009  10:58:01&#39; and&lt;br /&gt;date2=&#39;Oct/10/2009  12:17:10&#39;&lt;br /&gt;&lt;br /&gt;find the difference between date1 and date2 in seconds(i.e. 91149 seconds) and later convert it to hh:mm:ss format (i.e. 25:19:09).&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;The complete python program:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;import sys,time,string,getopt&lt;br /&gt;&lt;br /&gt;def usage():&lt;br /&gt;    print &quot;Usage: adbtimediff.py -f &amp;lt;fromTime&amp;gt; -t &amp;lt;toTime&amp;gt; \n&quot;&lt;br /&gt;    sys.exit(2)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;def parse_args():&lt;br /&gt;    global fromTime,toTime&lt;br /&gt;    fromTime = toTime = &quot;&quot;&lt;br /&gt;&lt;br /&gt;    try:&lt;br /&gt;        opts, args = getopt.getopt(sys.argv[1:], &quot;f:t:&quot;, [&quot;fromtime&quot;, &quot;totime&quot;])&lt;br /&gt;    except getopt.GetoptError:&lt;br /&gt;        print &quot;Invalid arguments, exiting&quot;&lt;br /&gt;        sys.exit(2)&lt;br /&gt;&lt;br /&gt;    for arg, val in opts:&lt;br /&gt;        if  arg in (&quot;-f&quot;,&quot;--fromtime&quot;):&lt;br /&gt;            fromTime = val&lt;br /&gt;        elif arg in (&quot;-t&quot;,&quot;--totime&quot;):&lt;br /&gt;            toTime = val&lt;br /&gt;&lt;br /&gt;    if fromTime == toTime == &quot;&quot; :&lt;br /&gt;        usage()&lt;br /&gt;&lt;br /&gt;def compute_time(time1):&lt;br /&gt;    t = time1.split(&#39;:&#39;)&lt;br /&gt;    return time.mktime(time.strptime(&quot;:&quot;.join(t[0:len(t)]),&quot;%b/%d/%Y %H:%M:%S&quot;))&lt;br /&gt;&lt;br /&gt;def subtract(list):&lt;br /&gt;    return list[1] - list[0]&lt;br /&gt;&lt;br /&gt;def time_convert(secs):&lt;br /&gt;    secs = int(secs)&lt;br /&gt;    mins = secs // 60&lt;br /&gt;    hrs = mins // 60&lt;br /&gt;    return &quot;%02d:%02d:%02d&quot; % (hrs, mins % 60, secs % 60)&lt;br /&gt;&lt;br /&gt;def main():&lt;br /&gt;    parse_args()&lt;br /&gt;    print &quot;Fromtime : &quot; + str(fromTime) + &#39;\n&#39; + &quot;Totime : &quot; + str(toTime)&lt;br /&gt;    timelist = [ fromTime, toTime ]&lt;br /&gt;    s = map(compute_time,timelist)&lt;br /&gt;    d = subtract(s)&lt;br /&gt;    print &quot;diff in seconds : &quot; + str(d)&lt;br /&gt;    f = str(d).split(&#39;.&#39;)&lt;br /&gt;    final = time_convert(f[0])&lt;br /&gt;    print &quot;Total difference in required format : &quot; + str(final)&lt;br /&gt;&lt;br /&gt;main()&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Executing the above script:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; python timediff.py -f &#39;Oct/09/2009  10:58:01&#39; -t &#39;Oct/10/2009  12:17:10&#39;&lt;br /&gt;&lt;br /&gt;Output:&lt;br /&gt;&lt;br /&gt;Fromtime : Oct/09/2009  10:58:01&lt;br /&gt;Totime : Oct/10/2009  12:17:10&lt;br /&gt;diff in seconds : 91149.0&lt;br /&gt;Total difference in required format : 25:19:09&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Related concepts and posts:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;- &lt;a href=&quot;http://pythonstarter.blogspot.com/2009/10/python-seconds-to-hh-mm-ss-conversion.html&quot;&gt;Convert seconds to hh:mm:ss format using python&lt;/a&gt;&lt;br /&gt;- Python &lt;a href=&quot;http://docs.python.org/library/time.html#time.mktime&quot;&gt;time.mktime&lt;/a&gt;&lt;br /&gt;- Python &lt;a href=&quot;http://docs.python.org/library/time.html#time.strftime&quot;&gt;time.strftime&lt;/a&gt;&lt;br /&gt;- Python &lt;a href=&quot;http://docs.python.org/library/functions.html#map&quot;&gt;map&lt;/a&gt;&lt;br /&gt;- Python &lt;a href=&quot;http://docs.python.org/library/getopt.html&quot;&gt;getopt&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://pythonstarter.blogspot.com/feeds/3977348846992697140/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3580963069839202358/3977348846992697140?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/3977348846992697140'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/3977348846992697140'/><link rel='alternate' type='text/html' href='http://pythonstarter.blogspot.com/2009/10/python-time-difference-between-dates.html' title='Python - time difference between dates'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3580963069839202358.post-2027134024732822192</id><published>2009-10-14T11:29:00.000-07:00</published><updated>2009-10-14T11:34:42.547-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python command line"/><category scheme="http://www.blogger.com/atom/ns#" term="python datetime module"/><category scheme="http://www.blogger.com/atom/ns#" term="python newbie"/><category scheme="http://www.blogger.com/atom/ns#" term="python strftime function"/><category scheme="http://www.blogger.com/atom/ns#" term="python time module"/><category scheme="http://www.blogger.com/atom/ns#" term="python timedelta"/><title type='text'>Python - seconds to hh-mm-ss conversion</title><content type='html'>Solution1: Using python &#39;time&#39; module &lt;a href=&quot;http://docs.python.org/library/time.html#time.strftime&quot;&gt;strftime&lt;/a&gt; function.&lt;br /&gt;&lt;pre&gt; &lt;br /&gt;Python 2.5.2 (r252:60911, Jul 22 2009, 15:35:03)&lt;br /&gt;[GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)] on linux2&lt;br /&gt;Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.&lt;br /&gt;&gt;&gt;&gt; import time&lt;br /&gt;&gt;&gt;&gt; time.strftime(&#39;%H:%M:%S&#39;, time.gmtime(7302))&lt;br /&gt;&#39;02:01:42&#39;&lt;br /&gt;&gt;&gt;&gt; time.strftime(&#39;%H:%M:%S&#39;, time.gmtime(86399))&lt;br /&gt;&#39;23:59:59&#39;&lt;br /&gt;&gt;&gt;&gt; time.strftime(&#39;%H:%M:%S&#39;, time.gmtime(86405))&lt;br /&gt;&#39;00:00:05&#39;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So as seen above this solution works only for num seconds &lt; 1 day (86400 seconds)&lt;br /&gt;&lt;br /&gt;Solution2: Using python datetime module, &lt;a href=&quot;http://docs.python.org/library/datetime.html#timedelta-objects&quot;&gt;timedelta&lt;/a&gt; object.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&gt;&gt;&gt; import datetime&lt;br /&gt;&gt;&gt;&gt; x = datetime.timedelta(seconds=7302)&lt;br /&gt;&gt;&gt;&gt; str(x)&lt;br /&gt;&#39;2:01:42&#39;&lt;br /&gt;&gt;&gt;&gt; x = datetime.timedelta(seconds=86399)&lt;br /&gt;&gt;&gt;&gt; str(x)&lt;br /&gt;&#39;23:59:59&#39;&lt;br /&gt;&gt;&gt;&gt; x = datetime.timedelta(seconds=86405)&lt;br /&gt;&gt;&gt;&gt; str(x)&lt;br /&gt;&#39;1 day, 0:00:05&#39;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Solution3: Using normal division in python&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;import sys&lt;br /&gt;&lt;br /&gt;secs = int(sys.argv[1])&lt;br /&gt;mins = secs // 60&lt;br /&gt;hrs = mins // 60&lt;br /&gt;&lt;br /&gt;#hh:mm:ss&lt;br /&gt;print &quot;%02d:%02d:%02d&quot; % (hrs, mins % 60, secs % 60)&lt;br /&gt;&lt;br /&gt;#mm:ss&lt;br /&gt;print &quot;%02d:%02d&quot; % (mins, secs % 60)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Executing it:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 102, 102);&quot;&gt;$&lt;/span&gt; python timeconv.py 7302&lt;br /&gt;02:01:42&lt;br /&gt;121:42&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 102, 102);&quot;&gt;$&lt;/span&gt; python timeconv.py 86399&lt;br /&gt;23:59:59&lt;br /&gt;1439:59&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 102, 102);&quot;&gt;$&lt;/span&gt; python timeconv.py 86405&lt;br /&gt;24:00:05&lt;br /&gt;1440:05&lt;br /&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://pythonstarter.blogspot.com/feeds/2027134024732822192/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3580963069839202358/2027134024732822192?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/2027134024732822192'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/2027134024732822192'/><link rel='alternate' type='text/html' href='http://pythonstarter.blogspot.com/2009/10/python-seconds-to-hh-mm-ss-conversion.html' title='Python - seconds to hh-mm-ss conversion'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3580963069839202358.post-8492551444130839247</id><published>2009-10-07T20:39:00.000-07:00</published><updated>2009-10-07T20:43:17.057-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python file open"/><category scheme="http://www.blogger.com/atom/ns#" term="python for loop"/><category scheme="http://www.blogger.com/atom/ns#" term="python if"/><category scheme="http://www.blogger.com/atom/ns#" term="python list"/><category scheme="http://www.blogger.com/atom/ns#" term="python readlines"/><category scheme="http://www.blogger.com/atom/ns#" term="python split"/><category scheme="http://www.blogger.com/atom/ns#" term="python startswith"/><category scheme="http://www.blogger.com/atom/ns#" term="python strip"/><category scheme="http://www.blogger.com/atom/ns#" term="python write file"/><title type='text'>Print line above pattern in python</title><content type='html'>Input file: &#39;data.txt&#39; contains results of a set of students in the following format.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; cat data.txt&lt;br /&gt;id:502&lt;br /&gt;Result:Pass&lt;br /&gt;id:909&lt;br /&gt;Result:Fail&lt;br /&gt;id:503&lt;br /&gt;Result:Pass&lt;br /&gt;id:501&lt;br /&gt;Result:Pass&lt;br /&gt;id:802&lt;br /&gt;Result:Fail&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Required:&lt;/span&gt;&lt;br /&gt;Print the Ids of the students who have passed the exam.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;The python program:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;fp = open(&quot;passedids.txt&quot;,&quot;w&quot;)&lt;br /&gt;data = open(&quot;data.txt&quot;).readlines()&lt;br /&gt;for i in range(len(data)):&lt;br /&gt;        if data[i].startswith(&quot;Result:Pass&quot;):&lt;br /&gt;                fp.write(data[i-1].split(&quot;:&quot;)[1])&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Output:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; cat passedids.txt&lt;br /&gt;502&lt;br /&gt;503&lt;br /&gt;501&lt;br /&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://pythonstarter.blogspot.com/feeds/8492551444130839247/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3580963069839202358/8492551444130839247?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/8492551444130839247'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/8492551444130839247'/><link rel='alternate' type='text/html' href='http://pythonstarter.blogspot.com/2009/10/print-line-above-pattern-in-python.html' title='Print line above pattern in python'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3580963069839202358.post-7487682098312627150</id><published>2009-10-06T10:49:00.000-07:00</published><updated>2009-10-06T10:53:17.176-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python for loop"/><category scheme="http://www.blogger.com/atom/ns#" term="python if"/><category scheme="http://www.blogger.com/atom/ns#" term="python newbie"/><category scheme="http://www.blogger.com/atom/ns#" term="python readlines"/><category scheme="http://www.blogger.com/atom/ns#" term="python startswith"/><title type='text'>Python - delete lines between two pattern</title><content type='html'>&lt;span style=&quot;font-weight: bold;&quot;&gt;Input file:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; cat input.txt&lt;br /&gt;test1&lt;br /&gt;test2&lt;br /&gt;test3&lt;br /&gt;BEGIN&lt;br /&gt;test4&lt;br /&gt;test5&lt;br /&gt;test6&lt;br /&gt;END&lt;br /&gt;test7&lt;br /&gt;test8&lt;br /&gt;test9&lt;br /&gt;BEGIN&lt;br /&gt;test10&lt;br /&gt;test11&lt;br /&gt;END&lt;br /&gt;test12&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Required:&lt;/span&gt;&lt;br /&gt;From the above file delete the lines which are between a BEGIN-END block and print rest of the lines.&lt;br /&gt;&lt;br /&gt;The python script deleteline.py:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;flag = 1&lt;br /&gt;linelist = open(&quot;input.txt&quot;).readlines()&lt;br /&gt;for line in linelist:&lt;br /&gt;    if line.startswith(&quot;BEGIN&quot;):&lt;br /&gt;        flag = 0&lt;br /&gt;    if line.startswith(&quot;END&quot;):&lt;br /&gt;        flag = 1&lt;br /&gt;    if flag and not line.startswith(&quot;END&quot;):&lt;br /&gt;       print line,&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Executing it:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 102, 102);&quot;&gt;$&lt;/span&gt; python deleteline.py&lt;br /&gt;test1&lt;br /&gt;test2&lt;br /&gt;test3&lt;br /&gt;test7&lt;br /&gt;test8&lt;br /&gt;test9&lt;br /&gt;test12&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now if we need to print the lines which are between a BEGIN-END block.&lt;br /&gt;Here is a modification of the above scirpt.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;flag = 1&lt;br /&gt;linelist = open(&quot;input.txt&quot;).readlines()&lt;br /&gt;for line in linelist:&lt;br /&gt;    if line.startswith(&quot;BEGIN&quot;):&lt;br /&gt;        flag = 0&lt;br /&gt;    if line.startswith(&quot;END&quot;):&lt;br /&gt;        flag = 1&lt;br /&gt;    if not flag and not line.startswith(&quot;BEGIN&quot;):&lt;br /&gt;       print line,&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Executing it:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; python printline.py&lt;br /&gt;test4&lt;br /&gt;test5&lt;br /&gt;test6&lt;br /&gt;test10&lt;br /&gt;test11&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Related post:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;- &lt;a href=&quot;http://pythonstarter.blogspot.com/2009/05/lookup-file-operation-using-python.html&quot;&gt;Lookup &lt;/a&gt;file operation using python</content><link rel='replies' type='application/atom+xml' href='http://pythonstarter.blogspot.com/feeds/7487682098312627150/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3580963069839202358/7487682098312627150?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/7487682098312627150'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/7487682098312627150'/><link rel='alternate' type='text/html' href='http://pythonstarter.blogspot.com/2009/10/python-delete-lines-between-two-pattern.html' title='Python - delete lines between two pattern'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3580963069839202358.post-5990492108495249129</id><published>2009-09-24T20:35:00.000-07:00</published><updated>2009-09-24T20:44:21.698-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python capitalize"/><category scheme="http://www.blogger.com/atom/ns#" term="python lowercase"/><category scheme="http://www.blogger.com/atom/ns#" term="python newbie"/><category scheme="http://www.blogger.com/atom/ns#" term="python string methods"/><category scheme="http://www.blogger.com/atom/ns#" term="python swapcase"/><category scheme="http://www.blogger.com/atom/ns#" term="python titlecase"/><category scheme="http://www.blogger.com/atom/ns#" term="python uppercase"/><title type='text'>Python string methods for case conversion</title><content type='html'>Few important python string methods for case conversion.&lt;br /&gt;&lt;br /&gt;swapcase()&lt;br /&gt;Return a copy of the string with uppercase characters converted to lowercase and vice versa.&lt;br /&gt;&lt;br /&gt;upper()&lt;br /&gt;Return a copy of the string converted to uppercase.&lt;br /&gt;&lt;br /&gt;title()&lt;br /&gt;Return a titlecased version of the string: words start with uppercase characters, all remaining cased characters are lowercase.&lt;br /&gt;&lt;br /&gt;lower()&lt;br /&gt;Return a copy of the string converted to lowercase.&lt;br /&gt;&lt;br /&gt;capitalize( )&lt;br /&gt;Return a copy of the string with only its first character capitalized.&lt;br /&gt;&lt;br /&gt;On python prompt:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&gt;&gt;&gt; s=&#39;www.ExAmple.cOM&#39;&lt;br /&gt;&gt;&gt;&gt; s&lt;br /&gt;&#39;www.ExAmple.cOM&#39;&lt;br /&gt;&gt;&gt;&gt; s.swapcase()&lt;br /&gt;&#39;WWW.eXaMPLE.Com&#39;&lt;br /&gt;&gt;&gt;&gt; s.upper()&lt;br /&gt;&#39;WWW.EXAMPLE.COM&#39;&lt;br /&gt;&gt;&gt;&gt; s.lower()&lt;br /&gt;&#39;www.example.com&#39;&lt;br /&gt;&gt;&gt;&gt; s.title()&lt;br /&gt;&#39;Www.Example.Com&#39;&lt;br /&gt;&gt;&gt;&gt; s.capitalize()&lt;br /&gt;&#39;Www.example.com&#39;&lt;br /&gt;&gt;&gt;&gt; st=&quot;This is the Best&quot;&lt;br /&gt;&gt;&gt;&gt; st.capitalize()&lt;br /&gt;&#39;This is the best&#39;&lt;br /&gt;&gt;&gt;&gt; st.title()&lt;br /&gt;&#39;This Is The Best&#39;&lt;br /&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://pythonstarter.blogspot.com/feeds/5990492108495249129/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3580963069839202358/5990492108495249129?isPopup=true' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/5990492108495249129'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/5990492108495249129'/><link rel='alternate' type='text/html' href='http://pythonstarter.blogspot.com/2009/09/python-string-methods-for-case.html' title='Python string methods for case conversion'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3580963069839202358.post-9062585375148919725</id><published>2009-09-15T21:07:00.000-07:00</published><updated>2009-09-15T21:30:38.915-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python command line"/><category scheme="http://www.blogger.com/atom/ns#" term="python file open"/><category scheme="http://www.blogger.com/atom/ns#" term="python functions"/><category scheme="http://www.blogger.com/atom/ns#" term="python newbie"/><category scheme="http://www.blogger.com/atom/ns#" term="python split"/><category scheme="http://www.blogger.com/atom/ns#" term="python strip"/><title type='text'>Find text string in file in Python</title><content type='html'>Each line of file &quot;querymapping.txt&quot; contains two fields.&lt;br /&gt;1st field is a sql query and&lt;br /&gt;2nd one is a filename where the output of that sql query is stored.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; cat querymapping.txt&lt;br /&gt;select * from tab_fan_details;|/tmp/query7&lt;br /&gt;select * from tab_fan_speed_details;|/tmp/query4&lt;br /&gt;select * from tab_fan_spec;|/tmp/query1&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Required:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Write a python function to look-up a particular sql query (send as 1st argument to the script ) in the querymapping.txt file and return the query output filename. If no match found return default filename as &#39;/tmp/query0&#39;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;The python program:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;import sys&lt;br /&gt;default=&#39;/tmp/query0&#39;&lt;br /&gt;&lt;br /&gt;def lookupfilename(query):&lt;br /&gt;    &#39;&#39;&#39;Lookup query output filename from query&#39;&#39;&#39;&lt;br /&gt;    for line in open(&#39;querymapping.txt&#39;):&lt;br /&gt;        if query.strip() in line.strip().split(&quot;|&quot;)[0]:&lt;br /&gt;            return line.strip().split(&quot;|&quot;)[1]&lt;br /&gt;    return default&lt;br /&gt;&lt;br /&gt;fname=lookupfilename(sys.argv[1])&lt;br /&gt;print fname&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Executing the script:&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; python qlookup.py &quot;select * from tab_fan_speed_details;&quot;&lt;br /&gt;/tmp/query4&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; python qlookup.py &quot;select * from tab_fan_speed_details_new;&quot;&lt;br /&gt;/tmp/query0&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Related post:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;- &lt;a href=&quot;http://pythonstarter.blogspot.com/2009/05/lookup-file-in-python-using-dictionary.html&quot;&gt;Lookup file in python using dictionary&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://pythonstarter.blogspot.com/feeds/9062585375148919725/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3580963069839202358/9062585375148919725?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/9062585375148919725'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/9062585375148919725'/><link rel='alternate' type='text/html' href='http://pythonstarter.blogspot.com/2009/09/find-text-string-in-file-in-python.html' title='Find text string in file in Python'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3580963069839202358.post-6629159899164299165</id><published>2009-09-13T21:16:00.000-07:00</published><updated>2009-09-13T21:22:50.821-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python command line"/><category scheme="http://www.blogger.com/atom/ns#" term="python exceptions"/><category scheme="http://www.blogger.com/atom/ns#" term="python file close"/><category scheme="http://www.blogger.com/atom/ns#" term="python file objects"/><category scheme="http://www.blogger.com/atom/ns#" term="python file open"/><category scheme="http://www.blogger.com/atom/ns#" term="python file read"/><category scheme="http://www.blogger.com/atom/ns#" term="python file readline"/><category scheme="http://www.blogger.com/atom/ns#" term="python newbie"/><category scheme="http://www.blogger.com/atom/ns#" term="python os module"/><category scheme="http://www.blogger.com/atom/ns#" term="python sys module"/><title type='text'>Print file content to output - Python</title><content type='html'>Required: Write a python program to print the content of a file to output (same as Linux/UNIX cat command do)&lt;br /&gt;&lt;br /&gt;Way1: Using &lt;a href=&quot;http://docs.python.org/library/stdtypes.html#file.read&quot;&gt;file.read&lt;/a&gt; file object in python&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;import sys,os.path&lt;br /&gt;&lt;br /&gt;if len(sys.argv) &amp;lt; 2:&lt;br /&gt;   print &#39;No file specified&#39;&lt;br /&gt;   sys.exit()&lt;br /&gt;else:&lt;br /&gt;   try:&lt;br /&gt;      f = open(sys.argv[1], &#39;r&#39;)&lt;br /&gt;      print f.read(),&lt;br /&gt;      f.close()&lt;br /&gt;   except IOError:&lt;br /&gt;      print &quot;File&quot; + sys.argv[1] + &quot;does not exist.&quot;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Execute it this way: To print the contents of file.txt to the output.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;$ python cat-read.py file.txt&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Way2: Another similar python program using &lt;a href=&quot;http://docs.python.org/library/stdtypes.html#file.readline&quot;&gt;file.readline&lt;/a&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;import sys&lt;br /&gt;&lt;br /&gt;def readfile(fname):&lt;br /&gt;   f = file(fname)&lt;br /&gt;   while True:&lt;br /&gt;      line = f.readline()&lt;br /&gt;      if len(line) == 0:&lt;br /&gt;         break&lt;br /&gt;      print line.strip() #Avoid strip: print line,&lt;br /&gt;   f.close()&lt;br /&gt;&lt;br /&gt;if len(sys.argv) &amp;lt; 2:&lt;br /&gt;   print &#39;No file specified&#39;&lt;br /&gt;   sys.exit()&lt;br /&gt;else:&lt;br /&gt;   readfile(sys.argv[1])&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Read about python &lt;a href=&quot;http://docs.python.org/library/functions.html#file&quot;&gt;file()&lt;/a&gt;&lt;br /&gt;In python 3.0 file() is removed.&lt;br /&gt;&lt;br /&gt;Related concepts:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Python file object &lt;a href=&quot;http://docs.python.org/library/stdtypes.html#file.close&quot;&gt;file.close()&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Python File Objects described &lt;a href=&quot;http://docs.python.org/library/stdtypes.html#file-objects&quot;&gt;here&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;</content><link rel='replies' type='application/atom+xml' href='http://pythonstarter.blogspot.com/feeds/6629159899164299165/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3580963069839202358/6629159899164299165?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/6629159899164299165'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/6629159899164299165'/><link rel='alternate' type='text/html' href='http://pythonstarter.blogspot.com/2009/09/print-file-content-to-output-python.html' title='Print file content to output - Python'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3580963069839202358.post-4891641957227877617</id><published>2009-09-02T21:51:00.000-07:00</published><updated>2009-09-02T21:55:40.892-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="python break"/><category scheme="http://www.blogger.com/atom/ns#" term="python continue"/><category scheme="http://www.blogger.com/atom/ns#" term="python elif"/><category scheme="http://www.blogger.com/atom/ns#" term="python file open"/><category scheme="http://www.blogger.com/atom/ns#" term="python for loop"/><category scheme="http://www.blogger.com/atom/ns#" term="python if"/><category scheme="http://www.blogger.com/atom/ns#" term="python list"/><category scheme="http://www.blogger.com/atom/ns#" term="python newbie"/><category scheme="http://www.blogger.com/atom/ns#" term="python readlines"/><category scheme="http://www.blogger.com/atom/ns#" term="python split"/><title type='text'>Print last instance of file - python example</title><content type='html'>Input file:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; cat data.txt&lt;br /&gt;k:begin:0&lt;br /&gt;i:0:66&lt;br /&gt;i:1:76&lt;br /&gt;k:end:0&lt;br /&gt;k:begin:7&lt;br /&gt;i:0:55&lt;br /&gt;i:1:65&lt;br /&gt;i:2:57&lt;br /&gt;k:end:7&lt;br /&gt;k:begin:2&lt;br /&gt;i:0:10&lt;br /&gt;i:1:0&lt;br /&gt;k:end:7&lt;br /&gt;k:begin:2&lt;br /&gt;i:0:46&lt;br /&gt;k:end:7&lt;br /&gt;k:begin:9&lt;br /&gt;i:0:66&lt;br /&gt;i:1:56&lt;br /&gt;i:2:46&lt;br /&gt;i:3:26&lt;br /&gt;k:end:7&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Required: Print last instance of the above file. One instance being from &quot;k:begin&quot; to &quot;k:end&quot;&lt;br /&gt;&lt;br /&gt;The python program:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;result =[]&lt;br /&gt;all = open(&quot;data.txt&quot;).readlines()&lt;br /&gt;for line in all[::-1]: #start from last ;proceed up&lt;br /&gt;        f=line.split(&quot;:&quot;)&lt;br /&gt;        if f[0]==&quot;k&quot; and f[1]==&quot;end&quot;:&lt;br /&gt;                continue&lt;br /&gt;        elif f[0]==&quot;k&quot; and f[1]==&quot;begin&quot;:&lt;br /&gt;                break&lt;br /&gt;        else: result.append(line)&lt;br /&gt;print result&lt;br /&gt;print &quot;\nlast instance is\n&quot;&lt;br /&gt;print &#39;&#39;.join(result[::-1]) #reverse&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Executing it:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(255, 0, 0);&quot;&gt;$&lt;/span&gt; python ins.py&lt;br /&gt;[&#39;i:3:26\n&#39;, &#39;i:2:46\n&#39;, &#39;i:1:56\n&#39;, &#39;i:0:66\n&#39;]&lt;br /&gt;&lt;br /&gt;last instance is&lt;br /&gt;&lt;br /&gt;i:0:66&lt;br /&gt;i:1:56&lt;br /&gt;i:2:46&lt;br /&gt;i:3:26&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Related post:&lt;br /&gt;- &lt;a href=&quot;http://pythonstarter.blogspot.com/2009/05/print-first-few-instances-of-file.html&quot;&gt;Print first few instances from file using python&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://pythonstarter.blogspot.com/feeds/4891641957227877617/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3580963069839202358/4891641957227877617?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/4891641957227877617'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3580963069839202358/posts/default/4891641957227877617'/><link rel='alternate' type='text/html' href='http://pythonstarter.blogspot.com/2009/09/print-last-instance-of-file-python.html' title='Print last instance of file - python example'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/00278385409494551967</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>