评论

收藏

[JavaScript] python写一个通讯录之step by step

开发技术 开发技术 发布于:2021-06-23 22:27 | 阅读数:529 | 评论:0

编写过程:


第一步:手动代码堆积第二步:函数复用第三步:数据持久化之数据保存第四步:数据持久化之数据读取第五步:数据持久化之数据删除第六步:数据持久化之数据更新

初稿—》数据持久化之保存数据—–》数据持久化之加载数据—–》数据持久化之删除数据—-》数据持久化之检索数据—》数据持久化之数据更新    本演示不为堆积代码,仅为梳理一个编码的概念过程~希望对你有所帮助
  第一步:手工代码堆积

#!/usr/bin/env pythoncoding:utf8Author:zhuimaEmail:993182876@qq.comDate:2015-03-23Function:Create the address book step by stepVersion:0.1
 Initialized variables
msg = '''
Add information
Display information
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 ''</pre><h2>   <br /></h2><br><br /><span style="font-size:16px;"><strong>用到的python功能:</strong></span>
    <ol class="list-paddingleft-2"><li><span style="font-size:16px;line-height:1.5;">while循环:实现循环输入</span></li><li><span style="font-size:16px;line-height:1.5;">if判断:判断输入内容并进行调用相关函数</span></li><li><span style="font-size:16px;line-height:1.5;">break:跳槽循环</span></li><li><span style="font-size:16px;line-height:1.5;">列表:在当前会话中保存数据</span></li><li><span style="font-size:16px;">三引号的使用</span></li></ol><strong><span style="font-size:16px;">测试结果:</span></strong><img src="https://s3.51cto.com/wyfs02/M02/5B/A7/wKioL1UP3yvgvP4xAAHMTL3rC7o475.jpg" border="0"><strong><span style="font-size:16px;">Note:</span></strong><span style="font-size:16px;">  print打印时,不换行使用’,’来实现,但是针对嵌套列表来说,很可能出现下面的情况,这个时候在跳出当前循环print空白行即可。</span><br /><h2>第二步:函数复用<br /><img src="https://s3.51cto.com/wyfs02/M02/5B/AC/wKiom1UP3grAG-BSAAGYqMPL5mQ502.jpg" border="0">   </h2><br>   <span style="font-size:16px;">如此我们也能实现我们想要的功能,但是总感觉有点别扭,要不要来点高大上的,来来来,叫来函数来一发~</span>
  <br /><span style="font-size:16px;">代码如下:</span>
  <pre class="brush:xml;toolbar:false">#!/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 = '''

  • Add information
  • Display information
  • 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 ''</pre><br /><strong><span style="font-size:16px;">引入新功能:</span></strong>
  <span style="font-size:16px;">  python函数功能</span><br /><span style="font-size:16px;">测试结果:</span>
  <span style="font-size:16px;">测试结果其实和第一步是一样的,没有什么区别~</span><img src="https://s3.51cto.com/wyfs02/M00/5B/A7/wKioL1UP30mjOFAOAAHMTL3rC7o117.jpg" border="0"><h2>第三步:数据持久化之保存数据<br /><img src="https://s3.51cto.com/wyfs02/M01/5B/A7/wKioL1UP31uRnQEIAAHhTfyIx70812.jpg" border="0">   </h2><br>   <span style="font-size:16px;">这里我们用函数来实现了格式化代码,复用等功能,但是我关闭了当前会话,所有的东西又都没了,那这就是闹玩呗,没有实际意义啊~</span>
  <span style="font-size:16px;">所以这里我们要引入数据持久化的概念~(这里仅讨论文件保存形式的数据持久化,不涉及数据库相关)</span><br /><strong><span style="font-size:16px;">引入功能:</span></strong><span style="font-size:16px;">  字符串和列表转换,将列表转成字符串,然后写入文件</span><br /><span style="font-size:16px;">代码如下:</span>
  <span style="font-size:16px;"> </span>
  <pre class="brush:xml;toolbar:false">#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()
  ....</pre><span style="font-size:16px;"><strong>测试效果:</strong></span><img src="https://s3.51cto.com/wyfs02/M02/5B/A7/wKioL1UP33vAGKahAAKCc4J9KKA866.jpg" border="0"><h2>第四步:数据持久化之数据读取<br /><img src="https://s3.51cto.com/wyfs02/M02/5B/AC/wKiom1UP3lriLU4iAAHIMXTCu4k851.jpg" border="0">   </h2><br><br /><strong><span style="font-size:16px;">引入功能:</span></strong>
    <span style="font-size:16px;">  字符串和列表转换,将字符串传换成列表,然后追加到列表中</span>
    <br />
    <span style="font-size:16px;">代码如下:</span>
      <pre class="brush:xml;toolbar:false">#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:
....
测试效果:    DSC0000.jpg 第五步:数据持久化之删除数据
DSC0001.jpg    


                                                                           引入功能:        检索账号,找到账号对应的子列表所在的索引,进行删除操作        如何在嵌套列表中找到value对应的索引?
   
    代码片段如下:
<pre class="brush:xml;toolbar:false">....
  msg = '''

  • Add information
  • Display information
  • Delete by name
  • 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()
  ....</pre><br />测试效果:<img src="https://s3.51cto.com/wyfs02/M00/5B/A7/wKioL1UP39GQIaezAAFN8KySigs278.jpg" border="0"><br /><h2>第六步:数据持久化之更新数据<br /><img src="https://s3.51cto.com/wyfs02/M01/5B/AD/wKiom1UP3rTiRBMrAAIrbJ1AqG0484.jpg" border="0">   </h2><br><br /><strong><span style="font-size:16px;">引入功能:</span></strong>
  <span style="font-size:16px;">  和删除数据同理,</span><span style="font-size:16px;line-height:1.5;">检索账号,找到账号对应的子列表所在的索引,进行相关value的更新操作</span>
  <br />
  <span style="font-size:16px;">代码片段如下:</span>
    <span style="font-size:16px;"> </span>
#Initialized variables
msg = '''
Add information
Display information
Delete by name
Update by name
Exit 
'''
info = '''
name
gender
tel
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()
  ....</pre>   <br /><br /><br />测试效果:<img src="https://s3.51cto.com/wyfs02/M02/5B/AD/wKiom1UP3srSF5e-AAHTb2b5_q4273.jpg" border="0"><img src="https://s3.51cto.com/wyfs02/M00/5B/A7/wKioL1UP3_qxuHpuAAIICsxSNz0812.jpg" border="0">   <br />   <br /><h2><span style="font-size:16px;">整体思路:</span><br />
  </h2><br><span style="font-size:16px;">完成一个通讯录的增删查改,局限于列表,完善自己的组建代码的一个思路,搭建起一套框架。</span><span style="font-size:16px;">你要思考的问题:</span><ol class="list-paddingleft-2"><li><span style="font-size:16px;line-height:1.5;">初始化数据</span></li><li><span style="font-size:16px;line-height:1.5;">如何增加数据</span></li><li><span style="font-size:16px;line-height:1.5;">数据如何写入到文件</span></li><li><span style="font-size:16px;line-height:1.5;">如何从文件中读取数据到列表</span></li><li><span style="font-size:16px;line-height:1.5;">如何删除数据并同步到文件</span></li><li><span style="font-size:16px;line-height:1.5;">如何更新数据并同步到文件</span></li></ol><span style="font-size:16px;">完整代码块:</span>
    <pre class="brush:xml;toolbar:false">#!/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 = '''

  • Add information
  • Display information
  • Delete by name
  • Update by name
  • Exit
  '''
  info = '''

  • name
  • gender
  • 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


关注下面的标签,发现更多相似文章