Hi,<br><br><div class="gmail_quote">On 29 September 2011 17:07, lina <span dir="ltr">&lt;<a href="mailto:lina.lastname@gmail.com">lina.lastname@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div></div>I found one thing a bit weird, Here is the one:<div class="im"><br>import os.path<br><br>tokens=[&#39;E&#39;]<br>result=[]<br><br>&quot;&quot;&quot;<br>for fileName in os.listdir(&quot;.&quot;):<br>
    if os.path.isfile(fileName) and os.path.splitext(fileName)[1]==&quot;.xpm&quot;:<br>&quot;&quot;&quot;<br></div>filedata = open(&quot;1.xpm&quot;)<br>text=filedata.readlines()<br>for line in text[23]:<br>    print line <br>
</blockquote><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><br>
1] $ python try2.py | wc -l<br>206<br></blockquote><div><br>Please go and carefully study how strings, lists etc work -- you don&#39;t seem to grasp what &quot;text[23]&quot; returns vs what text[23:24] etc returns etc.  To spell it out:  If the variable text refers to a list of strings, then text[23] is the 24th string.  24th, because the index starts from 0.  text[23:24] by contrast, is a sublist (called a slice) that contains strings 24 and 25 from the original list. <br>
<br>So, if text[23] is already itself a single string, what can the following code possibly mean?  Eg in general, what does s[0] give you if s is a string?  A: The first character in s. So then, what does the following do:<br>
<br>for x in text[23]:<br>  print x<br><br>a: It steps through the letters in text[23] and prints each in turn (on a new line.)<br><br>So what you then do when you &quot;wc -l&quot; text[23], is to effectively count the number of characters in line 24....<br>
<br>You should probably just have run the script without the wc -l and this would&#39;ve become very clear very quickly on inspecting the output.   ;)<br><br>The other results are similarly explainable, by examining the output. (To be exact, the lines in the sublists/slices include newline characters.  When you print them seperately, you therefore end up with more lines than you think.  Try &quot;print line.strip()&quot; to get rid of the newline.  Try the following in the python interpreter:<br>
<br>f=open(&#39;C:\\Users\\walterp\\Desktop\\1.xpm&#39;)<br>&gt;&gt;&gt; lines=f.readlines()<br>&gt;&gt;&gt; slice1=lines[23]<br>&gt;&gt;&gt; slice2=lines[23:24]<br>&gt;&gt;&gt; slice3=lines[23:25]<br>&gt;&gt;&gt; print slice1<br>
&gt;&gt;&gt; print slice2<br>&gt;&gt;&gt; print slice3<br>&gt;&gt;&gt; print len(slice3)<br><br>etc.<br><br><br>Walter<br> <br></div></div><br>