pytho_14.ppt

  1. 1、本文档共39页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
pytho_14

Chapter 14 – File Processing and Serialization 14.1 Introduction Files provide long-term retention of large amounts of data Persistent data maintained in files Files stored on secondary storage devices, such as magnetic disks, optical disks and tapes 14.2 Data Hierarchy Data items processed by computers form data hierarchy in which data items become larger, more complex in structure in progression from bits to bytes to characters to fields, etc. Smallest computer-supported data item bit (short for “binary digit”) either 0 or 1 1 byte = 8 bits Preferable to program with characters (digits, letters and special symbols) Fields composed of characters (or bytes) Record (typically, tuple, dictionary or instance) composed of several fields 14.2 Data Hierarchy File: group of related records Record key distinguishes individual records 14.2 Data Hierarchy 14.3 Files and Streams Python views files as sequential streams of bytes Each file ends with an end-of-file marker or a specific byte number recorded in system-maintained administrative data structure Opening a file creates an object associated with a stream Three file streams created when Python program executes – sys.stdin (standard input stream), sys.stdout (standard output stream) and sys.stderr (standard error stream) 14.3 File and Streams 14.4 Creating a Sequential-Access File Python imposes no file structure fig14_03.py 1 # Fig. 14.3: fig14_03.py 2 # Opening and writing to a file. 3 4 import sys 5 6 # open file 7 try: 8 file = open( clients.dat, w ) # open file in write mode 9 except IOError, message: # file open failed 10 print sys.stderr, File could not be opened:, message 11 sys.exit( 1 ) 12 13 print Enter the account, name and balance. 14 print Enter end-of-file to end input. 15 16 while 1: 17 18 try: 19 accountLine = raw_input( ? ) # get account entry 20 except EOFError: 21 break # u

文档评论(0)

af60068 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档