How to jump to a specific line in python

I want to go to line 34 in a .txt file and read it. How would you do that in Python?

T. Zengerink

4,2195 gold badges29 silver badges31 bronze badges

asked Mar 15, 2010 at 1:11

ArchHaskellerArchHaskeller

1,2401 gold badge12 silver badges28 bronze badges

3

Use Python Standard Library's linecache module:

line = linecache.getline[thefilename, 33]

should do exactly what you want. You don't even need to open the file -- linecache does it all for you!

answered Mar 15, 2010 at 1:21

Alex MartelliAlex Martelli

823k163 gold badges1202 silver badges1379 bronze badges

6

This code will open the file, read the line and print it.

# Open and read file into buffer
f = open[file,"r"]
lines = f.readlines[]

# If we need to read line 33, and assign it to some variable
x = lines[33]
print[x]

answered Nov 8, 2011 at 5:38

A solution that will not read more of the file than necessary is

from itertools import islice
line_number = 34

with open[filename] as f:
    # Adjust index since Python/islice indexes from 0 and the first 
    # line of a file is line 1
    line = next[islice[f, line_number - 1, line_number]]

A very straightforward solution is

line_number = 34

with open[filename] as f:
    f.readlines[][line_number - 1]

IAbstract

19.6k15 gold badges92 silver badges143 bronze badges

answered Mar 15, 2010 at 2:32

Mike GrahamMike Graham

70.8k14 gold badges97 silver badges129 bronze badges

There's two ways:

  1. Read the file, line by line, stop when you've gotten to the line you want
  2. Use f.readlines[] which will read the entire file into memory, and return it as a list of lines, then extract the 34th item from that list.

Solution 1

Benefit: You only keep, in memory, the specific line you want.

code:

for i in xrange[34]:
    line = f.readline[];
# when you get here, line will be the 34th line, or None, if there wasn't
# enough lines in the file

Solution 2

Benefit: Much less code
Downside: Reads the entire file into memory
Problem: Will crash if less than 34 elements are present in the list, needs error handling

line = f.readlines[][33]

answered Mar 15, 2010 at 1:20

Lasse V. KarlsenLasse V. Karlsen

371k97 gold badges620 silver badges805 bronze badges

You could just read all the lines and index the line your after.

line = open['filename'].readlines[][33]

answered Mar 15, 2010 at 1:15

tarntarn

2,1822 gold badges13 silver badges17 bronze badges

1

for linenum,line in enumerate[open["file"]]:
    if linenum+1==34: print line.rstrip[]

answered Mar 15, 2010 at 1:24

ghostdog74ghostdog74

313k55 gold badges252 silver badges339 bronze badges

1

I made a thread about this and didn't receive help so I took matter into my own hands.

Not any complicated code here.

import linecache
#Simply just importing the linecache function to read our line of choosing

number = int[input["Enter a number from 1-10 for a random quote "]]
#Asks the user for which number they would like to read[not necessary] 

lines = linecache.getline["Quotes.txt", number]
#Create a new variable in order to grab the specific line, the variable 
#integer can be replaced by any integer of your choosing.

print[lines]
#This will print the line of your choosing.

If you are completing this in python make sure you have both files [.py] and [.txt] in the same location otherwise python will not be able to retrieve this, unless you specify the file location. EG.

linecache.getline["C:/Directory/Folder/Quotes.txt

This is used when the file is in another folder than the .py file you are using.

Hope this helps!

answered Jul 10, 2017 at 15:19

AwaisAwais

556 bronze badges

Chủ Đề