简体   繁体   English

我正在努力从 python 的文本文件中读取特定的单词和行

[英]I am struggling with reading specific words and lines from a text file in python

I want my code to be able to find what the user has asked for and print the 5 following lines.我希望我的代码能够找到用户要求的内容并打印以下 5 行。 For example if the user entered "james" into the system i want it to find that name in the text file and read the 5 lines below it.例如,如果用户在系统中输入“james”,我希望它在文本文件中找到该名称并读取它下面的 5 行。 Is this even possible?这甚至可能吗? All i have found whilst looking through the internet is how to read specific lines.我在浏览互联网时发现的只是如何阅读特定的行。

So, you want to read a .txt file and you want to read, let's say the word James and the 5 lines after it.所以,你想读一个.txt文件,你想读,比如说James和它后面的 5 行。

Our example text file is as follows:我们的示例文本文件如下:

Hello, this is line one
The word James is on this line
Hopefully, this line will be found later,
and this line,
and so on...
are we at 5 lines yet?
ah, here we are, the 5th line away from the word James
Hopefully, this should not be found

Let's think through what we have to do.让我们想想我们必须做什么。


What We Have to Do我们必须做什么

  • Open the text file打开文本文件
  • Find the line where the word 'James' is找到单词“James”所在的行
  • Find the next 5 lines找到接下来的 5 行
  • Save it to a variable将其保存到变量
  • Print it打印它

Solution解决方案

Let's just call our text file info.txt .让我们调用我们的文本文件info.txt You can call it whatever you want.你可以随心所欲地称呼它。

To start, we must open the file and save it to a variable:首先,我们必须打开文件并将其保存到变量中:

file = open('info.txt', 'r') # The 'r' allows us to read it

Then, we must save the data from it to another variable, we shall do it as a list:然后,我们必须将其中的数据保存到另一个变量中,我们将其作为一个列表进行:

file_data = file.readlines()

Now, we iterate (loop through) the line with a for loop, we must save the line that 'James' is on to another variable:现在,我们用 for 循环iterate (循环)该行,我们必须将“James”所在的行保存到另一个变量:

index = 'Not set yet'
for x in range(len(file_data)):
    if 'James' in file_data[x]:
        index = x
        break
    
if index == 'Not set yet':
    print('The word "James" is not in the text file.')

As you can see, it iterates through the list, and checks for the word 'James'.如您所见,它遍历列表,并检查单词“James”。 If it finds it, it breaks the loop.如果它找到它,它将打破循环。 If the index variable still is equal to what it was originally set as, it obviously has not found the word 'James'.如果index变量仍然等于它最初设置的值,那么它显然还没有找到“詹姆斯”这个词。

Next, we should find the five lines next and save it to another variable:接下来,我们应该找到接下来的五行并将其保存到另一个变量中:

five_lines = [file_data[index]]
for x in range(5):
    try:
        five_lines.append(file_data[index + x + 1])
    except:
        print(f'There are not five full lines after the word James. {x + 1} have been recorded.')
        break

Finally, we shall print all of these:最后,我们将打印所有这些:

for i in five_lines:
    print(i, end='')

Done!完毕!

Final Code最终代码

file = open('info.txt', 'r') # The 'r' allows us to read it
file_data = file.readlines()


index = 'Not set yet'
for x in range(len(file_data)):
    if 'James' in file_data[x]:
        index = x
        break
    
if index == 'Not set yet':
    print('The word "James" is not in the text file.')


five_lines = [file_data[index]]
for x in range(5):
    try:
        five_lines.append(file_data[index + x + 1])
    except:
        print(f'There are not five full lines after the word James. {x + 1} have been recorded.')
        break
    
for i in five_lines:
    print(i, end='')

I hope that I have been helpful.我希望我对您有所帮助。

Yeah, sure.好,当然。 Say the keyword your searching for ("james") is keywrd and Xlines is the number of lines after a match you want to return假设您搜索的关键字(“james”)是keywrd ,而Xlines是您想要返回的匹配后的行数

def readTextandSearch1(txtFile, keywrd, Xlines):

    with open(txtFile, 'r') as f:  #Note, txtFile is a full path & filename
        allLines = f.readlines()  #Send all the lines into a list
        #with automatically closes txt file at exit

    temp = []  #Dim it here so you've "something" to return in event of no match
    for iLine in range(0, len(allLines)):
        if keywrd in allLines[iLine]:
            #Found keyword in this line, want the next X lines for returning
            
            maxScan = min(len(allLines),Xlines+1)  #Use this to avoid trying to address beyond end of text file.
            for iiLine in range(1, maxScan):
                temp.append(allLines[iLine+iiLine]
            
            break #On assumption of only one entry of keywrd in the file, can break out of "for iLine" loop

    return temp

Then by calling readTextandSearch1() with appropriate parameters, you'll get a list back that you can print at your leisure.然后通过使用适当的参数调用 readTextandSearch1(),您将得到一个列表,您可以在闲暇时打印该列表。 I'd take the return as follows:我的回报如下:

rtn1 = readTextAndSearch1("C:\\Docs\\Test.txt", "Jimmy", 6)

if rtn1:  #This checks was Jimmy within Test.txt
    #Jimmy was in Test.txt
    print(rtn1)

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

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