Re: [Tutor] Help required
On 03/20/2013 02:12 AM, Arijit Ukil wrote: I am new to python. My intention is to read the file digi.txt and store in separate arrays all the values of each columns. However, the following program prints only the last value, i.e. 1350696500.0. Please help to rectify this. f = open (/digi.txt/, /r+/) datafile = f.readlines() list_of_lists = datafile fordata inlist_of_lists: lstval = data.split (/','/) timest = float(lstval[0]) energy = float(lstval[1]) f.close() printtimest Regards, Arijit Ukil You would want to create an empty list and then append values to it inside the loop. E.g. lst=[]; lst.append(val) -m -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help required
On 20 March 2013 06:12, Arijit Ukil arijit.u...@tcs.com wrote: I am new to python. My intention is to read the file digi.txt and store in separate arrays all the values of each columns. However, the following program prints only the last value, i.e. 1350696500.0. Please help to rectify this. f = open (digi.txt, r+) datafile = f.readlines() list_of_lists = datafile for data in list_of_lists: lstval = data.split (',') timest = float(lstval[0]) energy = float(lstval[1]) f.close() print timest A few questions to ask yourself. Is 'f' a descriptive name, equivalent to your other variable names? What does your third line do, and do you think it's actually required? What implications would it cause if you removed it? How do you overcome those issues? What value is stored in 'timest' the first iteration of the loop, the second, the third? (Put a print statement in the loop to find out) Are you actually doing anything with 'energy'? Do you need to wait until after the loop to close your file? HTH, Bodsda ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help required
First up please give a sensible subject, say problem parsing csv file. I'm assuming the crud shown below is due to you posting in html, please use plain text instead. On 20/03/2013 06:12, Arijit Ukil wrote: I am new to python. My intention is to read the file digi.txt and store in separate arrays all the values of each columns. However, the following program prints only the last value, i.e. 1350696500.0. Please help to rectify this. f = open (/digi.txt/, /r+/) You might like to use the with statement as it saves the explicit close later. Do you really need update mode? I'll assume not. with open('digi.txt', 'r') as f: datafile = f.readlines() datafile is a poor name, it's the contents of the file here not the file itself. list_of_lists = datafile This gives another name for datafile which you only use in the for loop, you can use datafile directly. fordata inlist_of_lists: lstval = data.split (/','/) The csv module from the standard library can look after this for you. timest = float(lstval[0]) energy = float(lstval[1]) You do nothing with timest and energy within the for loop. f.close() printtimest You're printing the last value for timest here. So you need something like the following, I'll leave you to look up the csv module if you're interested. with open('digi.txt', 'r') as f: lines = f.readlines() timestamps = [] energies = [] for line in lines: splits = line.split(',') # I'll admit not the best name timestamps.append(float(splits[0])) energies.append(float(splits[1])) # process timestamps and energies to your heart's content Regards, Arijit Ukil -- If you're using GoogleCrap™ please read this http://wiki.python.org/moin/GoogleGroupsPython. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help required to count no of lines that are until 1000 characters
Dave Angel da...@ieee.org wrote But you have a serious bug in your code, that nobody in the first five responses has addressed. That while loop will loop over the first line repeatedly, till it reaches or exceeds 1000, regardless of the length of subsequent lines. Oooh, good catch, I completely missed that one! :-) Alan G ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help required to count no of lines that are until 1000 characters
On Tue, May 11, 2010 at 1:00 PM, ramya natarajan nramy...@gmail.com wrote: Hello, I have to read lines from files exactly upto 1000 characters. But the problem is its reading entire line and not stopping excatly in 1000 characters. Can some one help what mistake i am doing here?. log = open('/tmp/new.txt','r') lines,char = 0,0 for line in log.readlines(): while char 1000 : for ch in line : char += len(ch) lines += 1 print char , lines here's the pseudocode of what you're doing, it might help you understand what the problem is: for every line in the file: if the character count is less than 1000, add the length of the current line. You are missing a condition. Here is another version of your code that has the same problem, see if this helps make it clearer: lines, chars = 0,0 with open('/temp/new.txt') as f: for line in f: if chars 1000: break chars += len(line) This sounds a lot like a homework problem so I won't give you the answer, but I hope that helps. Also do you realize you are counting newlines as well? You may not want to do this, depending on your intended application. Hope that helps, -Luke ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help required to count no of lines that are until 1000 characters
On Tue, 11 May 2010 11:00:20 -0700 ramya natarajan nramy...@gmail.com wrote: Hello, I am very beginner to programming, I got task to Write a loop that reads each line of a file and counts the number of lines that are read until the total length of the lines is 1,000 characters. I have to read lines from files exactly upto 1000 characters. Here is my code: I created file under /tmp/new.txt which has 100 lines and 2700 characters , I wrote code will read exactly 1000 characters and count lines upto those characters.But the problem is its reading entire line and not stopping excatly in 1000 characters. Can some one help what mistake i am doing here?. log = open('/tmp/new.txt','r') lines,char = 0,0 for line in log.readlines(): while char 1000 : for ch in line : char += len(ch) lines += 1 print char , lines 1026 , 38 Its counting entire line instead of character upto 1000 -- can some one point out what mistake am i doing here , where its not stopping at 1000 . I am reading only char by car My new.txt -- cotains content like this is my new number\n Can some one please help. I spent hours and hours to find issue but i am not able to figure out, Any help would be greatly appreciated. Thank you Ramya Either you read line per line, but then you cannot stop exactly at the 1000th character; or you traverse the text char per char, but this is a bit picky. I would read line per line, and when count = 1000, read chars inside current line to get to the 1000th, if needed. (Your specification does not state this, but your disappointment seems to be about that issue ;-) Denis vit esse estrany ☣ spir.wikidot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help required to count no of lines that are until 1000 characters
On 12-May-2010, at 12:32 AM, spir ☣ wrote: On Tue, 11 May 2010 11:00:20 -0700 ramya natarajan nramy...@gmail.com wrote: Hello, I am very beginner to programming, I got task to Write a loop that reads each line of a file and counts the number of lines that are read until the total length of the lines is 1,000 characters. I have to read lines from files exactly upto 1000 characters. Here is my code: I created file under /tmp/new.txt which has 100 lines and 2700 characters , I wrote code will read exactly 1000 characters and count lines upto those characters.But the problem is its reading entire line and not stopping excatly in 1000 characters. Can some one help what mistake i am doing here?. log = open('/tmp/new.txt','r') lines,char = 0,0 for line in log.readlines(): while char 1000 : for ch in line : char += len(ch) lines += 1 print char , lines 1026 , 38 Its counting entire line instead of character upto 1000 -- can some one point out what mistake am i doing here , where its not stopping at 1000 . I am reading only char by car My new.txt -- cotains content like this is my new number\n Can some one please help. I spent hours and hours to find issue but i am not able to figure out, Any help would be greatly appreciated. Thank you Ramya Either you read line per line, but then you cannot stop exactly at the 1000th character; or you traverse the text char per char, but this is a bit picky. I would read line per line, and when count = 1000, read chars inside current line to get to the 1000th, if needed. (Your specification does not state this, but your disappointment seems to be about that issue ;-) Denis You can try read instead of readlines. Something like... print 'Number of lines till 1000th character:', len(open('/tmp/new.txt','r').read(1000).split('\n')) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help required to count no of lines that are until 1000characters
ramya natarajan nramy...@gmail.com wrote characters.But the problem is its reading entire line and not stopping excatly in 1000 characters. Do you really need to stop reading the file at 1000 characters rather than the line containing the 1000th character? That seems a very arbitrary sort of thing to do. Can some one help what mistake i am doing here?. log = open('/tmp/new.txt','r') lines,char = 0,0 for line in log.readlines(): while char 1000 : for ch in line : char += len(ch) lines += 1 The problem is that the inner for loop will always process every character in the line. You want to stop (or break) from the for loop when char gets to 1000. So you need to insert a test inside the for loop. Or don't use the for loop and use an index to get the characters within your while loop. BTW You don't want to add the length of the characters you just want to add 1... stopping at 1000 . I am reading only char by car No you are not, you are reading all the chars in every line that you read from the file. In fact even if you fix this loop error you will still be reading the full line from the file. Thats why I asked if you really had to stop reading the file at 1000 chars, because if so this design is fundamentally wrong. But I suspect you only need to stop reading at the line containing the 1000th char... If that is so there is an easier way to do it: # pseudo code chars = 0 for count, line in enumerate(file) if chars + len(line) 1000 chars += len(line) else: break print 1000 chars read in, count, lines HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help required to count no of lines that are until 1000 characters
spir ☣ denis.s...@gmail.com wrote Either you read line per line, but then you cannot stop exactly at the 1000th character; or you traverse the text char per char, but this is a bit picky. Or you could just read 1000 chars from the file then pick out the lines from that. But that requires you to count newlines as characters! :-) HTH, Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help required to count no of lines that are until 1000 characters
ramya natarajan wrote: Hello, I am very beginner to programming, I got task to Write a loop that reads each line of a file and counts the number of lines that are read until the total length of the lines is 1,000 characters. I have to read lines from files exactly upto 1000 characters. Here is my code: I created file under /tmp/new.txt which has 100 lines and 2700 characters , I wrote code will read exactly 1000 characters and count lines upto those characters.But the problem is its reading entire line and not stopping excatly in 1000 characters. Can some one help what mistake i am doing here?. log = open('/tmp/new.txt','r') lines,char = 0,0 for line in log.readlines(): while char 1000 : for ch in line : char += len(ch) lines += 1 print char , lines 1026 , 38 Its counting entire line instead of character upto 1000 -- can some one point out what mistake am i doing here , where its not stopping at 1000 . I am reading only char by car My new.txt -- cotains content like this is my new number\n Can some one please help. I spent hours and hours to find issue but i am not able to figure out, Any help would be greatly appreciated. Thank you Ramya The problem is ill-specified (contradictory). It'd probably be better to give the exact wording of the assignment. If you read each line of the file, then it would only be a coincidence if you read exactly 1000 characters, as most likely one of those lines will overlap the 1000 byte boundary. But you have a serious bug in your code, that nobody in the first five responses has addressed. That while loop will loop over the first line repeatedly, till it reaches or exceeds 1000, regardless of the length of subsequent lines. So it really just divides 1000 by the length of that first line. Notice that the lines += 1 will execute multiple times for a single iteration of the for loop. Second, once 1000 is reached, the for loop does not quit. So it will read the rest of the file, regardless of how big the file is. It just stops adding to lines or char, since char reached 1000 on the first line. The simplest change to your code which might accomplish what you want is to put the whole thing inside a function, and return from the function when the goal is reached. So instead of a while loop, you need some form of if test. See if you can run with that. Remember that return can return a tuple (pair of numbers). There are plenty of other optimizations and approaches, but you'll learn best by incrementally fixing what you already have. DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help required
Hi, First of all thanks a lot for your reply. search.findall is not working. here is my complete small program. def getAcl(request, pg): pged = PageEditor(request, pg) pagetext = pged.get_raw_body() search=re.compile(^#acl InternationalGroup.*\n, re.M).search(pagetext) if search: ret=search.group() else: ret='not defined' return ret def execute(macro, args): html=pbACL List For International Group:/bbr all={} pages = macro.request.rootpage.getPageList() for pagename in pages: if Page(macro.request,pagename).isStandardPage(): all[Page(macro.request, pagename).link_to(macro.request)]=getAcl(macro.request, pagename) html+=table all1=sorted(all.items()) for pg, ac in all1: if ac != not defined: html+=trtd%s/td % pg html+=td%s/td/tr % ac html+=/table return macro.formatter.rawHTML(html) Now i explain this. I have some wiki pages(or you can say documents). This python program is giving me list of all those pages, where i have written #acl InternationalGroup:read line in the pages and these pages may have also CatInternational line in the page. Now i want to search all those pages, where i have NOT written #acl InternationalGroup:read But i have written only CatInternational in the page text. I dont know how can i find only those pages where i have written only CatInternational line in the page. I shall be veryy thankful to you really for help. Best Regards, Waqas Date: Fri, 25 Sep 2009 15:39:27 -0600 Subject: Re: [Tutor] Help required From: vinces1...@gmail.com To: waqas...@hotmail.com CC: python-l...@python.org; tutor@python.org On Fri, Sep 25, 2009 at 1:56 PM, waqas ahmad waqas...@hotmail.com wrote: Hi, I dont know it is the right place to post this question. I need help to change one search code line . can you help me please. here is my search method code: search=re.compile(^#acl InternationalGroup.*\n, re.M).search(pagetext) if search: ret=search.group() else: ret='not defined' return ret here i am searching for #acl InternationalGroup in the pageText and when it true is then give me search group. I want to change this for following requirement: I want to search for #acl InternationalGroup and CatInternational for both in the pageText. when #acl InternationalGroup is not there but only CatInternational is there. then return me search group. I shall be thankful to you for any help. Best Regards, Waqas What can you do with the new Windows Live? Find out ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor i think this is what you are looking for: search=re.compile((#acl\sInternationalGroup|CatInternational).*\n, re.M).search(pagetext) if search: ret=search.findall() else: ret='not defined' return ret _ Show them the way! Add maps and directions to your party invites. http://www.microsoft.com/windows/windowslive/products/events.aspx___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help required
On Fri, Sep 25, 2009 at 1:56 PM, waqas ahmad waqas...@hotmail.com wrote: Hi, I dont know it is the right place to post this question. I need help to change one search code line . can you help me please. here is my search method code: search=re.compile(^#acl InternationalGroup.*\n, re.M).search(pagetext) if search: ret=search.group() else: ret='not defined' return ret here i am searching for #acl InternationalGroup in the pageText and when it true is then give me search group. I want to change this for following requirement: I want to search for #acl InternationalGroup and CatInternational for both in the pageText. when #acl InternationalGroup is not there but only CatInternational is there. then return me search group. I shall be thankful to you for any help. Best Regards, Waqas -- What can you do with the new Windows Live? Find outhttp://www.microsoft.com/windows/windowslive/default.aspx ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor i think this is what you are looking for: search=re.compile((#acl\sInternationalGroup|CatInternational).*\n, re.M).search(pagetext) if search: ret=search.findall() else: ret='not defined' return ret ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor