Hi guys,

First post so please don't be too harsh :D.

I'm trying to open a .txt file, search for a specific line starting with (" Performance Summary"), copying the first and third numbers in the columns of the 4th line below it and every line below that until there is a space.

Here is an example of the txt file, I need the 1st column and 3rd. (It doesn't look like it, but each number has a space e.g. 1000.0 3.86 36.90... and i need the first and third number of each row)

Rubbish
Rubbish
etc.

Performance Summary

-------------------

Eng.Speed [rev/min] B.Power [kW] B.Torque [Nm] BMEP [bar] BSFC [g/kW/hr]
-------------------------------------------------------------------------------
1000.0 3.86 36.90 7.74 273.51
2000.0 9.17 43.79 9.18 253.08
3000.0 15.48 49.28 10.33 250.95
4000.0 20.51 48.96 10.26 248.44
5000.0 26.60 50.80 10.65 248.71
6000.0 35.72 56.85 11.92 247.68
7000.0 44.67 60.94 12.78 248.26
8000.0 55.96 66.79 14.00 253.12

Here is what I have so far,I kind of understand that I can join multiple things to the buffer, but I can't get further than copying " Performance Summary" to my output...

data_file = open("file1.txt")
out_file = open("summary.txt", "w")
buffer = []
for line in data_file:
    buffer.append(line)
    if line.startswith(" Performance Summary"):
        out_file.write("".join(buffer))
        buffer = []

data_file.close()
out_file.close()

Hope you can help,
Thanks,
Kitson

Recommended Answers

All 3 Replies

First things first, I would recommend opening files this way:

with open(filename) as file:
    """ Do some stuff """

This guarantees that no matter what happens with your code (unexpected error, hangs, etc.) the file will always be closed properly.

As for the other tasks - once you've found your line ("performance summary"), you can traverse to the next line manually by calling

next(file) # returns the next line in the file

Since 'next(file)' gives the next line from the current position in the file, you could call it after you've found your line, and then start parsing those data/numbers lines.

I wrote a quick and dirty example that puts it all together:

with open(filename) as file:
    for line in file:
        if 'Performance Summary' in line: # the line we want
            data_lines = []
            for i in range(3):
                next(file) # advance 3 lines

            data = next(file).strip() # read the 4'th line, remove redundant spaces
            while data:               # while data isn't an empty line (empty lines == False in python)
                data_lines += data    # store in list
                data = next(file).strip() # keep reading...
            
            break # stop checking the rest of lines in the file

After you have the list 'data_lines', you can parse each item in it and write the results to another file.

Hope this helps a bit - good luck :-)

commented: Really Helpful +0

Hi,
Thanks for the quick response, it was really helpful! :D. Worked first time. Do I have to close the file I open on the first line of your code? Or does python just save the data in the file to "file"?
Thanks again,
Kitson

Glad to hear everything worked out.
No, you don't need to close the file opened in the first line, since opening files with:

with open(name_of_file) as file_object

will make sure python closes it for you properly, no matter what.

You should note that the file_object is not the data stored within the file, but rather a file handler that enables you to perform different file operations.
Have a look here for more details: http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files

Good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.