python简单查询用户

简介:

一,第一种:

采用文件中contact.txt中第一行读出后,将其分割成列表,然后对其判断,但是不支持部门查询。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/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  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

第二种:将文件中每行读出后成字符串的形式,因此支持各个字段查询。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/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"

第三种:用字典的方式来实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/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










本文转自 deng304749970 51CTO博客,原文链接:http://blog.51cto.com/damondeng/1354239,如需转载请自行联系原作者
目录
相关文章
|
1月前
|
数据库 Python
【YashanDB知识库】python驱动查询gbk字符集崖山数据库CLOB字段,数据被驱动截断
【YashanDB知识库】python驱动查询gbk字符集崖山数据库CLOB字段,数据被驱动截断
|
4月前
|
存储 小程序 Python
农历节日倒计时:基于Python的公历与农历日期转换及节日查询小程序
### 农历节日倒计时:基于Python的公历与农历日期转换及节日查询小程序 该程序通过`lunardate`库实现公历与农历的日期转换,支持闰月和跨年处理,用户输入农历节日名称后,可准确计算距离该节日还有多少天。功能包括农历节日查询、倒计时计算等。欢迎使用! (239字符)
308 86
|
6月前
|
数据采集 人工智能 自然语言处理
AI Agent 金融助理0-1 Tutorial 利用Python实时查询股票API的FinanceAgent框架构建股票(美股/A股/港股) AI Finance Agent
金融领域Finance AI Agents方面的工作,发现很多行业需求和用户输入的 query都是和查询股价/行情/指数/财报汇总/金融理财建议相关。如果需要准确的 金融实时数据就不能只依赖LLM 来生成了。常规的方案包括 RAG (包括调用API )再把对应数据和prompt 一起拼接送给大模型来做文本生成。稳定的一些商业机构的金融数据API基本都是收费的,如果是以科研和demo性质有一些开放爬虫API可以使用。这里主要介绍一下 FinanceAgent,github地址 https://github.com/AI-Hub-Admin/FinanceAgent
|
6月前
|
iOS开发 MacOS Python
Python编程小案例—利用flask查询本机IP归属并输出网页图片
Python编程小案例—利用flask查询本机IP归属并输出网页图片
72 1
|
5月前
|
测试技术 API 数据安全/隐私保护
Python连接到Jira实例、登录、查询、修改和创建bug
通过使用Python和Jira的REST API,可以方便地连接到Jira实例并进行各种操作,包括查询、修改和创建Bug。`jira`库提供了简洁的接口,使得这些操作变得简单易行。无论是自动化测试还是开发工作流的集成,这些方法都可以极大地提高效率和准确性。希望通过本文的介绍,您能够更好地理解和应用这些技术。
513 0
|
7月前
|
关系型数据库 MySQL 数据库
Python MySQL查询返回字典类型数据的方法
通过使用 `mysql-connector-python`库并选择 `MySQLCursorDict`作为游标类型,您可以轻松地将MySQL查询结果以字典类型返回。这种方式提高了代码的可读性,使得数据操作更加直观和方便。上述步骤和示例代码展示了如何实现这一功能,希望对您的项目开发有所帮助。
241 4
|
7月前
|
存储 Python
深度剖析:Python里字典树Trie的构建与查询,让你的代码更优雅!
在编程的世界里,数据结构的选择往往直接决定了程序的效率和可读性。今天,我们将深入探索一种高效处理字符串搜索与匹配的数据结构——字典树(Trie),也称作前缀树或单词查找树。通过Python实现Trie树,我们将看到它如何优雅地解决一系列字符串相关的问题,并提升代码的整体质量。
96 2
|
6月前
|
SQL 前端开发 Python
基于python-django的neo4j人民的名义关系图谱查询系统
基于python-django的neo4j人民的名义关系图谱查询系统
103 0
|
6月前
|
IDE 搜索推荐 网络安全
Python编程:编写被动信息搜集之网址的IP及Whois查询
Python编程:编写被动信息搜集之网址的IP及Whois查询
66 0
|
8月前
|
测试技术 API 开发者
Python 魔法:打造你的第一个天气查询小工具自动化测试框架的构建与实践
【8月更文挑战第31天】在这篇文章中,我们将一起踏上编程的奇妙旅程。想象一下,只需几行代码,就能让计算机告诉你明天是否要带伞。是的,你没有听错,我们将用Python这把钥匙,解锁天气预报的秘密。不论你是编程新手还是想拓展技能的老手,这篇文章都会为你带来新的视角和灵感。所以,拿起你的键盘,让我们一起创造属于自己的天气小工具吧!