简体   繁体   中英

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. 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. 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.

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
  • Find the next 5 lines
  • Save it to a variable
  • Print it

Solution

Let's just call our text file 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:

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'. 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'.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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