473,786 Members | 2,866 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 22165
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.dewri tes:
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.dewri tes:
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
6861
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 replace just one item. I know I can get the entire array, then save the whole thing (with a for loop and if statements so that the changed data will be saved), but it seems like a lot of unnecessary reading and writing. Is there a way to directly save...
5
4038
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 the content into a string, and how to read line-by-line consecutively, but how can i read just 1 specific line? Thank you.
5
2763
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 information out of a text file and need to pull specific words out of a string. For example, the text file looks sorta like this:
2
3049
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
5820
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 file.. does anyone know how i can get it to write meow on the line after "putwordhere"???????????? #include <iostream> #include <fstream> #include <string> using namespace std; int main(){ string line; string searchage;
10
15098
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 without running out of memory, so I'm reading and as I'm reading I'm going to write to a file. This is the thing though, I already can do this and have it done, but I want to modify the program so you can choose what data you want to take out. To...
7
3212
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 code I need to put it into. using System; using System.Drawing; using System.IO;
6
18305
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 text into the next row. so: 1. how do i write to a new line every time i write to the file? 2. if i dont want to write to a new line but just want to insert it
6
4984
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 able to
0
9650
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10164
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9962
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6748
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.