[Tutor] user input help

2011-01-05 Thread Jason Staudenmayer
Hi all, I'm pretty new to programming in general and figured I'd try out 
python. 
I'm working on a small program to add users to a sqlite db. The problem I'm 
having it dealing with the user input, I'd like to be able to repeat the 
function to get the input if the user doesn't accept it.

here's the code I have now:

def promptInput():
""" Get employee data from user running this program"""

lname = raw_input("Please enter employees last name\n")
fname = raw_input("Please enter employees first name\n")
email = raw_input("Please enter employee email address (or press enter to \
leave blank)\n")
result = (lname, fname, email)
return result

def getEmplyInfo():
# get the data from input
result = promptInput()
# print the data so the user can check and verify spelling
print "Is the following info correct [y/n]\n%s, %s %s" % (result[1], \
result[0], result[2])
check = raw_input()
#see if the user needs to make corrections to the data he entered
if check == "y":
print "this check is done so we can add user"
print "%s, %s %s" % (result[1], result[0], result[2])
else:
check = ""
promptInput()

The if else loop is were I'm loosing it. If I select n it will ask for the 
input 
again but only once. If on the second time around I enter n to re-do it just 
exits.



Thanks

Jason
 
 
 
..·><º>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] user input help

2011-01-05 Thread Jason Staudenmayer
> -Original Message-
> From: Alex Hall [mailto:mehg...@gmail.com] 
> Sent: Wednesday, January 05, 2011 3:23 PM
> To: Jason Staudenmayer
> Cc: tutor@python.org
> Subject: Re: [Tutor] user input help
> 
> 
> On 1/5/11, Jason Staudenmayer  wrote:
> > Hi all, I'm pretty new to programming in general and 
> figured I'd try out
> > python.
> > I'm working on a small program to add users to a sqlite db. 
> The problem I'm
> > having it dealing with the user input, I'd like to be able 
> to repeat the
> > function to get the input if the user doesn't accept it.
> >
> > here's the code I have now:
> >
> > def promptInput():
> > """ Get employee data from user running this program"""
> >
> > lname = raw_input("Please enter employees last name\n")
> > fname = raw_input("Please enter employees first name\n")
> > email = raw_input("Please enter employee email address 
> (or press enter
> > to \
> > leave blank)\n")
> > result = (lname, fname, email)
> > return result
> >
> > def getEmplyInfo():
> > # get the data from input
> > result = promptInput()
> > # print the data so the user can check and verify spelling
> > print "Is the following info correct [y/n]\n%s, %s %s" 
> % (result[1], \
> > result[0], result[2])
> > check = raw_input()
> > #see if the user needs to make corrections to the data 
> he entered
> > if check == "y":
> > print "this check is done so we can add user"
> > print "%s, %s %s" % (result[1], result[0], result[2])
> > else:
> > check = ""
> > promptInput()
> >
> > The if else loop is were I'm loosing it. If I select n it 
> will ask for the
> > input
> > again but only once. If on the second time around I enter n 
> to re-do it just
> > exits.
> This is because the function is done once it detects the y or n, so
> after you enter the n, one of those if/else statements has fired, and
> the function has nothing else to do. You will want a while loop,
> something like:
> 
> repeat=True
> while repeat:
>  answer=raw_input("Is the data okay?")
>  if answer=="y": repeat=False
>   else:
>promptInput()
>repeat=True
> 
> Anyway, something along those lines. Look in the manual for while
> loops. Basically, they are a way to repeat an action until a condition
> is met. You will also run across for loops, which are mostly used for
> repeating an event a set number of times. You can use them
> interchangeably, but they each have situations where one works better
> than the other, and you want a while loop here.
> >
> >
> >
> > Thanks
> >
> > Jason
> >
> >
> >
> > ..·><º>
> > ___
> > Tutor maillist  -  Tutor@python.org
> > To unsubscribe or change subscription options:
> > http://mail.python.org/mailman/listinfo/tutor
> >
> 
> 
> -- 
> Have a great day,
> Alex (msg sent from GMail website)
> mehg...@gmail.com; http://www.facebook.com/mehgcap
> 

That works great!! I forgot about while loops. It's been a while since I've 
used then.

Thanks much

Jason
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] shlex.split if there is an apostrophe in the string?

2011-01-07 Thread Jason Staudenmayer

> -Original Message-
> From: tutor-bounces+jasons=adventureaquarium@python.org 
> [mailto:tutor-bounces+jasons=adventureaquarium@python.org]
>  On Behalf Of Sean Carolan
> Sent: Friday, January 07, 2011 2:22 PM
> To: Tutor@python.org
> Subject: [Tutor] shlex.split if there is an apostrophe in the string?
> 
> 
> I'm practicing manipulating data with a text file.  I'm trying to use
> shlex.split to break up each line, which works great until it gets to
> the first apostrophe:
> 
> fin = open('huckfinn.txt')
> startstring = 'START OF THIS PROJECT'
> 
> for line in fin:
> print line
> words = shlex.split(line)
> 
> This is the line is where it's choking, complaining that there is no
> closing quote:
> 
> YOU don't know about me without you have read a book by the 
> name of The
> 
> How can I get shlex.split to ignore single apostrophes such 
> as the one above?
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 
WARNING Total newbie here.

