python查找输出文字_Python基础练习,查询文本内容并输出;
1、編寫可供用戶查詢的員工信息表:
1).用戶認證(輸入用戶名,密碼)
2).查詢關鍵字:姓名
3).顯示ID,name,section,phone
4).員工信息表內容如下:
cat?search_name.txt
001wenlong IT12345678
002xiaojun IT12345678
003ankang IT12345678
004xiang IT12345678
腳本如下:#!/usr/bin/python
#-*-?coding:utf-8?-*-????????#輸入這一條就可以在Python腳本里面使用漢語注釋!此腳本可以直接復制使用;
while?True:????????????#進入死循環
input?=?raw_input('Please?input?your?username:')????#交互式輸入用戶信息,輸入input信息;
if?input?==?"wenlong":????????#如果input等于wenlong則進入此循環(如果用戶輸入wenlong)
password?=?raw_input('Please?input?your?pass:')????#交互式信息輸入,輸入password信息;
p?=?'123'??????????????????#設置變量P賦值為123
while?password?!=?p:?????????#如果輸入的password?不等于p(123),?則進此入循環
password?=?raw_input('Please?input?your?pass?again:')????#交互式信息輸入,輸入password信息;
if?password?==?p:????????#如果password等于p(123),則進入此循環
print?'welcome?to?select?system!'??????????????#輸出提示信息;
while?True:???????????#進入循環;
match?=?0?????#設置變量match等于0;
input?=?raw_input("Please?input?the?name?whom?you?want?to?search?:")???#交互式信息輸入,輸入input信息;
while?not?input.strip():???#判斷input值是否為空,如果input輸出為空,則進入循環;
input?=?raw_input("Please?input?the?name?whom?you?want?to?search?:")????????#交互式信息輸入,輸入input信息;
name_file?=?file('search_name.txt')?????#設置變量name_file,file('search_name.txt')是調用名為search_name.txt的文檔
while?True:???????????????#進入循環;
line?=?name_file.readline()???????????#以行的形式,讀取search_name.txt文檔信息;
if?len(line)?==?0:??????#當len(name_file.readline()?)為0時,表示讀完了文件,len(name_file.readline()?)為每一行的字符長度,空行的內容為\n也是有兩個字符。len為0時進入循環;
break???????#執行到這里跳出循環;
if?input?in?line:????#如果輸入的input信息可以匹配到文件的某一行,進入循環;
print?'Match?item:?%s'??%line?????#輸出匹配到的行信息;
match?=?1????#給變量match賦值為1
if?match?==?0?:??????????????#如果match等于0,則進入???;
print?'No?match?item?found!'?????????#輸出提示信息;
else:?print?"Sorry?,user??%s?not?found?"?%input??????#如果輸入的用戶不是wenlong,則輸出信息沒有這個用戶;
上面加了備注的腳本也可以直接使用,這里重新貼一次沒有注釋的供學習;#!/usr/bin/python
while?True:
input?=?raw_input('Please?input?your?username:')
if?input?==?"wenlong":
password?=?raw_input('Please?input?your?pass:')
p?=?'123'
while?password?!=?p:
password?=?raw_input('Please?input?your?pass?again:')
if?password?==?p:
print?'welcome?to?select?system!'
while?True:
match?=?0
input?=?raw_input("Please?input?the?name?whom?you?want?to?search?:")
while?not?input.strip():
print?'No?match?item?found!'
input?=?raw_input("Please?input?the?name?whom?you?want?to?search?:")
name_file?=?file('search_name.txt')
while?True:
line?=?name_file.readline()
if?len(line)?==?0:
break
if?input?in?line:
print?'Match?item:?'??,?line
match?=?1
if?match?==?0?:
print?'No?match?item?found!'
else:?print?"Sorry?,user??%s?not?found?"?%input
測試圖片:
總結
以上是生活随笔為你收集整理的python查找输出文字_Python基础练习,查询文本内容并输出;的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 联想展示 ThinkBook Plus
- 下一篇: python列表索引超出范围 等于啥_p