Hi again,

I have a program which opens a file, searches for a string ("Performance Summary"), misses 3 lines then copies the following lines until there are no more.

# Extracts data from a files and saves it in a summary file


with open("file1.txt") as file:
    for line in file:
        if 'Performance Summary' in line: # Starting point
            data_lines = []
            for i in range(3):
                next(file) # Advance 3 lines

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

with open("summary.txt", "w") as out_file:
    out_file.write("".join(data_lines))

Each line which is being copied is something like this:
1000.0 3.86 36.90 7.74 273.51 63.1 1.000 1.002 No ( 10)

Where I need to copy the first and third element to generate an output something like:
1000.0 36.9
2000.0 43.79
3000.0 50.45
...
...

I tried using:

data.split()
first = data.pop(0)
third = data.pop(2)

But, I got "AttributeError: 'str' object has no attribute 'pop'" which I'm guessing is because I only have a line of text and not a list. I can't find any conversion which will work.

Hope you can help,
Kitson

I managed to extract the specific numbers I need :D

It took a while but I realised I had to assign the data.split() to something:

list = data.split()
first = list.pop(0)
...

My only problem now is I get a continuous list of numbers, can I get each data set (first, third) to be on a separate line?

I now have each number on a separate line, but I really could do with each pair on a line.

I used extend as opposed to append and added '\n' to "out_file.write('\n'.join(data_lines))"

Here is what I have altogether now:

# Extracts data from a files and saves it in a summary file


with open("file1.txt") as file:
    for line in file:
        if 'Performance Summary' in line: # Starting point
            data_lines = []
            for i in range(3):
                next(file) # Advance 3 lines

            data = next(file)# read the 4th line
            data.strip() # remove whilte spaces
            while data: # while data isn't an empty line
                list = data.split()
                first = list.pop(0)
                third = list.pop(1)
                data_lines.extend([first, third])# store in list
                data = next(file).strip()# keep reading...
            break # stop checking lines

with open("summary.txt", "w") as out_file:
    out_file.write('\n'.join(data_lines))

Hope you guys can help!

Took forever and its pretty messy but I've done it:

# Extracts data from a files and saves it in a summary file


with open("file1.txt") as file:
    for line in file:
        if 'Performance Summary' in line: # Starting point
            data_lines = []
            for i in range(3):
                next(file) # Advance 3 lines

            data = next(file)# read the 4th line
            data.strip() # remove whilte spaces
            while data: # while data isn't an empty line
                list = data.split()
                first = list.pop(0)
                third = list.pop(1)
                both = ' '.join([first, third])
                data_lines.append(both)# store in list
                data = next(file).strip()# keep reading...
            break # stop checking lines

with open("summary.txt", "w") as out_file:
    out_file.write("Eng.Speed B.Torque \n")
    out_file.write('\n'.join(data_lines))

I know the title isn't thrilling but no view in 5 hours?

Concratulations, clean up of code is one thing we are glad to advice. unfortunately I can not test this now but here is edit, which mayby need some corrections.

# Extracts data from a files and saves it in a summary file
data_lines = []
start = float('inf')
with open("file1.txt") as file: 
    for line_number, line in enumerate(file):
        if 'Performance Summary' in line: # Starting point
             start = line_number
        if line_number > start + 3:
                splitted = data.strip().split()      
                data_lines.append(splitted[0]+', '+splitted[2])
         
with open("summary.txt", "w") as out_file:
    out_file.write("Eng.Speed B.Torque \n")
    out_file.writelines(data_lines)
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.