使用python搜索文件中的数据?

2024-09-27 23:27:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有档案文件.txt我想用电话号码搜索文件中的数据,例如,如果我输入了996452544,那么结果将是Alex 996452544alex@gmail如何在python中做到这一点我不知道我是新手帮我。在

在文件.txt在

Alex 996452544 alex@gmail
Jhon 885546694 jhon@gmail
Arya 896756885 arya@gmail

在代码.py在

^{pr2}$

Tags: 文件数据代码pytxt电话号码gmailalex
3条回答
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()

这个简单的代码就可以完成这项工作

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()

希望这能有所帮助。如果是,接受并投赞成票

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()

相关问题 更多 >

    热门问题