Python Forum
[Solved] Trying to read specific lines from a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved] Trying to read specific lines from a file
#1
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!
likes this post
Reply
#2
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
Reply
#3
(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.
Reply
#4
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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?
Reply
#6
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?
Reply
#7
(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
Laplace12 likes this post
Reply
#8
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Input parameter: Single file or glob? Winfried 0 123 Sep-10-2021, 11:54 AM
Last Post: Winfried
Thumbs Up [SOLVED] Find last occurence of pattern in text file? Winfried 4 405 Aug-13-2021, 08:21 PM
Last Post: Winfried
Thumbs Up Parsing a YAML file without changing the string content..?, Flask - solved. SpongeB0B 2 267 Aug-05-2021, 08:02 AM
Last Post: SpongeB0B
  Read csv file through PyCharm kimx0961 3 438 Aug-01-2021, 07:05 PM
Last Post: perfringo
  Importing a function from another file runs the old lines also dedesssse 6 374 Jul-06-2021, 07:04 PM
Last Post: deanhystad
  [Solved] Plotting data from txt file Laplace12 1 331 Jul-06-2021, 07:14 AM
Last Post: Laplace12
  Why it does not print(file.read()) Rejaul84 1 398 Jul-01-2021, 10:37 PM
Last Post: bowlofred
  Read and write active Excel file euras 4 530 Jun-29-2021, 11:16 PM
Last Post: Pedroski55
  [Solved] Reading every nth line into a column from txt file Laplace12 7 784 Jun-29-2021, 09:17 AM
Last Post: Laplace12
  [Solved] Using readlines to read data file and sum columns Laplace12 4 509 Jun-16-2021, 12:46 PM
Last Post: Laplace12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020