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


Messages In This Thread
[Solved] Trying to read specific lines from a file - by Laplace12 - Jun-18-2021, 11:51 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 844 Feb-29-2024, 12:30 AM
Last Post: Winfried
  Extracting specific file from an archive tester_V 4 512 Jan-29-2024, 06:41 PM
Last Post: tester_V
  Recommended way to read/create PDF file? Winfried 3 2,886 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,447 Nov-09-2023, 10:56 AM
Last Post: mg24
  read file txt on my pc to telegram bot api Tupa 0 1,119 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,111 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,281 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 6,598 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,127 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Read csv file with inconsistent delimiter gracenz 2 1,200 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