Jump to content

Reading specific lines from a file (Python3)

lewdicrous
Go to solution Solved by Sauron,
2 minutes ago, lewdicrous said:

There is a fixed number of lines for the header, body and footer. I'm trying to ignore the header and footer so that I can use the information from the body.

 

 

I have to open two text files that are more or less identical, take the information contained in both and add them  to a third text file.. if that made any sense

then just change the second loop from this

while lines != "":
        print (lines.split())
        lines = infile.readline()

to this

while count < 13:
        print (lines.split())
        lines = infile.readline()
        count += 1

assuming the number of data lines is the same. If it isn't, well, as I said you should be checking for format.

Spoiler

for y in range(1,2):
    infile = open("ResultsRound%d.txt" %y, "r")
    lines = infile.readline()
    count = 1
    while count < 8: 
        lines = infile.readline()
        count += 1
    scored = 0
    conceded = 0
    while lines != "":
        print (lines.split())
        lines = infile.readline()
    conceded += int((lines.split()[2])) 
    scored += int((lines.split()[1])) 
print (scored,conceded)

the code I wrote is above and the output it produces is below, I don't want the last line (only the teams)


['Team1', '7', '6', '7', '1', '50.0']
['Team2', '5', '10', '1', '-5', '0.0']
['Team3', '4', '5', '6', '-1', '50.0']
['Team4', '14', '6', '12', '8', '100.0']
['Team5', '7', '9', '4', '-2', '25.0']
['=======================================================================================================']

 

Hey, I'm trying to read certain lines from a text file while ignoring everything before and after those lines, the program I wrote ignores the first set of unwanted lines, but it still shows the last line (unwanted).

5acdcb505f8fd_Screenshot-2018-4-11MicrosoftWord-HW3_updated_Abirdocx-HW3_SP18pdf.png.18aa29f654b3f55a503e6b175e1d9971.png

This is what the text file looks like.

 

How can I improve the code so that it does what I want?

Link to comment
Share on other sites

Link to post
Share on other sites

If the file fits in memory you can just start slicing from the line you want. Also it's good practise to use the "with" statement with file, so that you don't accidentally forget to close the file when you are done with it.

Just read in everything and slice to the desired lines:

with open("ResultsRound%d.txt" %y, "r") as f:
	contents = f.readlines()
	contents_desired = contents[start_line:-1]
    # Other stuff

 

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, tikker said:

- snip -

I'll try it out, thanks

I am closing the program, I just didn't include it in the code above but thanks for the heads up.

Cheers!

Link to comment
Share on other sites

Link to post
Share on other sites

While selecting the lines individually works, it's not good practice - what if more lines are added? You should print every line that fits the desired format as opposed to the lines at a fixed index.

 

For example, you could check if the number of items in the list returned by split() is equal to 6, or go further and check that every string is of the correct type (number or string).

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, Sauron said:

While selecting the lines individually works, it's not good practice - what if more lines are added?

There is a fixed number of lines for the header, body and footer. I'm trying to ignore the header and footer so that I can use the information from the body.

 

 

I have to open two text files that are more or less identical, take the information contained in both and add them together then output the new information to a third text file.. if that made any sense

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, lewdicrous said:

There is a fixed number of lines for the header, body and footer. I'm trying to ignore the header and footer so that I can use the information from the body.

 

 

I have to open two text files that are more or less identical, take the information contained in both and add them  to a third text file.. if that made any sense

then just change the second loop from this

while lines != "":
        print (lines.split())
        lines = infile.readline()

to this

while count < 13:
        print (lines.split())
        lines = infile.readline()
        count += 1

assuming the number of data lines is the same. If it isn't, well, as I said you should be checking for format.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Sauron said:

- snip -

This has been bugging me since last week..

 

Thank you!

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

×