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
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 1,003 Dec-19-2024, 11:57 AM
Last Post: snippsat
  [SOLVED] [Linux] Write file and change owner? Winfried 6 1,489 Oct-17-2024, 01:15 AM
Last Post: Winfried
  Read TXT file in Pandas and save to Parquet zinho 2 1,206 Sep-15-2024, 06:14 PM
Last Post: zinho
  [solved] how to delete the 10 first lines of an ascii file paul18fr 7 1,689 Aug-07-2024, 08:18 PM
Last Post: Gribouillis
  Pycharm can't read file Genericgamemaker 5 1,542 Jul-24-2024, 08:10 PM
Last Post: deanhystad
  Python is unable to read file Genericgamemaker 13 3,573 Jul-19-2024, 06:42 PM
Last Post: snippsat
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 3,214 May-03-2024, 07:23 AM
Last Post: Pedroski55
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 9,621 Feb-29-2024, 12:30 AM
Last Post: Winfried
  Extracting specific file from an archive tester_V 4 2,045 Jan-29-2024, 06:41 PM
Last Post: tester_V
  Recommended way to read/create PDF file? Winfried 3 4,606 Nov-26-2023, 07:51 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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