【问题标题】:taking input from user and search it in a text file and return the position of that point in the textfile从用户那里获取输入并在文本文件中搜索并返回该点在文本文件中的位置
【发布时间】:2019-07-30 06:52:21
【问题描述】:

我正在尝试在文本文件中搜索一个字符串(由用户输入),如果该字符串存在于文本文件中,那么它将返回它的位置(文件中的位置)

我在 python 中使用了文本文件的 seek 和 tell 方法

def search(self,identity):
    with open("dbase.txt", 'r') as dbase:
      find = dbase.readline()
      while str.casefold(find) == str.casefold(identity):
          pos = dbase.tell() 
          find = dbase.readline()
          return pos

完整代码:

class app:
    ''' class that takes the data and save it
    into a text file name dbase.txt'''
    def get_data(self):
        self.name = input("Name : ")
        self.add = input("Address : ")
        self.mob = input("Mobile : ")
    def write_data(self):
        dbase = open("dbase.txt",'a')
        dbase.write(self.name+"\n")
        dbase.write(self.add+"\n")
        dbase.write(self.mob+"\n")
        dbase.close()
    def read_data(self,pos):
        dbase = open("dbase.txt",'r')
        dbase.seek(pos)
        self.name = dbase.readline()
        self.add = dbase.readline()
        self.mob = dbase.readline()  
        print(self.name)
        print(self.add)
        print(self.mob)
    def search(self,identity):
            data = open("dbase.txt", 'r').read()
            desired_string = identity
            if desired_string in data:
                pos = data.find(desired_string)
                return pos
            else:
                print("The desired string does not exist in the file")

call = app()
f = input("Enter :")
pos = call.search(f)
call.read_data(pos)

identity 代表我在此函数中作为参数传递的用户输入,我想在文件中匹配此身份,并且我在变量 find 中提取文件数据,因此如果 find 等于 identity 那么我想返回它是文件中的当前位置,但它不起作用,我试图在 while 循环中打印一些东西,例如 print("x") 或其他东西来检查天气 While 循环条件是否为真,因为如果它为真,那么它将打印那个“x”,但它没有打印出我得出的结论是while循环条件为假的任何东西,因此我认为故障就在这条线上。

while str.casefold(find) == str.casefold(identity):

但我不明白为什么会这样,因为我输入的字符串实际上存在于文件中。

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:
    data = open("filename.ext", 'r').read().replace("\n", "\n\n")
    
    desired_string = "apples"
    
    if desired_string in data:
        index = data.find(desired_string)
    else:
        print("The desired string does not exist in the file")
    

    上述代码将在文件中搜索所需的字符串,如果存在,则其第一次出现的索引将存储在变量index 中。否则文件中不存在所需的字符串将被打印出来。

    【讨论】:

    • 告诉我一件事,如果字符串是“apples”,则存在于包含一些数据的文件中,例如“香蕉橙葡萄苹果”,还有一件事是每个水果名称都在新行中那么如果我搜索苹果,它将在索引中存储什么,然后它将在索引中存储 0,就好像获取字母苹果列表然后“a”在 0 位置它将在索引中存储苹果的文件位置,如由tell函数给出的那样
    • @KHANAYAAN 如果文件内容是"banana orange grapes apples",那么index会存储值21。为什么?因为 21 是我们的字符串匹配的索引。 index 变量将存储我们的字符串匹配的第一个字符的索引。
    • 仍然没有给出想要的结果我打印了 3 个水果的名字让我们再次考虑数据是“香蕉橙葡萄苹果”我的意思是desired_string 是橙色然后光标必须从 'o'橙子到苹果的“s”,但它只打印橙色葡萄仍然苹果不打印
    • @KHANAYAAN 我无法理解你想用上面的代码实现什么。是否要在匹配所需字符串后打印文件的所有内容?
    • 好吧,让我解释一下,我实际上想使用 get_data() [用于获取用户输入] 和 write_data() [用于编写该文本文件中保存一个人的姓名地址和手机号码在文件 dbase.txt] 我希望这一点很清楚现在接下来要做的事情现在我希望用户必须输入他/她的名字才能看到他的数据所以在变量'f'中[见最后第 3 行]我拿使用 search() 在文本文件中搜索他/她的名字的用户的名字,在搜索中我已经传递了他/她的名字,即变量 'f' 作为参数,在搜索中被变量 'identity' 接受()
    猜你喜欢
    • 2015-07-17
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多