简体   繁体   English

使用python搜索文本文件中的行

[英]Searching for lines in a text file using python

I am trying to search string in a txt file. 我正在尝试在txt文件中搜索字符串。 Here is a sample txt file. 这是一个示例txt文件。 I am working with: 我正在与:

abc
def
ghi
centre
jkl
mno
pqr
123
456

I am trying to match the lines in the file and if a match is found then exit the program if a match is not found then add the line to the file: 我正在尝试匹配文件中的行,如果找到匹配项,如果找不到匹配项,则退出程序,然后将行添加到文件中:

I have the following code that I am working with: my issue is it works correctly if I try to match "abc" in the file, but say if I try to match "pqr" I see my second if block is also executed. 我有以下正在使用的代码:我的问题是,如果我尝试匹配文件中的“ abc”,它可以正常工作,但是如果我尝试匹配“ pqr”,我会看到我的第二个if块也被执行了。

Following is the code I have so far. 以下是我到目前为止的代码。

#!/usr/bin/python

new_data = raw_input("Enter txt:")    
file_name = "test.txt"

with open(file_name, "r") as F:
    data = F.readlines()

for num, line in enumerate(data, start=1):
    if line.strip() == new_data:
        print "exists {} {}".format(new_data, num)
        break

    if line.strip() != new_data:
        print "new data {} to be entered into line {}".format(new_data, num)

     else:
         print "break"

Following is the output i get if i try to match say "123" from the file: 以下是我尝试从文件中匹配“ 123”时得到的输出:

Enter txt:123
new data 123 to be entered into line 1
new data 123 to be entered into line 2
new data 123 to be entered into line 3
new data 123 to be entered into line 4
new data 123 to be entered into line 5
new data 123 to be entered into line 6
new data 123 to be entered into line 7
exists 123 8

IIUC, you want the second block to execute only if the data does not exist. IIUC,您希望仅当数据不存在时才执行第二个块。 Well, try this: 好吧,试试这个:

for num, line in enumerate(data, start=1):
    if line.strip() == new_data:
        print "exists {} {}".format(new_data, num)
        break
else:
    print "new data {} to be entered into line {}".format(new_data, num)

You can take advantage of the for...else syntax. 您可以利用for...else语法。 The else is executed if the loop does not break , ie, if the data does not exist in the file. 如果循环不break ,即文件中不存在数据,则执行else

The problem with your code is that if the new_data is not found in iteration your 2nd if condition prints new data to be entered even if it exists in the subsequent iteration, only the the first data abc will give desired result as the iteration starts from there. 您的代码存在的问题是,如果在迭代中未找到new_data则第二个条件将打印new data to be entered即使该new data to be entered在后续迭代中也存在,则只有第一个数据abc会从迭代开始时提供所需的结果。

One good solution is already given by @COLDSPEED . @COLDSPEED已经给出了一个好的解决方案。

Another basic approach could be this: 另一个基本方法可能是:

for num, line in enumerate(data, start=1):
    if line.strip() == new_data:
        flag = 1
        print("exists {} {}".format(new_data, num))
        break
    else:
        flag = 0

if flag == 0:
    print("new data {} to be entered into line {}".format(new_data, num))

It will iterate through the whole data and if the new_data is found it will set the flag to be 1 and will break, else it will set the flag to be 0 and will print new data to be entered 它将遍历整个数据,如果找到了new_data ,它将flag设置为1并中断,否则它将flag设置为0并打印new data to be entered

Hope this help :) 希望这个帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM