《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.
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.
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).
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 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()
相关文章推荐
- Core Python Programming一书中关于深浅拷贝的错误
- 《快学Scala》第20章部分习题参考解答(Actor)
- Core Python Programming笔记,Note
- 马知恩周义仓编常微分方程定性与稳定性方法部分习题参考解答
- 数分一(大一) 练习题1.1-部分习题解答
- 《C语言程序设计(第二版新版)》第一章习题解答(部分)
- Python学习日志 - 3.列表简介(习题解答)
- Python学习日志 - 4.操作列表(习题解答)
- 大一上:计算机导论复习(每章分值、常考题型以及部分章节课后习题答案【网上没有,老师自己做的】)
- python学习第五章操作列表部分课后练习自己尝试的代码
- linux基础知识必掌握知识[自己原来上学总结的难免有错误,多谢指点]
- C++primer第五版第八章部分习题解答
- Python学习日志 - 6.字典(习题解答)
- Python教材第九章部分习题
- 算法笔记_123:蓝桥杯第七届省赛(Java语言B组部分习题)试题解答
- 《linux c 编程一站式学习》课后部分习题解答
- 机器学习(周志华)习题解答4.3: Python小白详解ID3决策树的实现
- Python学习日志 - 7.用户输入和while循环(习题解答)
- 写个C和指针习题程序犯太多错误,记下提醒自己
- Coursera公开课Functional Programming Principles in Scala习题解答:Week 2