Re: [Tutor] How to format JSON output within script
On Tue, Aug 18, 2009 at 1:02 PM, Vincent Gulinao wrote: > I see you could pipe your output to 'python -mjson.tool', but how do I > achieve the same within my script? > > TIA. > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > There is a JSON module in Python...the examples in the docs are pretty self explanatory: http://docs.python.org/library/json.html Can you give a bit of an example of what you're trying to do? -Mal W ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problems with encoding in BeautifulSoup
On Tue, Aug 18, 2009 at 9:00 AM, Eduardo Vieira wrote: > Hello, I have this sample script from beautiful soup, but I keep > getting an error because of encoding. I have google for solutions but > I don't seem to understand. Even this is dealt in Beautiful Soup's doc > but I am not able to understant/apply the solution successfully. > > from BeautifulSoup import BeautifulSoup > import urllib2 > page = urllib2.urlopen('http://www.yellowpages.ca/search/si/1/Signs/QC') > > # if I change the url to > http://www.yellowpages.ca/search/si/3/Signs/ON, it works because > there's no french words... > > soup = BeautifulSoup(page) > > companies = soup('h2') > > print soup.originalEncoding > > print companies[:4] > > However, if I do this, I don't get errors even when there are accents: > for title in companies: > print title > > Here is the Error output: > utf-8 > Traceback (most recent call last): > File "C:\myscripts\encondingproblem.py", line 13, in > print companies[:4] > UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in > position 373: ordinal not in range(128) > > === > Thanks in advance. > > Eduardo > www.expresssignproducts.com > ___ > Tutor maillist - tu...@python.org > http://mail.python.org/mailman/listinfo/tutor > It's caused by Python not wanting to send non-ASCII characters to your terminal. To override this you need to create a sitecustomize.py file in your /usr/lib/python/ folder and put the following in it: import sys sys.setdefaultencoding("utf-8") This will set the default encoding in Python to UTF8 and you should stop getting these parsing errors. I dealt with this recently when I was playing around with some international data. -Mal W ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Web framework: looking for python-tutor's angle.
On Fri, Aug 14, 2009 at 4:09 AM, Mac Ryan wrote: > A couple of months ago I took the time to read a few articles on python > web application frameworks and I got the impression that the two most > mature and active projects are Zope and Django. > > Zope vs. Django hits 879.000 pages on google but much of the debate - or > at least this is my impression - falls into the class "vi vs. emacs" or > "gtk vs. qt" with many people singling out a single characteristics that > for them is THE characteristic making one framework better than the > other. > > This [1] graph seems to corroborate my final impression (i.e. that > django is the way to go). Yet, I would be very interested in hearing > what the members of this list think, as I particularly enjoy the > "learner centered" approach that most of the people seems to have here. > > I believe my needs are quite ordinary: my customers are typically small > businesses needing to process their data on a single server, sometime > exposing part of the application as front-end to the customers (hence > easy and flexible theming is important). I would definitively be happy > to sacrifice some functionality in exchange for a leaner and cleaner > design (i.e. more modular, elegant and intuitive), though. > > Thank you in advance for your time, > Mac. > > [1] http://www.google.com/trends?q=python+zope%2C+python+django > > ___ > Tutor maillist - tu...@python.org > http://mail.python.org/mailman/listinfo/tutor > Your needs definitely favour Django. The learning curve is so much shallower for Django and it's very simple to theme not to mention a much more beginner-tolerant community IMHO. -Mal W ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Ptyhon GUI doubt
Hi Raj, I used to get this error sometimes when I was using Windows. Killing the Python process via Task Manager and restarting IDLE was enough to get me going but if it still remains an issue, you can run Python in single process mode, the following is snipped from IDLE help: "Running without a subprocess: If IDLE is started with the -n command line switch it will run in a single process and will not create the subprocess which runs the RPC Python execution server. This can be useful if Python cannot create the subprocess or the RPC socket interface on your platform. However, in this mode user code is not isolated from IDLE itself. Also, the environment is not restarted when Run/Run Module (F5) is selected. If your code has been modified, you must reload() the affected modules and re-import any specific items (e.g. from foo import baz) if the changes are to take effect. For these reasons, it is preferable to run IDLE with the default subprocess if at all possible. " On Mon, Aug 24, 2009 at 9:28 PM, Reddy Etikela, Rajasekhar wrote: > Hi, > > I am new to the Python. I have installed Python 2.6.2 version in windows XP. > > When I try to open the IDLE(Python GUI), getting the below message. Any > configuration settings required for this? > > > Please let me know the details. > > Thanks, > Raj > ___ > Tutor maillist - tu...@python.org > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] web2py vs django
On Thu, Sep 10, 2009 at 5:29 AM, Alan Gauld wrote: > > "Kent Johnson" wrote >> >> That thread is about web.py, not web2py, they are different frameworks. > > I confess I too had missed that subtlety! > Like editors and standards... > The wonderful thing about Web Frameworks in Python - there are so many to > choose from! > > But it is confusing, my advice is just pick one and stick with it! > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > ___ > Tutor maillist - tu...@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > That is the advice that certainly worked for me. For a long time I was switching between web frameworks, as each have their nuances which make them seem better than the others, and what you end up with is a bit of knowledge on each...but not enough on any single one to get the full power out of it. There is always risk involved with backing a single horse, but I found it massively beneficial when I did. In the end I settled with Pylons because I like it's lack of glue between components but I'm positive I could have done all of my projects in any of the frameworks and each would have had their challenges and successes. Kudos to Massimo though; web2py is a spiffy idea and it's implemented well. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using the time module to extract a semi-random number
Hi Katt, The following does the trick for me: number = int(time.strftime("%S", time.localtime())) There may be better ways to do it though. -Mal On Thu, Sep 17, 2009 at 8:43 AM, Laurii wrote: > Hello all, > > I am currently reading through the Tutorial for Non-Programers by Josh > Cogliati. I have had great success until now. > > The exercise to modify a number guessing program from a fixed number "number > = 78" to using the time module and use the seconds at the time the program > is used to be the number. (i.e. if the clock on your computer says 7:35:25 > then it would take the 25 and place it in "number". > > The following is what I have so far: > #* > # > # hilow2.py > # > # This program asks the user to guess a > # number. If it is wrong the program tells > # the user if it is higher or lower. The > # first one had a fixed assigned number. > # This program takes the last two digits > # of the time and assigns it as the random > # number. > # > #* > #*.h header files** > #* > #**modules* > from time import time, ctime > #* > #**define lists/dictionaries** > #* > #define functions* > #* > #*define global variables*** > the_time = ctime() > number = the_time > guess = 0 > guess_try = 0 > #* > #Main Program** > while guess != number: > guess = input("Guess a number:") > if guess != number: > if guess > number : > print "To High" > elif guess < number: > print "To Low" > guess_try = guess_try + 1 > print "Bingo! You are correct." > print "It only took you",guess_try,"tries." > #* > > I think it has to do with the fact that I am using the wrong time function > and am not familiar with how to cut out the seconds and send it to the > variable number. > > I believe I should use the strftime(format[,t]) but am not sure how to. All > help is appreciated. > > Thanks in advance, > > Katt > ___ > Tutor maillist - tu...@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] ex-ftp
On Tue, Sep 22, 2009 at 2:39 PM, prasad rao wrote: > hello friends > I am trying to write a class to save a url.page. > But it is not working.It is saving the html.page.But not getting > images.I am unable to show the list (object.links). > Please take a look at it and show me how to rectify it. > > import urllib2,ftplib,re > class Collect: > def __init__(self,parent): > self.parent=parent > self.links=[] > self.ims=[] > s=urllib2.urlopen(self.parent) > data=s.read() > self.data=data > a=re.compile ('<[aA].*[\'"](.*)[\'"].*>'); b=re.compile(' img[\'"](.+)[\'"].*') > try: > z=re.search(a,self.data).group(1) > self.links.extend(z) > except:pass > try: > y=re.search(b,self.data).group(1) > self.ims.extend(y) > except:pass > return > > def save(self,data): > d=open('C:/%s .html'%self.parent[10:15],'w') > d.write(data) > return > def bring(self): > ftp=ftplib.FTP(self.parent) > ftp.login() > for x in self.ims: > data=ftp.retlines(x) > d=open('C:/%s'%x,'w') > d.write(data) > return > > def show(self,z): > for x in z: > print x > return > > > c=Collect('http://www.asstr.org') > c.save(c.data) > c.bring() > #c.show(c.ims) > c.links > > Thanks in advance. > ___ > Tutor maillist - tu...@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > Might be nice to mention to all who access these emails from work that the site this script is scraping is not safe for work. -Mal ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor