鱼C论坛

 找回密码
 立即注册
查看: 2584|回复: 2

[技术交流] 利用列表对文件进行增删改查

[复制链接]
发表于 2014-12-19 16:54:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 wei_Y 于 2014-12-19 18:08 编辑

  1. import os
  2. import os.path

  3. while True:
  4.     #进入之前要验证用户名与密码
  5.     user = input('Please input username:').strip()
  6.     while user == '' or user != 'roy':
  7.         user = input('Please input username:').strip()
  8.         continue
  9.     else:
  10.         password = input('Please input password:').strip()
  11.         while password == '' or password != 'abc':
  12.             password = input('Please input password:').strip()
  13.             continue
  14.     print('Welcome enter system!!!')
  15.    
  16.     #不断循环要操作的项目,输入q或quit就退出
  17.     while True:
  18.         print('1. Add User\n2. Modify User\n3. Del User\n4. Find User\n')
  19.       
  20.         #把文件读出来放到列表
  21.         f = open('content.txt','r')
  22.         li = f.readlines()
  23.         f.close()
  24.         num = 0

  25.         choose = input('Please choose:').strip().lower()

  26.         if choose == 'q' or choose == 'quit':
  27.             print('ByeBye!!')
  28.             break
  29.         #增加用户
  30.         elif choose == '1':
  31.             fadd = open('content.txt','w')
  32.             u = input('Please input id:')
  33.             p = input('Please input name:')
  34.             s = input('Please input sex:')
  35.             d = input('Please input Department:')
  36.             li.append('\n'+u+'\t'+p+'\t'+s+'\t'+d+'\t')
  37.             fadd.writelines(li)
  38.             fadd.flush()
  39.             continue


  40.         #修改文件内容
  41.         elif choose == '2':
  42.             co = 0

  43.             ok = input('Please input old key:')
  44.             nk = input('Please input new key:')
  45.             
  46.             frm = open('content.txt','w')

  47.             for lines in li:
  48.                 if ok in li[co]:
  49.                     li[co] = li[co].replace(ok,nk)
  50.                 co += 1
  51.             frm.writelines(li)
  52.             frm.flush()
  53.             continue

  54.         #删除用户
  55.         elif choose == '3':
  56.             co = 0
  57.             rm = input('Please input remove key index:')
  58.             frm = open('content.txt','w')

  59.             for lines in li:
  60.                 if rm in li[co]:
  61.                     del li[co]
  62.                 co += 1
  63.             frm.writelines(li)
  64.             frm.flush()
  65.             continue

  66.         #查找内容
  67.         elif choose == '4':
  68.             find_name = input('Please input find key words:').strip()
  69.             for line in li:
  70.                 if find_name in line:
  71.                     print (line)
  72.                     num += 1
  73.             else:
  74.                 if num == 0:
  75.                     print('Not a valid key words!')
  76.                     continue
  77.                 else:
  78.                     print('Find %d data' % num)
  79.                     num = 0
  80.             continue
  81.         else:
  82.             print('Input error!')
  83.             continue

  84.     f.close()
  85.     break
复制代码




评分

参与人数 1荣誉 +2 鱼币 +2 贡献 +1 收起 理由
wei_Y + 2 + 2 + 1 下次记得代码格式~。

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-12-21 17:53:15 | 显示全部楼层
:titter:
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-12-21 20:53:56 | 显示全部楼层
  有点看不懂。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-28 21:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表