我有一个文件file.txt我想使用电话号码搜索文件中的数据,例如,如果我输入996452544然后结果将是Alex 996452544 alex @ gmail如何在python中执行此操作我不知道我是新手帮助我。
file.txt的
Alex 996452544 alex@gmail
Jhon 885546694 jhon@gmail
Arya 896756885 arya@gmail
code.py
def searchContact():
number=raw_input("Enter phone number to search data : ")
obj1=open("file.txt","r")
re=obj1.read()
print re
obj1.close()
searchContact()
答案 0 :(得分:1)
def searchContact():
number=raw_input("Enter phone number to search data : ")
obj1=open("file.txt","r")
re=obj1.read()
print re
x= re.split("\n")
matching = [s for s in x if number in s]
print matching
obj1.close()
searchContact()
答案 1 :(得分:1)
这个简单的代码将完成工作
def searchContact():
number=raw_input("Enter phone number to search data : ")
obj1=open("file.txt","r")
for line in obj1.readlines():
if number in line:
print(line)
obj1.close()
答案 2 :(得分:1)
def searchContact():
obj1 = open("address.txt","r")
number = raw_input("Enter phone number to search data : ")
for line in obj1.readlines():
if number in line:
print line
obj1.close()
答案 3 :(得分:1)
我希望这会对你有所帮助
<Column>
<Text text=“{/USER}“>
</Column>