|
Python中文件处理和序列化
1.文件和流
Python将每个文件都视为一个顺序的字节流。每个文件都结束于一个EOF(文件尾)标记,或结束于一个特定字节编号(由系统维护的一个管理数据结构记 录)。程序“打开"文件时,Python会创建一个对象,并将一个”流“与那个对象关联。 Python程序开始执行时,会创建3个文件流,包括sys.stdin,sys.stdout,sys.stderr。这些流在程序和特定文件/设备之间建立了沟通渠道。
2.创建顺序访问文件
|
# Fig. 14.3: fig14_03.py # Opening and writing to a file.
import sys
# open file try: file = open( "clients.dat", "w" ) # open file in write mode except IOError, message: # file open failed print >> sys.stderr, "File could not be opened:", message sys.exit( 1 )
print "Enter the account, name and balance." print "Enter end-of-file to end input."
while 1:
try: accountLine = raw_input( "? " ) # get account entry except EOFError: break # user entered EOF else: print >> file, accountLine # write entry to file file.close() |
模式 说明
"a" 所有输出都写到文件尾,如果指定的文件不存在,就创建一个 "r" 打开文件以便输入,如果文件不存在,就引发IOError异常 "r+" 打开文件以便输入和输出,如果文件不存在,就引发IOError异常 "w" 打开文件以便输出,如果文件存在,就删除其中所有数据,如果文件不存在,就创建一个 "w+" 打开文件以便输入和输出,如果文件存在,就删除其中所有数据,如果文件不存在,就创 建一个 "ab","rb","r+b" 打开文件以便进行二进制(也就是非文本形式)输入或输出。注意,只有在Windows和 "wb","w+b" Macintosh平台才支持这些模式 常用的文件对象的方法有close(),flush()等,可在Python中输入help(file)来获取更详细的信息。
3.从顺序访问文件读取数据
|
# Fig. 14.6: fig14_06.py # Reading and printing a file.
import sys
# open file try: file = open( "clients.dat", "r" ) except IOError: print >> sys.stderr, "File could not be opened" sys.exit( 1 ) records = file.readlines() # retrieve list of lines in file
print "Account".ljust( 10 ), print "Name".ljust( 10 ), print "Balance".rjust( 10 )
for record in records: # format each line fields = record.split() print fields[ 0 ].ljust( 10 ), print fields[ 1 ].ljust( 10 ), print fields[ 2 ].rjust( 10 )
file.close() |
|
# Fig. 14.7: fig14_07.py # Credit inquiry program.
import sys
# retrieve one user command def getRequest(): while 1: request = int( raw_input( "\n? " ) ) if 1 <= request <= 4: break
return request
# determine if balance should be displayed, based on type def shouldDisplay( accountType, balance ):
if accountType == 2 and balance < 0: # credit balance return 1
elif accountType == 3 and balance > 0: # debit balance return 1
elif accountType == 1 and balance == 0: # zero balance return 1
else: return 0
# print formatted balance data def outputLine( account, name, balance ): print account.ljust( 10 ), print name.ljust( 10 ), print balance.rjust( 10 )
# open file try: file = open( "clients.dat", "r" ) except IOError: print >> sys.stderr, "File could not be opened" sys.exit( 1 )
print "Enter request" print "1 - List accounts with zero balances" print "2 - List accounts with credit balances" print "3 - List accounts with debit balances" print "4 - End of run"
# process user request(s) while 1:
request = getRequest() # get user request
if request == 1: # zero balances print "\nAccounts with zero balances:" elif request == 2: # credit balances print "\nAccounts with credit balances:" elif request == 3: # debit balances print "\nAccounts with debit balances:" elif request == 4: # exit loop break else: # getRequest should never let program reach here print "\nInvalid request."
currentRecord = file.readline() # get first record
# process each line while ( currentRecord != "" ): account, name, balance = currentRecord.split() balance = float( balance ) if shouldDisplay( request, balance ): outputLine( account, name, str( balance ) )
currentRecord = file.readline() # get next record
file.seek( 0, 0 ) # move to beginning of file print "\nEnd of run." file.close() # close file |
4.将数据写入shelve文件
|
# Fig. 14.9: fig14_09.py # Writing to shelve file.
import sys import shelve
# open shelve file try: outCredit = shelve.open( "credit.dat" ) except IOError: print >> sys.stderr, "File could not be opened" sys.exit( 1 )
print "Enter account number (1 to 100, 0 to end input)"
# get account information while 1:
# get account information accountNumber = int( raw_input( "\nEnter account number\n? " ) ) if 0 < accountNumber <= 100: print "Enter lastname, firstname, balance" currentData = raw_input( "? " )
outCredit[ str( accountNumber ) ] = currentData.split()
elif accountNumber == 0: break
outCredit.close() # close shelve file |
5.从shelve文件获取数据
|
# Fig. 14.9: fig14_09.py # Reading a shelve file.
import sys import shelve
# print formatted credit data def outputLine( account, aList ): print account.ljust( 10 ), print aList[ 0 ].ljust | | |