Looks like you need to filter you data to excape the quote marks. I'm just 
learning so I'm not sure how to do that yet.

Jason
 
 
 
..·><º>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how avoid writing a newline?

2011-01-12 Thread Jason Staudenmayer
You should have a comma after the "#" in the highlighted print statement, that 
should repress the new line (I'm guessing that's the line you're talking about)
 
print >> f,t,"#",
 
Jason
 
 
 
..·><º>
 -Original Message-
From: tutor-bounces+jasons=adventureaquarium@python.org 
[mailto:tutor-bounces+jasons=adventureaquarium@python.org] On Behalf Of 
Tommy Kaas
Sent: Wednesday, January 12, 2011 10:40 AM
To: tutor@python.org
Subject: [Tutor] how avoid writing a newline?



I'm using Activepython 2.6.6 on PC/Win7

 

I have made a small scraper script as an exercise for myself. 

It scrapes the name and some details of the first 25 billionaires on 
the Forbes list.

It works and write the result in a text file, with the columns 
separated by "#"

It takes the name from the link (t = i.string) - open the link and 
scrape details from the next page.

But I can't find a way to write the name (the variable t) one and only 
one time in the beginning of the line.

As t is written now I get it in the beginning of the line but I also 
get a newline. 

Can I avoid that in a simple way?

 

Thanks in advance for any help

Tommy

 

 

from BeautifulSoup import BeautifulSoup

from mechanize import Browser

f = open("forbes.txt", "w")

br = Browser()

url = 
"http://www.forbes.com/lists/2010/10/billionaires-2010_The-Worlds-Billionaires_Rank.html";

page = br.open(url)

html = page.read()

soup = BeautifulSoup(html)

table = soup.find("table")

l = table.findAll('a')

for i in l[5:]:

t = i.string

print t #to the monitor



br.follow_link(text_regex=r"(.*?)"+t+"(.*?)")

tekst = br.response().read()

soup = BeautifulSoup(tekst)

table1 = soup.find('table', id='billTable')

rows = table1.findAll('tr')

print >> f, t,"#" 

for tr in rows:

tds = tr.findAll(text=True)

print >> f, tds[1].string,"#",tds[2].string,"#", 

print >> f, '\r\n'

 

f.close()

 

  

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] no luck with sqlinsert

2011-01-14 Thread Jason Staudenmayer
Don't build you sql separate from the execute (or so I was told when I was 
doing something similar)
 
cur.execute(INSERT INTO tkindbtal (kommune, komnr, i2005, i2006, i2007 \
, i2008, i2009, i2010) VALUES (%s,  %s,%s,  %s,   %s, %s,\
%s,  %s) % (cols[0], cols[1], int(cols[2]), int(cols[3]), int(cols[4]), \
int(cols[5]), int(cols[6]), int(cols[7]))



 

Jason
 
 
 
..·><º>

-Original Message-
From: tutor-bounces+jasons=adventureaquarium@python.org 
[mailto:tutor-bounces+jasons=adventureaquarium@python.org] On Behalf Of 
Tommy Kaas
Sent: Friday, January 14, 2011 11:43 AM
To: tutor@python.org
Subject: [Tutor] no luck with sqlinsert



I get a invalid syntax error when I try to run this script - and it's 
con.commit() which is highlighted when I get the error.

I can't see what is wrong. I think it looks like other scripts I'm 
running without problems. The scraping part works fine. And the table exists in 
the mysql db. I have just separated in an attempt to locate the problem. But no 
luck. I hope someone can spot the problem.

(ActivePython 2.6.6. on pc/win)

TIA

 

import urllib2

import MySQLdb

from BeautifulSoup import BeautifulSoup

 

con = MySQLdb.connect(host='mysql2.dicar.dk', user='python1', 
passwd='python1', db='python')

cur = con.cursor()

 

sqlinsert = '''

INSERT INTO tkindbtal (kommune, komnr, i2005, i2006, i2007, i2008, 
i2009, i2010)

VALUES   (%s,  %s,%s,  %s,   %s, %s,
 %s,  %s) 

'''

 

soup = 
BeautifulSoup(urllib2.urlopen('http://www.kaasogmulvad.dk/unv/python/kom_indbtal.htm').read())

 

rows = soup.findAll('tr')

print rows

 

for tr in rows[1:]:

cols = tr.findAll('td')

try:

cur.execute(sqlinsert, (cols[0], cols[1], int(cols[2]), 
int(cols[3]), int(cols[4]), int(cols[5]), int(cols[6]), int(cols[7]))

con.commit()

except:

con.rollback()

 

con.close()

 

 

Tommy Kaas

 

 

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor