第一步:手動代碼堆積python
第二步:函數複用數據庫
第三步:數據持久化之數據保存app
第四步:數據持久化之數據讀取框架
第五步:數據持久化之數據刪除ide
第六步:數據持久化之數據更新函數
初稿—》數據持久化之保存數據—–》數據持久化之加載數據—–》數據持久化之刪除數據—-》數據持久化之檢索數據—》數據持久化之數據更新 測試
本演示不爲堆積代碼,僅爲梳理一個編碼的概念過程~但願對你有所幫助 ui
#!/usr/bin/env python #coding:utf8 #Author:zhuima #Email:993182876@qq.com #Date:2015-03-23 #Function:Create the address book step by step #Version:0.1 #Initialized variables msg = ''' 1. Add information 2. Display information 0. Exit ''' #txl content like this tex = [['name','gender','telphone'],['name','gender','telphone']] txl = [] #define Add #define display while True: print msg op = raw_input('Please Select >>> ') if op == '1': name = raw_input('Please Enter Your name >>> ') gender = raw_input('Please Enter Your gender >>> ') tel = raw_input('Please Enter Your Telphone Number >>> ') txl.append([name,gender,tel]) elif op == '2': for list in txl: for info in list: print info, print '' elif op == '0': break else: print '' print 'Unkonw Choose,Please Select again!' print ''
用到的python功能: this
while循環:實現循環輸入編碼
if判斷:判斷輸入內容並進行調用相關函數
break:跳槽循環
列表:在當前會話中保存數據
三引號的使用
測試結果:
Note:
print打印時,不換行使用’,’來實現,可是針對嵌套列表來講,極可能出現下面的狀況,這個時候在跳出當前循環print空白行便可。
如此咱們也能實現咱們想要的功能,可是總感受有點彆扭,要不要來點高大上的,來來來,叫來函數來一發~
代碼以下:
#!/usr/bin/env python #coding:utf8 #Author:zhuima #Email:993182876@qq.com #Date:2015-03-23 #Function:Create the address book step by step # #Initialized variables msg = ''' 1. Add information 2. Display information 0. Exit ''' #txl content like this tex = [['name','gender','telphone'],['name','gender','telphone']] txl = [] #define Add def Add(): name = raw_input('Please Enter Your name >>> ') gender = raw_input('Please Enter Your gender >>> ') tel = raw_input('Please Enter Your Telphone Number >>> ') txl.append([name,gender,tel]) #define display def Disp(): for list in txl: for info in list: print info, while True: print msg op = raw_input('Please Select >>> ') if op == '1': Add() elif op == '2': Disp() elif op == '0': break else: print '' print 'Unkonw Choose,Please Select again!' print ''
引入新功能:
python函數功能
測試結果:
測試結果其實和第一步是同樣的,沒有什麼區別~
這裏咱們用函數來實現了格式化代碼,複用等功能,可是我關閉了當前會話,全部的東西又都沒了,那這就是鬧玩唄,沒有實際意義啊~
因此這裏咱們要引入數據持久化的概念~(這裏僅討論文件保存形式的數據持久化,不涉及數據庫相關)
引入功能:
字符串和列表轉換,將列表轉成字符串,而後寫入文件
代碼以下:
#define save def Save(): temp = [] for info in txl: temp.append(','.join(info)) s = '\n'.join(temp) fp = file('txl.db','w') fp.write(s) fp.close() while True: print msg op = raw_input('Please Select >>> ') if op == '1': Add() Save() ....
測試效果:
引入功能:
字符串和列表轉換,將字符串傳換成列表,而後追加到列表中
代碼以下:
#define load def Load(): fp = file('txl.db','r') content = fp.read() temp = content.split('\n') for info in temp: txl.append(info.split(',')) Load() while True: ....
測試效果:
引入功能:
檢索帳號,找到帳號對應的子列表所在的索引,進行刪除操做
如何在嵌套列表中找到value對應的索引?
代碼片斷以下:
.... msg = ''' 1. Add information 2. Display information 3. Delete by name 0. Exit ''' #define del def Del(): name = raw_input('Please Enter which one name you want to delete >>> ') for sub in txl: if sub[0] == name: txl.remove(sub) break Load() while True: print msg op = raw_input('Please Select >>> ') if op == '1': Add() Save() elif op == '2': Disp() elif op == '3': Del() Save() ....
測試效果:
引入功能:
和刪除數據同理,檢索帳號,找到帳號對應的子列表所在的索引,進行相關value的更新操做
代碼片斷以下:
#Initialized variables msg = ''' 1. Add information 2. Display information 3. Delete by name 4. Update by name 0. Exit ''' info = ''' 001. name 002. gender 003. tel 004. all ''' #define change def Change(): xingming = raw_input('Please Enter which one name you want to change >>> ') print info op = raw_input('Please select which one you want to change: ') for sub in txl: if sub[0] == xingming: if op == '001': name = raw_input('Please Enter Your name: ') txl[txl.index(sub)][0] = name elif op == '002': gender= raw_input('Please Enter Your gender: ') txl[txl.index(sub)][1] = gender elif op == '003': tel = raw_input('Please Enter Your tel: ') txl[txl.index(sub)][2] = tel else: name = raw_input('Please Enter Your name: ') gender= raw_input('Please Enter Your gender: ') tel = raw_input('Please Enter Your tel: ') txl[txl.index(sub)] = [name,gender,tel] break Load() while True: print msg op = raw_input('Please Select >>> ') if op == '1': Add() Save() elif op == '2': Disp() elif op == '3': Del() Save() elif op == '4': Change() Save() ....
測試效果:
完成一個通信錄的增刪查改,侷限於列表,完善本身的組建代碼的一個思路,搭建起一套框架。
你要思考的問題:
初始化數據
如何增長數據
數據如何寫入到文件
如何從文件中讀取數據到列表
如何刪除數據並同步到文件
如何更新數據並同步到文件
完整代碼塊:
#!/usr/bin/env python #coding:utf8 #Author:zhuima #Email:993182876@qq.com #Date:2015-03-23 #Function:Create the address book step by step # #Initialized variables msg = ''' 1. Add information 2. Display information 3. Delete by name 4. Update by name 0. Exit ''' info = ''' 001. name 002. gender 003. tel 004. all ''' #txl content like this tex = [['name','gender','telphone'],['name','gender','telphone']] txl = [] #define Add def Add(): name = raw_input('Please Enter Your name >>> ') gender = raw_input('Please Enter Your gender >>> ') tel = raw_input('Please Enter Your Telphone Number >>> ') txl.append([name,gender,tel]) #define display def Disp(): for list in txl: for info in list: print info, print '' #define save def Save(): temp = [] for info in txl: temp.append(','.join(info)) s = '\n'.join(temp) fp = file('txl.db','w') fp.write(s+'\n') fp.close() #define load def Load(): import os if os.path.exists('txl.db'): fp = file('txl.db','r') content = fp.read() fp.close() temp = content.split('\n') for info in temp: txl.append(info.split(',')) else: fp = file('txl.db','w') fp.close() #define del def Del(): name = raw_input('Please Enter which one name you want to delete >>> ') for sub in txl: if sub[0] == name: txl.remove(sub) break #define change def Change(): xingming = raw_input('Please Enter which one name you want to change >>> ') print info op = raw_input('Please select which one you want to change: ') for sub in txl: if sub[0] == xingming: if op == '001': name = raw_input('Please Enter Your name: ') txl[txl.index(sub)][0] = name elif op == '002': gender= raw_input('Please Enter Your gender: ') txl[txl.index(sub)][1] = gender elif op == '003': tel = raw_input('Please Enter Your tel: ') txl[txl.index(sub)][2] = tel else: name = raw_input('Please Enter Your name: ') gender= raw_input('Please Enter Your gender: ') tel = raw_input('Please Enter Your tel: ') txl[txl.index(sub)] = [name,gender,tel] break Load() while True: print msg op = raw_input('Please Select >>> ') if op == '1': Add() Save() elif op == '2': Disp() elif op == '3': Del() Save() elif op == '4': Change() Save() elif op == '0': break else: print '' print 'Unkonw Choose,Please Select again!' print ''
做者:追馬
文章首發:http://blog.magedu.com/archives/1365?preview=true&preview_id=1365&preview_nonce=76a54cd598