On Fri, Sep 30, 2011 at 12:31 AM, Walter Prins <wpr...@gmail.com> wrote:
> Hi, > > On 29 September 2011 17:07, lina <lina.lastn...@gmail.com> wrote: > >> I found one thing a bit weird, Here is the one: >> >> import os.path >> >> tokens=['E'] >> result=[] >> >> """ >> for fileName in os.listdir("."): >> if os.path.isfile(fileName) and os.path.splitext(fileName)[1]==".xpm": >> """ >> filedata = open("1.xpm") >> text=filedata.readlines() >> for line in text[23]: >> print line >> > >> 1] $ python try2.py | wc -l >> 206 >> > > 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. > > 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: > > for x in text[23]: > print x > > a: It steps through the letters in text[23] and prints each in turn (on a > new line.) > > So what you then do when you "wc -l" text[23], is to effectively count the > number of characters in line 24.... > > 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. ;) > > 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: > > f=open('C:\\Users\\walterp\\Desktop\\1.xpm') > >>> lines=f.readlines() > >>> slice1=lines[23] > >>> slice2=lines[23:24] > >>> slice3=lines[23:25] > >>> print slice1 > >>> print slice2 > >>> print slice3 > >>> print len(slice3) > Thanks, I truly understand now. There was a "blank line", and the strip() works. (I learned C 10 years ago, but barely used. so can understand something very basic. Thanks again for your explaination) Now I am facing how to read each column about the occurence of some letter. Long time ago, my senior left me a script with the following line, for line in text: result.append({t:line.count(t) for t in tokens}) for index,r in enumerate(result): print(index,"-----",r) I don't understand how it works, kinda of transpose a matrix? > etc. > > > Walter > > > > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -- Best Regards, lina
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor