473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

go to specific line in text file

Hello NG,

I am searching for a way to jump to a specific line in a text file, let's
say to line no. 9000.
Is there any method like file.seek() which leads me to a given line instead
of a given byte?

Hope for help
Patrick
Jun 27 '08 #1
5 22111
On Jun 17, 8:10 pm, Patrick David <patrick.da...@tu-clausthal.de>
wrote:
Hello NG,

I am searching for a way to jump to a specific line in a text file, let's
say to line no. 9000.
Is there any method like file.seek() which leads me to a given line instead
of a given byte?
If by "jump" you mean without reading the preceding 8999 lines, and by
"text file" you mean variable length records without a separate index
structure, that method hasn't been implemented yet. AFAIK it hasn't
been implemented in any other language either :-)

The linecache module may be what you want, depending on how closely
your access patterns match those the module was designed for.

Cheers,
John
Jun 27 '08 #2
Patrick David <pa***********@tu-clausthal.dewrites:
I am searching for a way to jump to a specific line in a text file,
let's say to line no. 9000. Is there any method like file.seek()
which leads me to a given line instead of a given byte?
You can simulate it fairly easily, but it will internally read the
file line by line and will take the time roughly proportional to the
size of the file.

from itertools import islice
def seek_to_line(f, n):
for ignored_line in islice(f, n - 1):
pass # skip n-1 lines

f = open('foo')
seek_to_line(f, 9000) # seek to line 9000

# print lines 9000 and later
for line in f:
print line
Jun 27 '08 #3
On Jun 17, 10:46 pm, Hrvoje Niksic <hnik...@xemacs.orgwrote:
Patrick David <patrick.da...@tu-clausthal.dewrites:
I am searching for a way to jump to a specific line in a text file,
let's say to line no. 9000. Is there any method like file.seek()
which leads me to a given line instead of a given byte?

You can simulate it fairly easily, but it will internally read the
file line by line and will take the time roughly proportional to the
size of the file.

from itertools import islice
def seek_to_line(f, n):
The OP gave no impression that he'd restrict himself to one
seek_to_line call per open. Perhaps you need
f.seek(0)
here otherwise
seek_to_line(f, 20)
seek_to_line(f, 10)
will give something unexpected (like jumping forwards instead of
backwards).
for ignored_line in islice(f, n - 1):
pass # skip n-1 lines

f = open('foo')
seek_to_line(f, 9000) # seek to line 9000

# print lines 9000 and later
for line in f:
print line
Jun 27 '08 #4
Patrick David wrote:
Hello NG,

I am searching for a way to jump to a specific line in a text file, let's
say to line no. 9000.
Is there any method like file.seek() which leads me to a given line instead
of a given byte?

Hope for help
Patrick
Others have given the general answer (No), but if you are lucky enough to have
FIXED length lines you can jump by just calculating the byte offset of the
beginning character in line 9000 by using file.seek() and then read the bytes.
You didn't say anything about the format of the file.

-Larry
Jun 27 '08 #5
On Jun 17, 3:10*am, Patrick David <patrick.da...@tu-clausthal.de>
wrote:
>
I am searching for a way to jump to a specific line in a text file, let's
say to line no. 9000.
Is there any method like file.seek() which leads me to a given line instead
of a given byte?
As others have said, no. But if you're wondering how other file
formats do this, like BDB or PostgreSQL data files, which absolutely
have to jump to the magical spot in the file where the data is, they
keep an index at the beginning of the file that points to what byte
the data they are looking for is in.

So if it's really important to be able to do this, consider that.

Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
5
by: kevin arana | last post by:
Hello. Could anyone please show me an example of how to retrieve a specific line of a text file (say line 3 of data.txt) into a string variable? I have seen many examples on how to dump all...
5
by: akelly_image | last post by:
Okay, if anyone could toss me some idea's here, please bare with my noobish questions, I just picked up VB2005 Pro about a week ago. ( no prior VB at all ) Here's my issue.. I'm pulling...
2
by: Craig | last post by:
I have the need to write a byte of information to a specific location in a text file. eg. the file looks something like this. FYYNN Line 1 Line 2 <eof>
1
by: ecrovoyan | last post by:
I am trying to write a c++ program that reads through a text file and when i comes across a specific line it adds to that file. i have this code, but it doesnt write "meow" until the end of the text...
10
blazedaces
by: blazedaces | last post by:
Alright guys, so the title explains exactly my goal. The truth is I'm going to be reading in a lot of data from an xml file. The file is too large and there's too much data to store in arraylists...
7
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
I want to change the font color and weight at a specific position in the text. I am not sure where or what I need to do. It will be at position 155/156 from the left on each line. Here is the...
6
by: globalrev | last post by:
i ahve a program that takes certain textsnippets out of one file and inserts them into another. problem is it jsut overwrites the first riow every time. i want to insert every new piece of...
6
by: Ramesh | last post by:
Hello, I am using the ofstream class to create a text file with keys and values like: Key1=Value10 Key2=Value15 Key3=Value20 In case I need to set a new value for Key2, say value50 - I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.