Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Python - Read text that comes after a string is found in text file

EIijah1
 Share

Go to solution Solved by Slottr,

Split the lines and use list indexes to get the values you want out of a 2D array 

So I'm relatively new to Python and have only been coding 1-2 hours a day for a month or so.
I've come across a problem at work where I'm pulling data out of a text file into a spreadsheet manually.
I don't need every single line as it's a report from another department 

So far I've been able to workout how to read a line I know is there
 

with open('example.txt') as txtfile:
    if 'First Name' in txtfile.read():
        print("true")

But I need to be able to read the line directly after 'First Name' rather than 'First Name' itself
I've done a lot of googling but haven't come across this exact problem
Below is an example of the text file received, but I would only need to pull some of the data rather than every line. 
 

EmployeeID: 123456
Issue at 18/05/2022
Staff Member Name Joe Bloggs
First Name Joe
Preferred Name Jo
Middle Name Melanie
Surname Bloggs
Start Date 27/06/2022
Contract End Date
Country New Zealand

Link to comment
Share on other sites

Link to post
Share on other sites

Split the lines and use list indexes to get the values you want out of a 2D array 

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, Slottr said:

Split the lines and use list indexes to get the values you want out of a 2D array 

Damn that's actually so simple, love it! 
Thanks for your quick response

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share


×