编写过程:
第一步:手动代码堆积
第二步:函数复用
第三步:数据持久化之数据保存
第四步:数据持久化之数据读取
第五步:数据持久化之数据删除
第六步:数据持久化之数据更新
初稿—》数据持久化之保存数据—–》数据持久化之加载数据—–》数据持久化之删除数据—-》数据持久化之检索数据—》数据持久化之数据更新
本演示不为堆积代码,仅为梳理一个编码的概念过程~希望对你有所帮助
第一步:手工代码堆积
#!/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功能:
while循环:实现循环输入
if判断:判断输入内容并进行调用相关函数
break:跳槽循环
列表:在当前会话中保存数据
三引号的使用
测试结果:
wKioL1UP3yvgvP4xAAHMTL3rC7o475.jpg
Note:
print打印时,不换行使用’,’来实现,但是针对嵌套列表来说,很可能出现下面的情况,这个时候在跳出当前循环print空白行即可。
第二步:函数复用
wKiom1UP3grAG-BSAAGYqMPL5mQ502.jpg
如此我们也能实现我们想要的功能,但是总感觉有点别扭,要不要来点高大上的,来来来,叫来函数来一发~
代码如下:
#!/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函数功能
测试结果:
测试结果其实和第一步是一样的,没有什么区别~
wKioL1UP30mjOFAOAAHMTL3rC7o117.jpg
第三步:数据持久化之保存数据
wKioL1UP31uRnQEIAAHhTfyIx70812.jpg
这里我们用函数来实现了格式化代码,复用等功能,但是我关闭了当前会话,所有的东西又都没了,那这就是闹玩呗,没有实际意义啊~
所以这里我们要引入数据持久化的概念~(这里仅讨论文件保存形式的数据持久化,不涉及数据库相关)
引入功能:
字符串和列表转换,将列表转成字符串,然后写入文件
代码如下:
#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()
....
测试效果:
wKioL1UP33vAGKahAAKCc4J9KKA866.jpg
第四步:数据持久化之数据读取
wKiom1UP3lriLU4iAAHIMXTCu4k851.jpg
引入功能:
字符串和列表转换,将字符串传换成列表,然后追加到列表中
代码如下:
#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:
....
测试效果:
wKiom1UP3m-A3UfIAAFN8KySigs188.jpg
第五步:数据持久化之删除数据
wKioL1UP362h8Bi6AAH0tzU5RAU800.jpg
引入功能:
检索账号,找到账号对应的子列表所在的索引,进行删除操作
如何在嵌套列表中找到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()
....
测试效果:
wKioL1UP39GQIaezAAFN8KySigs278.jpg
第六步:数据持久化之更新数据
wKiom1UP3rTiRBMrAAIrbJ1AqG0484.jpg
引入功能:
和删除数据同理,检索账号,找到账号对应的子列表所在的索引,进行相关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()
....
测试效果:
wKiom1UP3srSF5e-AAHTb2b5_q4273.jpg
wKioL1UP3_qxuHpuAAIICsxSNz0812.jpg
整体思路:
完成一个通讯录的增删查改,局限于列表,完善自己的组建代码的一个思路,搭建起一套框架。
你要思考的问题:
初始化数据
如何增加数据
数据如何写入到文件
如何从文件中读取数据到列表
如何删除数据并同步到文件
如何更新数据并同步到文件
完整代码块:
#!/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
©著作权归作者所有:来自51CTO博客作者lovelace521的原创作品,如需转载,请注明出处,否则将追究法律责任
订 阅
微服务技术架构和大数据治理实战
大数据时代的微服务之路
共18章 | 纯洁微笑
¥51.00 627人订阅
订 阅
猜你喜欢
我的友情链接python写一个通讯录step by step V3.0终于搞定了回家车票ZabbixWeb监控变频器如何通过 Scrapyd + ScrapydWeb 简单高效地部署和监控分布式爬虫项目Python 单元测试(unittest)编程学习第一步,让你20天搞定Python编程微信运维交互机器人
10313分享 关注 lovelace521
共同学习,写下你的评论
评论加载中...
作者其他优质文章