Click here to Skip to main content
15,891,993 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For exmple with readlines() the program runs through the entire txt
How to jump to a row and read only that row?

/Johan

What I have tried:

Cant find it described on google
Posted
Updated 17-Apr-20 7:37am

No.

The actual file that is stored does not have lines within it, it actually has line-breaks within it to mark where a new line starts.

So the only way to find where a "new line" starts is to read in the file and look for the line-break markers
 
Share this answer
 
Text files don't have "lines", they are a continuous stream of characters which contain a newline character (which can differ from system to system) which is interpreted by software as an "end of line" character.
As such, "Jumping to a line" is not possible, because lines are not all the same length, and a text file does not contain any index to where the lines start.

That doesn't mean you can't do it, but you have to either maintain or build a separate index as to the position in the file of the line start, and then you can use Seek to move to that position.

But be aware that you can't "insert" or "delete" from a text file for the same reason - no concept of lines means the operation just doesn't work. To do it, you have to copy the file into a new file up to the start of the INSERT or DELETE, add or skip your data, then copy the rest of the file. You can then delete the original, and rename the new file.
 
Share this answer
 
Caveat - I am a Python novice so this solution is unlikely to be bug free.

As you will now know from Solutions 1 and 2, the file is a continuous steam of characters, where the newline character separates the text into what we humans think of as "lines".

Solution 1 gave you a big clue as to an easy approach
Quote:
read in the file and look for the line-break markers
Read the entire file as a single string..
Python
file = open('my_text_file',mode='r')
wholefile = file.read()
file.close()
You can then get a list of all of the "lines" from the file using splitlines[^] e.g.
Python
listlines = wholefile.splitlines(false)
You could then extract the line you want from that list e.g. to get the 6th "line":
Python
line6 = listlines[5]
There is a good discussion of the pros and cons of splitlines versus split by Danziger here[^]
 
Share this answer
 
Quote:
Is there a way to jump to a txt file line without reading it line by line? How

Short answer: No and Yes.
Generally speaking, it is No because each line have different length so it is impossible to predict the position of a particular line in file.
If the file is specially crafted, with the same length for each line, going to a particular line is easy, it is just a matter of multiplying the line number with line size. This technique is very common with databases, it is the reason why defining a table imply giving the maximum size of each field.
 
Share this answer
 
Comments
CHill60 17-Apr-20 13:42pm    
However, you can "predict" the position by locating the linefeed character
Patrice T 17-Apr-20 13:47pm    
The OP said "without reading it line by line".
CHill60 17-Apr-20 15:10pm    
Read the entire file as a single stream. Split on linefeed (in memory)
Patrice T 17-Apr-20 15:15pm    
My view is that reading the whole file and split, is the same as reading line by line.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900