您的位置:首页 > 编程语言 > Python开发

《core python programming 》第二十三章的习题的部分解答,自己做的,错误肯定难免的

2013-05-05 20:07 706 查看
23-1. Web Services. Take the Yahoo! stock quote example (stock.py) and change the application to save the quote data to a file instead of displaying it to the screen.Optional: You may change the script so that users can choose to display the quote data or save it to a file.

#!/usr/bin/env python
from time import ctime
from urllib import urlopen
import os

ticks = ('YHOO', 'GOOG', 'EBAY', 'AMZN')
RANGE = range(3, 8)
URL = 'http://quote.yahoo.com/d/quotes.csv?s=%s&f=sl1c1p2'
TICKS = ('YHOO', 'GOOG', 'EBAY', 'AMZN','AMD')
COLS = ('TICKER', 'PRICE', 'CHG', '%AGE')
fobj = open('c:/101.txt', 'w')
fobj.write('Python-to-stock\r\n\r\n')
fobj.write('Prices quoted as of: %s\r\n' % ctime())
for i in range(4):
fobj.write('%s\t' % COLS[i])
fobj.write('\n')
u = urlopen(URL % ','.join(TICKS))
for data in u:
tick, price, chg, per = data.split(',')
fobj.write('%s\t' %eval(tick))
fobj.write('%.2f\t' % round(float(price),2))
fobj.write('%s\t' % chg)
fobj.write('%s\t\r\n' % eval(per.rstrip()))
print 'well done'
fobj.close()


23-2. Web Services. Update the Yahoo! stock quote example (stock.py) to download other stock quote data given the additional parameters listed above. Optional: You may add this feature to your solution to the above exercise.

#!/usr/bin/env python
from time import ctime
from urllib import urlopen
import os

ticks = ('YHOO', 'GOOG', 'EBAY', 'AMZN')
RANGE = range(3, 8)
URL = 'http://quote.yahoo.com/d/quotes.csv?s=%s&f=sl1c1p2oh'
TICKS = ('YHOO', 'GOOG', 'EBAY', 'AMZN')
COLS = ('TICKER', 'PRICE', 'CHG', '%AGE','LOP','DHP')
filename = raw_input('Enter file name: ')
fobj = open('c:/102.txt', 'w')
fobj.write('Python-to-stock\r\n\r\n')
fobj.write('Prices quoted as of: %s\r\n' % ctime())
for i in range(6):
fobj.write('%s\t' % COLS[i])
fobj.write('\n')
u = urlopen(URL % ','.join(TICKS))
for data in u:
tick, price, chg, per, lop, dhp= data.split(',')
fobj.write('%s\t' %eval(tick))
fobj.write('%.2f\t' % round(float(price),2))
fobj.write('%s\t' % chg)
fobj.write('%s\t' % eval(per.rstrip()))
fobj.write('%s\t' % lop)#这里是字符型不是如书上说的numberic
fobj.write('%s\t\r\n' % dhp)#这里是字符型不是如书上说的numberic
print 'well done'
fobj.close()


23-3.
Web Services and the csv Module. Convert stock.py to using the csv module to parse the incoming data, like we did in the
example code snippet. Extra Credit: Do the same thing to the Excel version of this script(estock.py).

#!/usr/bin/env python
import csv
from time import ctime
from urllib import urlopen

ticks = ('YHOO', 'GOOG', 'EBAY', 'AMZN')
URL = 'http://quote.yahoo.com/d/quotes.csv?s=%s&f=sl1c1p2'

print '\nPrices quoted as of:', ctime()
print '\nTICKER'.ljust(9), 'PRICE'.ljust(8), 'CHG'.ljust(5), '%AGE'
print '------'.ljust(8), '-----'.ljust(8), '---'.ljust(5), '----'
u = urlopen(URL % ','.join(ticks))

for tick, price, chg, per in csv.reader(u):
print tick.ljust(7), ('%.2f' % round(float(price),2)).rjust(6), chg.rjust(6), per.rjust(6)
u.close()


23-7.
Microsoft
Office Applications and Web Services. Interface to any existing Web service,whether REST- or URL-based, and write data to an Excel spreadsheet or format thedata nicely into a Word document. Format them properly for printing. Extra Credit:Support both Excel and Word

#!/usr/bin/env python
from Tkinter import Tk
from time import sleep, ctime
from tkMessageBox import showwarning
from urllib import urlopen
import win32com.client as win32

warn = lambda app: showwarning(app, 'Exit?')
RANGE = range(3, 8)
TICKS = ('YHOO', 'GOOG', 'EBAY', 'AMZN')
COLS = ('TICKER', 'PRICE', 'CHG', '%AGE')
URL = 'http://quote.yahoo.com/d/quotes.csv?s=%s&f=sl1c1p2'

def excel():
app = 'Excel'
xl = win32.gencache.EnsureDispatch('%s.Application' % app)
ss = xl.Workbooks.Add()
sh = ss.ActiveSheet
xl.Visible = True
sleep(1)

sh.Cells(1,1).Value = 'Python-to-%s Stock Quote Demo' % app
sleep(1)
sh.Cells(3, 1).Value = 'Prices quoted as of: %s' % ctime()
sleep(1)
for i in range(4):
sh.Cells(5, i+1).Value = COLS[i]
sleep(1)
sh.Range(sh.Cells(5, 1), sh.Cells(5, 4)).Font.Bold = True
sleep(1)
row = 6

u = urlopen(URL % ','.join(TICKS))
for data in u:
tick, price, chg, per = data.split(',')
sh.Cells(row, 1).Value = eval(tick)
sh.Cells(row, 2).Value = ('%.2f' % round(float(price), 2))
sh.Cells(row, 3).Value = chg
sh.Cells(row, 4).Value = eval(per.rstrip())
row += 1
sleep(1)
f.close()

warn(app)
ss.Close(False)
xl.Application.Quit()

if __name__=='__main__':
Tk().withdraw()
excel()


#!/usr/bin/env python
from Tkinter import Tk
from time import sleep, ctime
from tkMessageBox import showwarning
from urllib import urlopen
import win32com.client as win32

warn = lambda app: showwarning(app, 'Exit?')
RANGE = range(3, 8)
TICKS = ('YHOO', 'GOOG', 'EBAY', 'AMZN')
COLS = ('TICKER', 'PRICE', 'CHG', '%AGE')
URL = 'http://quote.yahoo.com/d/quotes.csv?s=%s&f=sl1c1p2'

def word():
app = 'Word'
word = win32.gencache.EnsureDispatch('%s.Application' % app)
doc = word.Documents.Add()
word.Visible = True
sleep(1)

rng = doc.Range(0,0)
rng.InsertAfter('Python-to-%s stock\r\n\r\n' % app)
sleep(1)
rng.InsertAfter('Prices quoted as of: %s\r\n' % ctime())
sleep(1)
for i in range(4):
rng.InsertAfter('%s\t' % COLS[i])
rng.InsertAfter('\r\n')
sleep(1)
row=6
u = urlopen(URL % ','.join(TICKS))
for data in u:
tick, price, chg, per = data.split(',')
rng.InsertAfter('%s\t' %eval(tick))
rng.InsertAfter('%.2f\t' % round(float(price),2))
rng.InsertAfter('%s\t' % chg)
rng.InsertAfter('%s\t\r\n' % eval(per.rstrip()))
sleep(1)
f.close()

warn(app)
doc.Close(False)
word.Application.Quit()

if __name__=='__main__':
Tk().withdraw()
word()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: