Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

用 Python 打造簡單的使用者查詢程式(三種實作)

用 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:")
                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"

第三種:改用字典(dict)的方式來實作,查詢效率與可讀性都更好。

#!/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 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved