Re: [Tutor] Is there a package to "un-mangle" characters?
On Fri, 11/22/13, Steven D'Aprano wrote: Subject: Re: [Tutor] Is there a package to "un-mangle" characters? To: tutor@python.org Date: Friday, November 22, 2013, 4:30 PM On Thu, Nov 21, 2013 at 12:04:19PM -0800, Albert-Jan Roskam wrote: > Hi, > > Today I had a csv file in utf-8 encoding, but part of the accented > characters were mangled. The data were scraped from a website and it > turned out that at least some of the data were mangled on the website > already. Bits of the text were actually cp1252 (or cp850), I think, > even though the webpage was in utf-8 Is there any package that helps > to correct such issues? Python has superpowers :-) http://blog.luminoso.com/2012/08/20/fix-unicode-mistakes-with-python/ > Cool website! Love the corny terminology he uses. The function he created may be useful in situations where chardet, charset and icu may not be useful: a small amount of textual data that's a total mess. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] raw input program is still running.
table, a second line with the total number of rows (atoms) in the table > > followed by a line with the ordered string of chemical symbols separated > > by a silgle space. > > > > My aim is load the numerical data from this file to a c++ program to > > process this information. > > > > Thanks in advance. > > > > -Ruben. > > > > I'm sorry but we don't write code for you here. I suggest that you > start out by reading the tutorial here > http://docs.python.org/3/tutorial/index.html, try writing something and > when and if you run into problems please feel free to get back to us. > > -- > Python is the second best programming language in the world. > But the best has yet to be invented. Christian Tismer > > Mark Lawrence > > > > -- > > Message: 6 > Date: Sat, 23 Nov 2013 03:19:46 +0200 > From: Davnobezimeni Sasha Balagura > To: tutor@python.org > Subject: [Tutor] Using tkinter::ttk::treeview to implement data view. > Message-ID: > < > caa7ixku8_kd9wsn+8xfqrfu9yhdmobgq1kxasn7fhxpt5ic...@mail.gmail.com> > Content-Type: text/plain; charset="iso-8859-1" > > Hello. > I have to implement parcer for .txt file with special formating. There are > a lot of data to display, modify, add. I decided to use > tkinter::ttk:treeviev for this. And it really good one, very fast and good > looking..but.. > But I cant set borders between cells, like in Excel. Is it real at all for > this widget? Or i've just overlooked something? > > Best regards, > Alex > -- next part -- > An HTML attachment was scrubbed... > URL: < > http://mail.python.org/pipermail/tutor/attachments/20131123/0599771c/attachment-0001.html > > > > -- > > Message: 7 > Date: Fri, 22 Nov 2013 16:08:25 -0600 > From: Paul Steele > To: tutor@python.org > Subject: [Tutor] numrows returning -1 > Message-ID: > < > cald6m00nu1usqd0oderb4sqtpwidinqrzwwxn15uudptax4...@mail.gmail.com> > Content-Type: text/plain; charset="iso-8859-1" > > Hey all... > > I am getting a numrows count of -1 when I print numrows (see line 23 of the > code). What does "-1" mean?I think my connection is working and my > table has data. > > Here's my code... > > #!/usr/bin/python > # -*- coding: utf-8 -*- > > import mysql.connector > from mysql.connector import errorcode > > try: > con = mysql.connector.connect(user='root', password='pw848596', > host='127.0.0.1', > database='mydb') > except mysql.connector.Error as err: > if err.errno == errorcode.ER_ACCESS_DENIED_ERROR: > print("Something is wrong with your user name or password") > elif err.errno == errorcode.ER_BAD_DB_ERROR: > print("Database does not exists") > else: > print(err) > else: > cur=con.cursor() > > cur.execute("select Name from Rosters") > numrows = cur.rowcount > print numrows > for x in xrange(0,numrows): > row = cursor.fetchone() > print "Name" > > con.close() > -- next part -- > An HTML attachment was scrubbed... > URL: < > http://mail.python.org/pipermail/tutor/attachments/20131122/bf7c39de/attachment.html > > > > -- > > Subject: Digest Footer > > ___ > Tutor maillist - Tutor@python.org > https://mail.python.org/mailman/listinfo/tutor > > > -- > > End of Tutor Digest, Vol 117, Issue 44 > ** > -- Warm regards. Satheesan Varier 860 (970) 2732 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Strange issue w/ Python shell 3.0.0.
Hej there, I noticed that sometimes when I do lots of modifications within a program, I get runtime errors even if the program is "clean". I realized that first time when I copy and pasted a program into a new window - it miraculously ran without any problems. Although it was exactly the same program that caused problems in the old window. Stupid rookie question: Does Python shell have a cache or so? And if that's the case - how do I empty it? Rafael ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Strange issue w/ Python shell 3.0.0.
On Sat, Nov 23, 2013 at 06:44:43PM +0100, Rafael Knuth wrote: > Hej there, > > I noticed that sometimes when I do lots of modifications within a > program, I get runtime errors even if the program is "clean". I > realized that first time when I copy and pasted a program into a new > window - it miraculously ran without any problems. Although it was > exactly the same program that caused problems in the old window. > > Stupid rookie question: Does Python shell have a cache or so? And if > that's the case - how do I empty it? ??? Your question unfortunately doesn't make a lot of sense to me. A cache for what? I suspect you are operating under an assumption which seems to make sense to you, but isn't actually correct. I think, if I were to answer your literal question, I would say "No", the Python shell doesn't have a cache. But the Python compiler has all sorts of caches, regardless of whether it is running in the shell or not. It caches modules. It caches all sorts of objects. But this shouldn't be a problem under normal circumstances. My guess is that it isn't a cache that is causing problems, but a misinterpretation of what you are seeing. But I could be wrong. Can you explain in detail what you mean by "when I do lots of modifications within a program", "runtime errors", and especially what you mean by calling the program "clean"? How are you making modifications to the program? How then do you see the errors? ... let me think... are you using reload()? Because that doesn't work like you might be thinking. When you reload a module, it doesn't affect objects which are already in use. Likewise with importing. But since I don't know how you are making modifications, or what you are doing that generates runtime errors, I can't tell what you're doing wrong. Oh, wait, I see you are using Python 3.0. Don't. Python 3.0 is not supported because it is buggy. You should use 3.1, or better still, 3.3. Python 3.3 is much better than 3.1 or 3.2, and 3.0 is buggy and slow. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Strange issue w/ Python shell 3.0.0.
> Oh, wait, I see you are using Python 3.0. Don't. Python 3.0 is not > supported because it is buggy. You should use 3.1, or better still, 3.3. > Python 3.3 is much better than 3.1 or 3.2, and 3.0 is buggy and slow. What I was trying to say is that sometimes I get runtime errors even if nothing's wrong with my code. I had those issues with Python 3.3.0 ... I wrote a program and when I executed it, and a runtime error occured. I then copy and pasted that into a new window, I saved it and - surprise, surprise - it ran without any issues. Although I didn't modify my program in any ways. That's why I came up with that question that might sound odd to you whether Python shell somehow "caches" older, buggy versions of my program which I saved previously ... ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Two subsequent for loops in one function - I got it!
@Peter @Steven @Don @Danny thank you *so much" for explaining the concept of a nested for loop! Your simplified example Steven made it very clear to me: for x in range(2, 7): print("outer loop, x =", x) for y in range(2, x): print("inner loop, x =", x, "y =", y) I have only one question left. Here's my original program again: for x in range(2, 10): for y in range(2, x): if x % y == 0: print(x, "equals", y, "*", x//y) break else: print(x, "is a prime number") So the first output of the outer loop is: 2. It's then passed to the inner loop: for y in range(2,x): if x % y == 0: ... And I was wondering what is happening inside that loop. The output of for y in range (2,2): should be ... none - correct? What exactly happens on the next line of code? if x % y == 0 To me it looks like if 2 % "no value" == 0 is executed here which I assume causes the loop to break - correct? Just want to understand how Python deals with "no values" within a program. Thanks in advance! All the best, Raf ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Two subsequent for loops in one function - I got it!
On Nov 23, 2013, at 2:57 PM, Rafael Knuth wrote: > > The output of > >for y in range (2,2): > > should be ... none - correct? No, it's not none. It's an empty list; thus, python executes nothing inside the inner loop. >>> range(2,2) [] >>> for y in range(2,2): ... print 'yes, I made it to here' ... >>> See? It has no output. By the way, the python REPL is your friend! Use it often when you can't figure out what is happening. Take care, Don ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Two subsequent for loops in one function - I got it!
> See? It has no output. By the way, the python REPL is your friend! Use it > often when you can't figure out what is happening. Oh, I didn't even know that such a thing exists :-) Cool! Unfortunately, I only found Python REPLs for version 2.7.2 or lower. Is there a REPL for 3.3.0 ..? Thanks, Raf ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Two subsequent for loops in one function - I got it!
On 23/11/13 20:54, Rafael Knuth wrote: See? It has no output. By the way, the python REPL is your friend! Unfortunately, I only found Python REPLs for version 2.7.2 or lower. Is there a REPL for 3.3.0 ..? The REPL (read–eval–print loop) is the >>> prompt. You type stuff in and Python reads it(R), evaluates it(E), prints the result(P) and then asks for more input to read(L). It exists in every version of Python and is frequently underestimated in its power to unravel what python is doing. In other words, just play with the interpreter... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Issue w/ while loops
On Sat, Nov 23, 2013 at 8:30 AM, Rafael Knuth wrote: > Marc, > > great feedback - thank you very much! > I will bear that in mind for the future. > > I modified my program as you suggested, but I received a runtime > error; I tried to fix that but unfortunately I didn't succeed. > The modified program executes well only when the user input has the > correct format (integer or float) and it breaks when the user enters a > string (at that point the program should print ("Invalid input") and > loop back to its beginning). > Can you please advise how I should change the program? > Thank you so much! > Raf > > Here's the original program: > > print("TIME TRACKING") > > while True: > hours_worked = input("How many hours did you work today? ") > try: > hours_worked = float(hours_worked) > break > except ValueError: > print ("Invalid input") > if hours_worked < 24: > print("You must be a human.") > else: > print("You must be a cyborg.") > > Here's the modified version: > > print("TIME TRACKING") > > hours_worked = "Invalid input" > while hours_worked == "Invalid input": > hours_worked = input("How many hours did you work today? ") > try: > hours_worked = float(hours_worked) > break > except ValueError: > print ("Invalid input") > if hours_worked < 24: > print("You must be a human.") > else: > print("You must be a cyborg.") > > And here's the error message I get: > > >>> > TIME TRACKING > How many hours did you work today? very long hours > Invalid input > Traceback (most recent call last): > File "C:\Users\Rafael_Knuth\Desktop\Rookies At > Work\Python\TimeReckord.py", line 12, in > if hours_worked < 24: > TypeError: unorderable types: str() < int() > >>> > The TypeError is basically just telling you that there's no meaningful way to compare the string "very long hours" with the integer 24. I should have recommended an exit condition more along the lines of checking whether hours_worked was still a string, like so: while isinstance(hours_worked, str) What I actually had in mind was to put the assignment to hours_worked inside the try/except - initialize it to a value that will keep you inside the loop, and only change its value to something that will escape the loop if the user enters a valid value. Like so: hours_worked = "Invalid input" while isinstance(hours_worked, str): try: hours_worked = float(raw_input("How many hours did you work today? ")) except ValueError: print ("Invalid input") if hours_worked < 24: print("You must be a human.") else: print("You must be a cyborg.") Note that I changed "input" to raw_input; I'm still using Python 2.7. If you're using Python 3, please disregard. In any case, the objective I was after was to make the "break" unnecessary. "Break"ing out of a loop should only happen in unusual cases; it shouldn't be the primary flow control. (Not because it's necessarily less efficient, or computationally wrong - but because a future maintainer will have to drill down into the loop to figure out what's happening.) If your IDE supports collapsing indented sections, it should be possible to understand the code flow without fully expanding it - or at least that's what I strive for. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Depth First Search Listing all possible combinations
On 23/11/13 21:30, Randolph Scott-McLaughlin II wrote: Inline image 2Inline image 1Hi Tutors, So I'm writing code for a depth first search where I have 1 start point and 1 end point. Between those points there are a series of 1-way roads that goes from one vertex to the next. Sometimes the vertex can have 4 or 5 one way road paths going into it or out of it. What I want to do is list each unique possible path someone can go from the start to end point, have the list written out and ideally labeled as "list'n'". Here's some code that I started off with but I keep getting errors What kind of errors? And if there are error messages please provide copies of the entire error message not just a summary. They are usually extremely informative. in general can't get it to do what I want it to do. What do you expect? What do you get? Don't force us to try to run it, guess what it should do, then assess what it is doing. Tell us. If you could guide me in the right direction or tell me how I can > alter the code I've been coming across to fit my needs that'd > be great. We can try but you need to tell us more detail too. I've also attached an image of the road map that I'm trying to develop, along with the decisional points that each vertex can take someone to and from. Thanks for help! It may be helpful to somebody but not to me! :-( #start=q9 q9 = (10, 33) q10 = (11, 28, 29, 30) q11 = (15) q16 = (17,19,24) q18 = (17, 19, 24) q24 = (25, 26) q27 = (34) q28 = (29, 30) q30 = (12, 31, 34) q32 = (34) q33 = (15, 30) q35 = (36, 37) q37 = (38, 39) q39 = (40, 41, 99) #end = 99 Could you do a smaller example that exhibits the problem? #trying new DFS code parent = {s:None} def DFS_VISIT(V, Adj,s): for v in Adj[s]: s.inprocess=True if v not in parent: s.inprocess=False parent[v]=s DFS_VISIT(V,Adj,s) You are using recursion but its not clear how you stop the recursion. Possibly when all elements of Adj[s] are not parents? But it looks lie you never use V and don't change Adj either. So I'm not sur4e how often this will recurse, but it may be more than the fixed limit compiled into Python. Is that one of the errors you get? #dfs visit code, controls checking notes around you def dfs(V,Adj): parent = {} for s in V: if s not in parent: parent[s] = None DFS_VISIT(V,Adj,s) Note that you define a new 'parent' here. This is local to this function and hides the parent defined above DFS. But DFS will continue to use the one defined at the top. Is that what you expected? Its usually better to create unique names to avoid confusion. I have no idea what the stuff below does, it's way too much for me to try to wade through. I can't see any sign of you calling dfs() or DFS_VISIT() so I don't know what the connection is. I gave up at that point. Sorry. import sys def extractPaths(current_node,path,loops_seen): path.append(current_node) # if node has outgoing edges if nodes[current_node]!=None: for thatnode in nodes[current_node]: valid=True # if the node we are going to has been # visited before, we are completeing # a loop. if thatnode-1 in path: i=len(path)-1 # find the last time we visited # that node while path[i]!=thatnode-1: i-=1 # the last time, to this time is # a single loop. new_loop=path[i:len(path)] # if we haven't seen this loop go to # the node and node we have seen this # loop. else don't go to the node. if new_loop in loops_seen: valid=False else: loops_seen.append(new_loop) if valid: extractPaths(thatnode-1,path,loops_seen) # this is the end of the path else: newpath=list() # increment all the values for printing for i in path: newpath.append(i+1) found_paths.append(newpath) # backtrack path.pop() # graph defined by lists of outgoing edges nodes=[[2],[3],[4],[5,9],[6,7],[7],[4,8],None,None] # I tried this but it didn't work nodes=zip(['q9','q10','q11','q16','q18','q24','q27','q28','q30','q32','q33','q35','q37','q39'],[(10,33),(11, 28, 29, 30), (15),(17,19,24),(25, 26),(34),(29, 30),(34),(15, 30),(36, 37),(38, 39),(40, 41, None)]) #also tried this but it ididn't work nodes = {1: [2, 3],2: [1, 4, 5, 6],3: [1, 4],4: [2, 3, 5],5: [2, 4, 6],6: [2, 5]} found_paths=list() extractPaths(0,list(),list()) for i in found_paths: print(i) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To
Re: [Tutor] Two subsequent for loops in one function - I got it!
On Sat, Nov 23, 2013 at 09:54:38PM +0100, Rafael Knuth wrote: > > See? It has no output. By the way, the python REPL is your friend! > > Use it often when you can't figure out what is happening. > > Oh, I didn't even know that such a thing exists :-) Cool! > Unfortunately, I only found Python REPLs for version 2.7.2 or lower. > Is there a REPL for 3.3.0 ..? Every version of Python has a REPL, also known as the interactive shell or interactive interpreter. That includes Jython and IronPython, and I presume others such as PyPy (which I have not used). If you launch Python from your operating system's command line with no arguments, like this: python or give the version number: python3.3 it will launch the interactive interpreter. Depending on your operating system, you may need to provide the full path to the executable. You might also like to investigate IPython and bpython, which are enhanced REPLs. IPython adds shell-like features and a Mathematica-like interface, and is mostly aimed at the scientific community. bpython adds colour and other goodies. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Two subsequent for loops in one function - I got it!
On Sat, Nov 23, 2013 at 08:57:54PM +0100, Rafael Knuth wrote: > I have only one question left. > Here's my original program again: > > for x in range(2, 10): > for y in range(2, x): > if x % y == 0: > print(x, "equals", y, "*", x//y) > break > else: > print(x, "is a prime number") > > So the first output of the outer loop is: 2. > It's then passed to the inner loop: > > for y in range(2,x): > if x % y == 0: > ... > > And I was wondering what is happening inside that loop. Absolutely nothing! The inner loop doesn't get executed. Python first generates the range object range(2, 2) which is empty (it starts at 2 and stops at 2). Since y iterates over an empty list, the loop immediately exits and the body gets executed zero times. > The output of > > for y in range (2,2): > > should be ... none - correct? Not quite "none", more like *nothing*. There's no output at all, because the body isn't executed even once. [...] > Just want to understand how Python deals with "no values" within a program. It doesn't. Things always have a value, if they are executed at all. But if they don't get executed, then they don't exist at all. if False: # This code never runs! print("This will never be printed") x = 23 print(x) At this point (unless x happened to have been defined even earlier in the code), trying to print x will cause a NameError -- the name 'x' doesn't exist. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Strange issue w/ Python shell 3.0.0.
On Sat, Nov 23, 2013 at 07:51:59PM +0100, Rafael Knuth wrote: > > Oh, wait, I see you are using Python 3.0. Don't. Python 3.0 is not > > supported because it is buggy. You should use 3.1, or better still, 3.3. > > Python 3.3 is much better than 3.1 or 3.2, and 3.0 is buggy and slow. > > What I was trying to say is that sometimes I get runtime errors even > if nothing's wrong with my code. > I had those issues with Python 3.3.0 ... I wrote a program and when I > executed it, and a runtime error occured. > I then copy and pasted that into a new window, I saved it and - > surprise, surprise - it ran without any issues. Although I didn't > modify my program in any ways. Are you perhaps using IDLE? Without seeing exactly what you mean, I'm not sure, but I *think* what is happening is something like this example. In Window #1, you have something like this: py> class Test: ... attr = 23 ... def test(): # oops a bug ... print(self.attr) ... py> x = Test() py> x.test() # failure! Traceback (most recent call last): File "", line 1, in TypeError: test() takes no arguments (1 given) py> py> # edit the Test class ... class Test: ... attr = 23 ... def test(self): # Fixed! ... print(self.attr) ... py> x.test() # but the error doesn't go away! Traceback (most recent call last): File "", line 1, in TypeError: test() takes no arguments (1 given) So then you copy and paste the Test class into Window #2, where it works perfectly: py> class Test: ... attr = 23 ... def test(self): # Fixed! ... print(self.attr) ... py> y = Test() py> y.test() 23 What's going on? It's not that Python has a cache, well, I suppose in a vague sense it does, but that *you* inadvertently have a cache. The problem is back in Window #1, where you have x = Test() with the buggy version of the method (missing the "self" argument). I must admit I kinda lied in the comment to this bit: py> # edit the Test class ... class Test: ... attr = 23 ... def test(self): # Fixed! ... print(self.attr) I didn't "edit the Test class" at all, although it looks like it. I actually created a NEW Test class, that looks the same except for the fixed bug. But x still belongs to the OLD Test class, so it still experiences the same bug, since as far as it is concerned, nothing has changed. What's going on here may be a little more clear if we avoid classes and just look at simple values: py> s = "Hello Wolrd!" # Oops a typo! py> motd = "The message of the day is: " + s py> print(motd) # oops, this is buggy The message of the day is: Hello Wolrd! py> s = "Hello World!" # Edit s to fix the typo. py> print(motd) # but motd has no way of knowing that The message of the day is: Hello Wolrd! Again, the same lie: I haven't actually edited s, and even if I had, it wouldn't effect motd. I've created a new s, which replaces the old one, but anything which was created from the old one will see no change. I can't tell if this is the problem you're experiencing, but from my experience as a beginner, it seems a reasonable chance. So, what to do about it? While the Python interactive interpreter is mighty powerful, it does have some limitations, and this is one of them. You just have to get used to the fact that it is not well-suited for editing large blocks of code. It is excellent for trying out small snippets, or for running blocks of code that don't have to change, but not for editing and running at the same time (at least not until you're experienced enough to deal with errors like this one). -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Strange issue w/ Python shell 3.0.0.
> So, what to do about it? While the Python interactive interpreter is > mighty powerful, it does have some limitations, and this is one of them. > You just have to get used to the fact that it is not well-suited for > editing large blocks of code. It is excellent for trying out small > snippets, or for running blocks of code that don't have to change, but > not for editing and running at the same time (at least not until you're > experienced enough to deal with errors like this one). Understood. Should I use a Python IDE like Pycharm or so? Would that help? If so, which ones would you recommend? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor