Re: [Tutor] How to... (a) write to files, (b) use try/except

2008-02-09 Thread Kent Johnson
Rob Stevenson wrote:

> Title = book.ProductName
> printstring = isbn + "\t" + book.ProductName + "\t" + 
> book.Authors.Author + "\tUsed from...\t" + book.UsedPrice +  "\tNew 
> price\t" + book.OurPrice
> print printstring
> print printstring >> fle
> except:
>ignorance='notBliss'
> fle.close

Should be fle.close(), you need the parentheses to make a function call.

> Now I have two problems
> 
> 1) the file c:\321.txt (which does exist) never gets written to.  I also 
> tried using
> 
> fle.write(printstring)
> 
> and fle.writeline(printstring) but they didn't do anything either.

Assuming that there are some results, it is probably the lack of a 
proper close that is the problem.
> 
> 2)  if any of the function calls from amazon.py, such as book.UsedPrice 
> fails then the rest of the details that may well have been retreived for 
> that get lost in the jump to the except block.  I'd really like to get 
> whatever info I can on the printstring assignment line and ignore any 
> errors, perhaps substituting 'none' instead of a retrieved value.

Instead of book.UsedPrice you can say getattr(book, 'UsedPrice', 'none') 
which will return the string 'none' if the attribute is not available.

> Also, a point of nettiquette for this list - how do I say thanks to 
> people - not to the list, but in direct emails?

I think either way is fine.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How to... (a) write to files, (b) use try/except

2008-02-09 Thread Rob Stevenson
Hello,

Sorry to be dense, but I'm having trouble writing to a file...

I have a list of book numbers - ISBNs in a file, one per line... (see list
at bottom of this message)

my code below...

import amazon
amazon.setLicense(' -- my amazon id is in here when I run - ')

isbn_list = "c:\\ISBN.txt"
amazon.setLocale("uk")
fle=open("c:\\321.txt",'a')

for isbn in file(isbn_list):

isbn = isbn.strip()
try:
books=amazon.searchByKeyword(isbn)
 except:
x=1
for book in books:
try:
Title = book.ProductName
printstring = isbn + "\t" + book.ProductName + "\t" +
book.Authors.Author + "\tUsed from...\t" + book.UsedPrice +  "\tNew price\t"
+ book.OurPrice
print printstring
print printstring >> fle
except:
   ignorance='notBliss'
fle.close


Now I have two problems

1) the file c:\321.txt (which does exist) never gets written to.  I also
tried using

fle.write(printstring)

and fle.writeline(printstring) but they didn't do anything either.

2)  if any of the function calls from amazon.py, such as
book.UsedPricefails then the rest of the details that may well have
been retreived for
that get lost in the jump to the except block.  I'd really like to get
whatever info I can on the printstring assignment line and ignore any
errors, perhaps substituting 'none' instead of a retrieved value.

Any help very appreciated.

Also, a point of nettiquette for this list - how do I say thanks to people -
not to the list, but in direct emails?

Thanks all,
Rob

==the ISBNs from my file ===


0006755151
059304083X
0349112150
0141030143
0099740915
0552773123
0747590087
0571224121
0552997706
0099490730
0141009128
0670852473
0044409672
0722540523
0375720979
1862074534
0571169945
08685506
0330481886
0333618815
057120273X
0099526409
0753509024
0099476649
0316857912
0007179731
014027443X
000637977X
1568360231
1844832007
1844832058
1844832023
1846270057
0810112434
0141022280
0552152293
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How to (a) write to files, (b) use try/except (clarification)

2008-02-09 Thread Rob Stevenson
by amazon ID in my message I meant my web services license number.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor