手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Python简单查询用户

[日期:2014-02-13] 来源:Linux社区  作者:Linux [字体:大 中 小]

Python简单查询用户

一,第一种:
采用文件中contact.txt中第一行读出后,将其分割成列表,然后对其判断,但是不支持部门查询。
#!/usr/bin/env python
while True:
        name=raw_input('please input name:')
        if name == 'small':
                while True:
                        password=raw_input('please input password:')
                        if password == '123':
                                print 'welcome login  system'
                                while True:
                                        input=raw_input('please input employee name:')
                                        f=open('contact.txt','r')
                                        match=0
                                        for i in f.readlines():
                                                if input == i.split(" ")[1]:
                                                        print i
                                                        match=1
                                        if match==0:
                                                        print "user not found,please input"
                                        f.close()
                                #break
                        else:
                                print 'password is wrong,try again'
                break

第二种:将文件中每行读出后成字符串的形式,因此支持各个字段查询。
#!/usr/bin/python
while True:
        input = raw_input("please input your username:")
        if input == 'small':
                password = raw_input("please input your pass:")
                p = '123'
                while password != p:
                        password = raw_input("wrong passwd,input again:")
                else:
                        print "welcome login to linux!"
                while True:
                        match_yes = 0
                        input = raw_input("\033[32mPlease input name whom you want to search:\33[0m")
                        contact_file = file('contact_list.txt','r')
                        while True:
                                line = contact_file.readline()
                                if len(line) == 0:break
                                if input != "" and input in line:
                                        print "match item: \33{36,1m%s\033[0m" % line
                                        match_yes = 1
                        if match_yes == 0 :print "no match item found"

第三种:用字典的方式来实现
#!/usr/bin/env python
f=file('contact.txt')
personal={}
for line in f.readlines():
        name= line.split()[0]
        personal[name]=line
while True:
f=file('contact.txt')
personal={}
for line in f.readlines():
        name= line.split()[0]
        personal[name]=line
while True:
        input=raw_input('please input username:').strip()
        if len(input) == 0:continue
        if input in personal:
        #if personal.has_key(input):
                #print 'username information:  %s' %personal.get(input)
                print 'username information:  %s' %personal[input]
        else:
                print '%s not found,please input' %input

Python 的详细介绍:请点这里
Python 的下载地址:请点这里

推荐阅读:

《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码] http://www.linuxidc.com/Linux/2013-11/92693.htm

Python脚本获取Linux系统信息 http://www.linuxidc.com/Linux/2013-08/88531.htm

linux
相关资讯       Python查询 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款