引用知識python
list + dict用於臨時存儲用戶數據信息mysql
cPickle用於格式化文件存取sql
依舊使用file來進行文件的存儲數據庫
原始代碼 實現功能(可作模板)app
一、判斷輸入內容是否在給出的menu目錄內,在的話,返回對應結果,不在就報錯ide
二、調用os模塊的exit功能函數
三、字典配合循環加上函數實現switch的功能測試
#!/usr/bin/env python #coding:utf8 #Author:zhuima #Date:2015-03-22 #Version:0.1 #Function:display a list and add date # 導入模塊 import os def menu(): '''設置munu目錄,提供給用戶的操做接口 ''' print ''' 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program ''' op = raw_input('Please select one >>> ') return op def txl_exit(): ''' 退出程序 ''' os._exit(0) def txl_error(): ''' 當用戶輸出選項不在定義的選項內的時候,報錯''' print print 'Unkonw options,Please try again!' # 定義dict,配合函數實現switch功能 ops = { # '1':txl_add, # '2':txl_dis, # '3':txl_update, # '4':txl_del, # '5':txl_sort, '0':txl_exit, } def main(): '''主程序 ''' while True: op = menu() ops.get(op,txl_error)() if __name__ == '__main__': main()
思路ui
默認定義一個空list,而後嵌套dict來實現臨時存儲功能spa
一、添加的代碼
# 導入模塊 import os txl = [] .... def txl_add(): '''添加用戶''' name = raw_input('Please Enter Your Name >>> ') age = raw_input('Please Enter Your Age >>> ') gender = raw_input('Please Enter Your Gender >>> ') tel = raw_input('Please Enter Your Tel >>> ') txl.append({'name':name,'age':age,'gender':gender,'tel':tel}) def txl_disp(): '''顯示原始的txl列表 ''' print txl ..... ops = { '1':txl_add, '2':txl_disp, # '3':txl_update, # '4':txl_del, # '5':txl_sort, '0':txl_exit, }
二、測試結果
[root@mysql01 day0330]# python v2_1.py 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 2 [] 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 1 Please Enter Your Name >>> zhuima Please Enter Your Age >>> 28 Please Enter Your Gender >>> f Please Enter Your Tel >>> 10086 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 2 [{'gender': 'f', 'age': '28', 'tel': '10086', 'name': 'zhuima'}] 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 0
三、完整代碼塊
格式化輸出,使用字典
格式化輸出:print "%(name)s\t%(age)s" % dict
一、添加代碼片斷
def txl_disp(): '''顯示原始的txl列表 ''' print "name\tage\tgender\ttel" print "-----------------------" for x in txl: print "%(name)s\t%(age)s\t%(gender)s\t%(tel)s" % x
二、測試結果
[root@mysql01 day0330]# python v2_1.py 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 2 name age gender tel ---------------------------- 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 2 name age gender tel ---------------------------- 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 1 Please Enter Your Name >>> zhuima Please Enter Your Age >>> 28 Please Enter Your Gender >>> f Please Enter Your Tel >>> 10086 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 1 Please Enter Your Name >>> nick Please Enter Your Age >>> 25 Please Enter Your Gender >>> m Please Enter Your Tel >>> 10010 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 1 Please Enter Your Name >>> kale Please Enter Your Age >>> 33 Please Enter Your Gender >>> f Please Enter Your Tel >>> 10011 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 2 name age gender tel ---------------------------- zhuima 28 f 10086 nick 25 m 10010 kale 33 f 10011 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 0
三、完整代碼
思路:
引入cPickle概念,cPickle對文件進行讀取與存入的格式化操做,
cPickle的loads功能(從文件中讀取文件,保證原有格式)和dumps(把相關格式的對象文件存放到文件中)功能
一、添加代碼塊
# 導入模塊 import os import cPickle # 定義數據庫文件名 fname = 'contact.db' txl = [] ... def txl_add(): '''添加用戶''' .... #調用txl_save()模塊 txl_save() def txl_disp(): '''顯示原始的txl列表 作了二次調整,若是txl爲空,則打印木有文件存在,若是txl不爲空 則打印出數據信息''' if len(txl) > 0: print "name\tage\tgender\ttel" print '----------------------------' for x in txl: print "%(name)s\t%(age)s\t%(gender)s\t%(tel)s" % x else: print print ">>> This is a empty file,There is no infomation! >>>" def txl_save(): '''使用cPickle進行列表到字符串的轉換 而後寫入文件 ''' s = cPickle.dumps(txl) fp = file(fname,'w') fp.write(s) fp.close() def txl_load(): '''從文件讀取信息,而後使用cPickle進行字符串到列表的轉換''' if os.path.exists(fname): fp = file(fname) s = fp.read() fp.close() txl.extend(cPickle.loads(s))
二、寫入測試結果
[root@mysql01 day0330]# ls test.py v2_1.py v2.py [root@mysql01 day0330]# python v2_1.py 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 2 >>> This is a empty file,There is no infomation! >>> 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 1 Please Enter Your Name >>> zhuima Please Enter Your Age >>> 25 Please Enter Your Gender >>> f Please Enter Your Tel >>> 10086 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 2 name age gender tel ---------------------------- zhuima 25 f 10086 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 1 Please Enter Your Name >>> nick Please Enter Your Age >>> 22 Please Enter Your Gender >>> m Please Enter Your Tel >>> 10010 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 2 name age gender tel ---------------------------- zhuima 25 f 10086 nick 22 m 10010 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 0 [root@mysql01 day0330]# ls contact.db test.py v2_1.py v2.py
三、讀取測試結果
[root@mysql01 day0330]# python v2_1.py 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 2 name age gender tel ---------------------------- zhuima 25 f 10086 nick 22 m 10010 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 0 [root@mysql01 day0330]#
四、完整代碼
思路:
根據用戶名來進行數據的刪除
先來看一個演示
案例演示如何刪除嵌套列表字典
In [6]: s Out[6]: [{'age': 25, 'name': 'zhuima'}, {'age': 33, 'name': 'nick'}] #第一個思路就是要使用del來進行字典的刪除,可是針對嵌套不生效 In [7]: for x in s: if x['name'] == 'zhuima': del x ...: print s ...: [{'age': 25, 'name': 'zhuima'}, {'age': 33, 'name': 'nick'}] # 後來使用列表的remove來進行刪除,成功 In [10]: for x in s: if x['name'] == 'zhuima': s.remove(x) ....: In [11]: s Out[11]: [{'age': 33, 'name': 'nick'}]
一、代碼片斷
def txl_del(): '''根據用戶名進行刪除用戶相應的信息,並進行數據存儲,若是用戶不輸人該如何''' name = raw_input('Please Enter Your Want To Delete name >>> ') for line in txl: if line['name'] == name: txl.remove(line) break #最後記得調用存入的函數,要否則刪除僅針對當前會話,沒有寫入文件 txl_save()
二、測試效果
[root@mysql01 day0330]# python v2_1.py 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 2 name age gender tel ---------------------------- zhuima 25 f 10086 nick 22 m 10010 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 4 Please Enter Your Want To Delete name >>> nick 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 2 name age gender tel ---------------------------- zhuima 25 f 10086 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 0 [root@mysql01 day0330]# python v2_1.py 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 2 name age gender tel ---------------------------- zhuima 25 f 10086 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>>
三、完整代碼
思路:
根據用戶的選擇進行排序操做
引入功能,就是lambda txl.sort(key=lambda x : x[op])
一、代碼片斷
def txl_sort(): '''根據用戶的輸入對數據進行排序,用到了lambda函數,有bug的,應該提供一個默認值出來進行排序''' op = raw_input('Order By [name | age | gender | tel ] Display >>> ') txl.sort(key=lambda x : x[op]) txl_disp()
二、測試結果
[root@mysql01 day0330]# python v2_1.py 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 2 name age gender tel ---------------------------- zhuima 25 f 10086 nick 22 m 10011 kale 29 f 10093 tony 18 m 10010 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 5 Order By [name | age | gender | tel ] Display >>> name name age gender tel ---------------------------- kale 29 f 10093 nick 22 m 10011 tony 18 m 10010 zhuima 25 f 10086 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 5 Order By [name | age | gender | tel ] Display >>> age name age gender tel ---------------------------- tony 18 m 10010 nick 22 m 10011 zhuima 25 f 10086 kale 29 f 10093 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>>
三、完整代碼
思路:
方法一: 使用字典從新賦值便可實現更新(本案例演示採用的第一種方式)方法二: 根據索引找到對應的用戶所在的dict,而後對整個dict進行更新
根據索引找到對應的dict進行更新的案例演示
In [38]: s Out[38]: [{'age': 44, 'name': 'zhuima'}, {'age': 33, 'name': 'nick'}] In [39]: for x in s: ....: if x['name'] == 'zhuima': ....: s[s.index(x)] = {'name':'zhuima521','age':28} ....: In [40]: s Out[40]: [{'age': 28, 'name': 'zhuima521'}, {'age': 33, 'name': 'nick'}] In [41]:
一、代碼片斷
def txl_update(status=True): '''根據用戶名對該用戶的相關數據進行更新操做,用戶名不可變,若是選項不更新,則保留默認值,不然更新''' txl_disp() name = raw_input('Select One Update By Name >>> ') for line in txl: if line['name'] == name: status = False old_age = line['age'] old_gender = line['gender'] old_tel = line['tel'] age = raw_input('Please Enter Your Age for %s >>> ' % name) gender = raw_input('Please Enter Your Gender for %s >>> ' % name) tel = raw_input('Please Enter Your Tel for %s >>> ' % name) if len(age) == 0: line['age'] = old_age else: line['age'] = age if len(gender) == 0: line['gender'] = old_gender else: line['gender'] = gender if len(tel) == 0: line['tel'] = old_tel else: line['tel'] = tel break if status: print "Unkonw User,Try Again!" txl_save()
二、測試結果
[root@mysql01 day0330]# python v2_1.py 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 2 name age gender tel ---------------------------- fuck 30 m 90000 kale 33 f 10093 tony 18 m 10010 zhuima 25 f 10086 max 32 f 20000 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 3 name age gender tel ---------------------------- fuck 30 m 90000 kale 33 f 10093 tony 18 m 10010 zhuima 25 f 10086 max 32 f 20000 Select One Update By Name >>> kale Please Enter Your Age for kale >>> Please Enter Your Gender for kale >>> Please Enter Your Tel for kale >>> 99999 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 2 name age gender tel ---------------------------- fuck 30 m 90000 kale 33 f 99999 tony 18 m 10010 zhuima 25 f 10086 max 32 f 20000 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>> 3 name age gender tel ---------------------------- fuck 30 m 90000 kale 33 f 99999 tony 18 m 10010 zhuima 25 f 10086 max 32 f 20000 Select One Update By Name >>> min Unkonw User,Try Again! 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program Please select one >>>
三、完整代碼
至此,咱們使用dict完成了數據庫的增、刪、改、查、排序等操做
完整代碼
#!/usr/bin/env python #coding:utf8 #Author:zhuima #Date:2015-03-22 #Version:0.1 #Function:display a list and add date # 導入模塊 import os import cPickle fname = 'contact.db' txl = [] def menu(): '''設置munu目錄,提供給用戶的操做接口 ''' print ''' 1.add user info 2.disp all user info 3.update user info by username 4:del user by username 5:sort user info by 0.exit program ''' op = raw_input('Please select one >>> ') return op def txl_add(): '''添加用戶''' name = raw_input('Please Enter Your Name >>> ') age = raw_input('Please Enter Your Age >>> ') gender = raw_input('Please Enter Your Gender >>> ') tel = raw_input('Please Enter Your Tel >>> ') txl.append({'name':name,'age':age,'gender':gender,'tel':tel}) txl_save() def txl_disp(): '''顯示原始的txl列表 ''' if len(txl) > 0: print "name\tage\tgender\ttel" print '----------------------------' for x in txl: print "%(name)s\t%(age)s\t%(gender)s\t%(tel)s" % x else: print print ">>> This is a empty file,There is no infomation! >>>" def txl_save(): '''對數據進行寫操做,寫以前進行格式轉換''' s = cPickle.dumps(txl) fp = file(fname,'w') fp.write(s) fp.close() def txl_load(): '''對文件進行讀取,若是文件存在的狀況下 ''' if os.path.exists(fname): fp = file(fname) s = fp.read() fp.close() txl.extend(cPickle.loads(s)) def txl_update(status=True): '''根據用戶名對該用戶的相關數據進行更新操做,用戶名不可變,若是選項不更新,則保留默認值,不然更新''' txl_disp() name = raw_input('Select One Update By Name >>> ') for line in txl: if line['name'] == name: status = False old_age = line['age'] old_gender = line['gender'] old_tel = line['tel'] age = raw_input('Please Enter Your Age for %s >>> ' % name) gender = raw_input('Please Enter Your Gender for %s >>> ' % name) tel = raw_input('Please Enter Your Tel for %s >>> ' % name) if len(age) == 0: line['age'] = old_age else: line['age'] = age if len(gender) == 0: line['gender'] = old_gender else: line['gender'] = gender if len(tel) == 0: line['tel'] = old_tel else: line['tel'] = tel break if status: print "Unkonw User,Try Again!" txl_save() def txl_del(): '''根據用戶名進行刪除用戶相應的信息,並進行數據存儲''' name = raw_input('Please Enter Your Want To Delete name >>> ') for line in txl: if line['name'] == name: txl.remove(line) break txl_save() def txl_sort(): '''根據用戶的輸入對數據進行排序,用到了lambda函數 ''' op = raw_input('Order By [name | age | gender | tel ] Display >>> ') txl.sort(key=lambda x : x[op]) txl_disp() def txl_exit(): ''' 退出程序 ''' os._exit(0) def txl_error(): ''' 當用戶輸出選項不在定義的選項內的時候,報錯''' print print 'Unkonw options,Please try again!' # 定義dict,配合函數實現switch功能 ops = { '1':txl_add, '2':txl_disp, '3':txl_update, '4':txl_del, '5':txl_sort, '0':txl_exit, } txl_load() def main(): '''主程序 ''' while True: op = menu() ops.get(op,txl_error)() if __name__ == '__main__': main()