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
Python实现简易天气查询系统
Python实现简易天气查询系统
33 4
|
1天前
|
缓存 API 定位技术
使用Python调用百度地图API实现地址查询
使用Python调用百度地图API实现地址查询
129 0
|
1天前
|
关系型数据库 MySQL 数据库连接
python查询数据库的某个表,将结果导出Excel
python查询数据库的某个表,将结果导出Excel
58 0
|
1天前
|
NoSQL 关系型数据库 MySQL
深入了解 Python MongoDB 查询:find 和 find_one 方法完全解析
在 MongoDB 中,我们使用 find() 和 find_one() 方法来在集合中查找数据,就像在MySQL数据库中使用 SELECT 语句来在表中查找数据一样
67 1
|
1天前
|
SQL 关系型数据库 MySQL
用 Python 连接数据库并进行查询。
【2月更文挑战第12天】【2月更文挑战第32篇】用 Python 连接数据库并进行查询。
|
1天前
|
JSON 数据格式 索引
python之JMESPath:JSON 查询语法库示例详解
python之JMESPath:JSON 查询语法库示例详解
15 0
|
1天前
|
开发者 Python
在Python中查询进程信息的实用指南
在Python中查询进程信息的实用指南
10 2
|
1天前
|
SQL 缓存 数据库
在Python Web开发过程中:数据库与缓存,如何使用ORM(例如Django ORM)执行查询并优化查询性能?
在Python Web开发中,使用ORM如Django ORM能简化数据库操作。为了优化查询性能,可以:选择合适索引,避免N+1查询(利用`select_related`和`prefetch_related`),批量读取数据(`iterator()`),使用缓存,分页查询,适时使用原生SQL,优化数据库配置,定期优化数据库并监控性能。这些策略能提升响应速度和用户体验。
18 0
|
1天前
|
数据采集 搜索推荐 数据挖掘
使用Python制作一个批量查询搜索排名的SEO免费工具
最近工作中需要用上 Google SEO(搜索引擎优化),有了解过的朋友们应该都知道SEO必不可少的工作之一就是查询关键词的搜索排名。关键词少的时候可以一个一个去查没什么问题,但是到了后期,一个网站都有几百上千的关键词,你再去一个一个查,至少要花费数小时的时间。 虽然市面上有很多SEO免费或者收费工具,但免费的基本都不能批量查,网上免费的最多也就只能10个10个查询,而且查询速度很慢。收费的工具如Ahrefs、SEMrush等以月为单位收费最低也都要上百美刀/月,当然如果觉得价格合适也可以进行购买,毕竟这些工具的很多功能都很实用。今天我给大家分享的这个排名搜索工具基于python实现,当然肯定
49 0
|
1天前
|
SQL NoSQL MongoDB
python MongoClient 创建数据库,并且创建表插入一行数据并查询
要在MongoDB中创建一个数据库、一个集合(在MongoDB中,集合类似于SQL中的表)并插入一行数据,你可以使用MongoDB的官方驱动程序。以下是一个使用Python的pymongo库来执行这些操作的示例: 首先,确保你已经安装了pymongo库。你可以使用pip来安装: bash pip install pymongo 接下来,使用以下Python代码来创建数据库、集合、插入数据并查询: python from pymongo import MongoClient # 连接到MongoDB服务器 client = MongoClient('mongodb://loc