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
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 546 Feb-29-2024, 12:30 AM
Last Post: Winfried
  Extracting specific file from an archive tester_V 4 428 Jan-29-2024, 06:41 PM
Last Post: tester_V
  Recommended way to read/create PDF file? Winfried 3 2,785 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,314 Nov-09-2023, 10:56 AM
Last Post: mg24
  read file txt on my pc to telegram bot api Tupa 0 1,052 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,043 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,162 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 6,016 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Loop through json file and reset values [SOLVED] AlphaInc 2 1,962 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Read csv file with inconsistent delimiter gracenz 2 1,149 Mar-27-2023, 08:59 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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