Python Forum
[Solved] Trying to read specific lines from a file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: [Solved] Trying to read specific lines from a file (/thread-34013.html)



[Solved] Trying to read specific lines from a file - Laplace12 - Jun-18-2021

Hey,

I'm struggling with a simple code. I want to extract certain lines from data files. The data files have up to a 100 times datasets after each other, so basically I want to read the same lines from each dataset. To simplify, the file that I want to read looks like this:

##############################################
Data set 1                                           

Value1
Value2

###################################################
Data set 2

Value1
Value2

###################################################
and so on for up to 100 datasets, and I wish to get Value1 and Value2 extracted for all of them.

For reading the first dataset, this code of course works:

file = open('filename')
all_lines = file.readlines()
print(all_lines[35])
print(all_lines[36])
print(all_lines[37])
print(all_lines[38])
print(all_lines[39])
print(all_lines[44])
print(all_lines[45])
I tried adding a n = number of lines command but ended up printing the specific line n times instead of separate values. I'm not sure how I could get the code to pick all those certain lines that I need and print all without having to separate the number of each line I want to print - for a large amount of datasets that seems pointless. I appreciate any help!


RE: Trying to read specific lines from a file - Larz60+ - Jun-18-2021

something like:
recs_needed = [0, 24, 72, 99]

with open('myfile') as fp:
    lines = fp.read()

for n, line in enumerate(lines):
    if n in recs_needed:
        # save this one



RE: Trying to read specific lines from a file - Laplace12 - Jun-21-2021

(Jun-18-2021, 02:17 PM)Larz60+ Wrote: something like:
recs_needed = [0, 24, 72, 99]

with open('myfile') as fp:
    lines = fp.read()

for n, line in enumerate(lines):
    if n in recs_needed:
        # save this one

Hey! Tried this, but I just got the results for the first spectrum. The problem seems that in recs_needed I'd still have to check for every single line I want to print manually.


RE: Trying to read specific lines from a file - buran - Jun-21-2021

Instead of hard-coded line numbers it will be much better to parse the file. Based on what you show it looks pretty trivial to do so.


RE: Trying to read specific lines from a file - Laplace12 - Jun-21-2021

(Jun-21-2021, 07:30 AM)buran Wrote: Instead of hard-coded line numbers it will be much better to parse the file. Based on what you show it looks pretty trivial to do so.

Thanks for the tip! I've only ever parsed files with pandas df, though, but I'm not sure that helps in this case, or does it still somehow make it easier to create loops if the file's parsed?


RE: Trying to read specific lines from a file - Laplace12 - Jun-21-2021

Alright, I now figured I can maybe try to iterate n and then loop over the lines I want:

r = range(1, 71)
n = iter(r)
 
with open('file') as fp:
    lines = fp.readlines()
    
lines_needed = [35*n, 36*n, 37*n, 38*n, 39*n, 44*n, 45*n]
 
for n, line in enumerate(lines):
    if n in lines_needed:
        print(line)
But I get this error:

    lines_needed = [35*n, 36*n, 37*n, 38*n, 39*n, 44*n, 45*n]

TypeError: unsupported operand type(s) for *: 'int' and 'range_iterator'
I think iter and range should work, but apparently changing the line number like this doesn't. Is the problem in itself with *n?


RE: Trying to read specific lines from a file - snippsat - Jun-21-2021

(Jun-21-2021, 07:55 AM)Laplace12 Wrote: Alright, I now figured I can maybe try to iterate n and then loop over the lines I want:
I still hard-coded lines as you most type them.
To give a example where find similarity in data set,then parse line under this.
found = False
with open('data_set.txt') as f,open('output.txt', 'w') as f_out:
    for line in f:
        if not '#' in line and not 'Data' in line and found:
            f_out.write(line)
        else:
            found = True

Output:
Value1 Value2 Value1 Value2



RE: Trying to read specific lines from a file - Laplace12 - Jun-21-2021

(Jun-21-2021, 10:02 AM)snippsat Wrote:
(Jun-21-2021, 07:55 AM)Laplace12 Wrote: Alright, I now figured I can maybe try to iterate n and then loop over the lines I want:
I still hard-coded lines as you most type them.
To give a example where find similarity in data set,then parse line under this.
found = False
with open('data_set.txt') as f,open('output.txt', 'w') as f_out:
    for line in f:
        if not '#' in line and not 'Data' in line and found:
            f_out.write(line)
        else:
            found = True

Output:
Value1 Value2 Value1 Value2

Oh I see - thanks a lot, this works perfectly!