Hi,<br><br><div class="gmail_quote">On 29 September 2011 17:07, lina <span dir="ltr"><<a href="mailto:lina.lastname@gmail.com">lina.lastname@gmail.com</a>></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=['E']<br>result=[]<br><br>"""<br>for fileName in os.listdir("."):<br>
if os.path.isfile(fileName) and os.path.splitext(fileName)[1]==".xpm":<br>"""<br></div>filedata = open("1.xpm")<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't seem to grasp what "text[23]" 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 "wc -l" 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'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 "print line.strip()" to get rid of the newline. Try the following in the python interpreter:<br>
<br>f=open('C:\\Users\\walterp\\Desktop\\1.xpm')<br>>>> lines=f.readlines()<br>>>> slice1=lines[23]<br>>>> slice2=lines[23:24]<br>>>> slice3=lines[23:25]<br>>>> print slice1<br>
>>> print slice2<br>>>> print slice3<br>>>> print len(slice3)<br><br>etc.<br><br><br>Walter<br> <br></div></div><br>