4 Replies - 388 Views - Last Post: 11 July 2012 - 10:35 AM Rate Topic: -----

#1 crazy123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 138
  • Joined: 04-August 11

Read from a certain line

Posted 10 July 2012 - 04:34 PM

There is a possibility to read a file from a certain line?

I read a big file with 1000000 of lines.
If I want to store the data of 500000 line to the last line. I need to open the file and read through until you reach the 500000 line where I'm going to start storing the data? Or I can read from the line 500000?
Is This A Good Question/Topic? 0
  • +

Replies To: Read from a certain line

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 5,981
  • Joined: 08-June 10

Re: Read from a certain line

Posted 10 July 2012 - 04:58 PM

If you don't start from the beginning and read until line 500000, how will you know where line 500000 is? Unless the lines have a constant size, in which case you could use File.seek to move to the correct position, you will have to open the file and look for the 500000'th new-line to discover the correct spot to start reading from.
Was This Post Helpful? 0
  • +
  • -

#3 crazy123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 138
  • Joined: 04-August 11

Re: Read from a certain line

Posted 10 July 2012 - 05:09 PM

View PostAtli, on 10 July 2012 - 04:58 PM, said:

If you don't start from the beginning and read until line 500000, how will you know where line 500000 is? Unless the lines have a constant size, in which case you could use File.seek to move to the correct position, you will have to open the file and look for the 500000'th new-line to discover the correct spot to start reading from.


That is my question. I think there's no way.
Was This Post Helpful? 0
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Read from a certain line

Posted 10 July 2012 - 08:29 PM

The first line is any and all text before the first new line character. The second line is all the text after the first new line and before the second one. Thus the 500000 line is the text between the 499999 new line char and the 500000 one. It's pretty much an iterative problem.
Was This Post Helpful? 0
  • +
  • -

#5 crazy123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 138
  • Joined: 04-August 11

Re: Read from a certain line

Posted 11 July 2012 - 10:35 AM

View Postatraub, on 10 July 2012 - 08:29 PM, said:

The first line is any and all text before the first new line character. The second line is all the text after the first new line and before the second one. Thus the 500000 line is the text between the 499999 new line char and the 500000 one. It's pretty much an iterative problem.


OK. Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1