python學習筆記四(數據持久儲存)

第四章剛開始,提出要把sktch.txt 中Man和Othe Man的臺詞分別儲存在兩個不同的list裏。

剛開始的代碼爲:

man = []
other = []
try :
    data = open('sketch.txt')
    for each_line in data:
        try:
            (role,line_spoken) = each_line.split(':',1)
            line_spoken = line_spoken.strip()
            if role == 'Man':
                man.append(line_spoken)
            elif role =='Other Man':
                other.append(line_spoken)
        except ValueError:
            pass
except IOError:
    print 'the txt is missing'
print man
print other

其中值得一提的是兩個異常的處理。外層的異常是指如果sketch文件不存在時,拋出來的異常爲‘the txt is missing’.內層的異常是針對split方法調用時如果沒有value值,時執行pass,即忽略不計。

而後,需要把man 和other的分別存儲在不同的txt裏。這裏的代碼如下:

man = []
other = []
try :
    data = open('sketch.txt')
    for each_line in data:
        try:
            (role,line_spoken) = each_line.split(':',1)
            line_spoken = line_spoken.strip()
            if role == 'Man':
                man.append(line_spoken)
            elif role =='Other Man':
                other.append(line_spoken)
        except ValueError:
            pass
except IOError:
    print('the txt is missing')
                                                                                                                                                                                                                                                                                                                                        
try:
        man_part = open('man_part.txt','w')
        other_part =open('other_part.txt','w')
        print(man,file = man_part)
        print(other,file = other_part)
        man_part.close()
        other_part.close()
except IOError:
        print('file error')

說明:

        自此由於書上是按照 Python3 介紹的,寫入文件的方法和Python2不兼容,所以爲了方便,我Python的版本也自動改爲了3.3.

書上緊接着介紹了finally 格式。

finally是配合try/except使用的,即是無論執行其中哪一種代碼,則finally都會執行。

 結合此例,則是關閉文件適合執行finally.如下:

finally:
        man_part.close()
        other.part.close()

之後又提到了如何顯示錯誤信息。用的標識符是err.

試驗一下:

我寫了一個Missingtxt.py

try:
        man_part = open('man_part.txt')
                                                                                                                                                                                 
        man_part.close()
except IOError as err:
        print('file error:' +str(err))

with的使用

with的使用可以取代finally。

例如上述的例子可以改寫爲:

try:
       with open('man_part.txt','w') as man_part:
            print(man,file = man_part)
       with open('other_part.txt','w') as other_part:     
            print(other,file = other_part)
except IOError:
        print('file error')

Pickle:

   pickle是持久儲存的作用,是python專有的格式。斷電後仍不消失。

import pickle
with open('mydata.pickle','wb') as mysavedata:
    pickle.dump([1,2,'data'], mysavedata)
with open('mydata.pickle','rb') as mystoredata:
    alist = pickle.load(mystoredata)
    print(alist)

由此改寫上述的程序:

import pickle
man = []
other = []
try :
    data = open('sketch.txt')
    for each_line in data:
        try:
            (role,line_spoken) = each_line.split(':',1)
            line_spoken = line_spoken.strip()
            if role == 'Man':
                man.append(line_spoken)
            elif role =='Other Man':
                other.append(line_spoken)
        except ValueError:
            pass
except IOError:
    print('the txt is missing')
        
try:
    try:
        with open('man_part.txt','wb') as man_part:
            pickle.dump(man,man_part)
        with open('other_part.txt','wb') as other_part:     
            pickle.dump(other,other_part)
    except PickleError as perr:
        print('pickling error'+ str(perr))
except IOError as err:
    print('file error'+ str(err))


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章