简体   繁体   中英

How can I get select specific lines of a text file in python?

I have two problems now with a text file :

a) First: I have a text file (it's a log), this log has many lines: 2221. I just want to print from line 2211 to 2220. How can I do this?

I have this code:

line_number=2011
with open('file.log') as f:
        i = 2011
        for line in f:
            if i == line_number:
                break
            i += 1
            print (line)

but print all the file

b) Second: Well, the lines 2211 to 2220 are this:

Dominio1.BL00010001.pdb 24.69530

Dominio1.BL00020001.pdb 14.33748

Dominio1.BL00030001.pdb 30.53454

Dominio1.BL00040001.pdb 23.82516

Dominio1.BL00050001.pdb 27.48684

Dominio1.BL00060001.pdb 18.17364

Dominio1.BL00070001.pdb 30.98407

Dominio1.BL00080001.pdb 17.19927

Dominio1.BL00090001.pdb 19.02460

Dominio1.BL00100001.pdb 22.57086

I want to create a code that selects the number line that has the smallest number (identify),and read the name of the .pdb (just the 24 characters of the line that has the smallest number).Cause, I need identify what's the .pdb that has the smallest number, and use it like a string in other script, like this:

model= '%s'%R

where '%s'%R is the name of .pdb that i need

How can I do it?

Your code merely breaks when you reach the line of interest, but you have no condition associated with the print, so it prints every line it encounters. If you change your code to something like:

start = 2011
end = 2220

with open('file.log') as f:
    for line_number, line in enumerate(f):
        if line_number > end:
            break
        if line_number > start:
            print line

And you can treat the filehandle as a list and slice it:

with open('file.log') as f:
    print "".join(list(f)[2011:2220])

A:

with open('file.log') as f:
    print f.read().split('\n')[2211:2220+1] 

First of all create a list of all the lines in the text file (Lines are seperated by a new line character("\\n"), then slice the list, easy as that.

Edit: Alternatively you could use the bulit-in function "readlines" If you don't mind the '\\n' at the end:

with open('file.log') as f:
    print f.readlines()[2211:2220+1]

B:

def s(item):
    return item[num_of_spaces:]
num_of_spaces = len("Dominio1.BL00010001.pdb         ")
with open('file.log') as f:
    lines = f.read().split('\n')[2211:2221]
print sorted(lines, key=s)[0]

This should work

with open('file.log') as f:
        rd=f.readlines()
        print (rd[2211:2221])

readlines() returns a list, so just slice the list with indices. Indices starting from 0 and the last number doesn't count, so you have to write 2220+1.

The problem with your snippet is you are always printing the line you read until you get to the desired line, then you break the loop! Try this instead

line_number=2011
with open('file.log') as opened_file:
     for i, line in enumerate(opened_file):
         # Only print it if you got to the desired line or upper
         if i >= line_number:
             print(line)

However, there are better approaches to this problem, specially if you're dealing with large size files. Take a look at this question .

If you want to take the name I suggest splitting the line, you may write:

columns = line.split()
print('File name is', columns[0])

So you get a list like this for each line ['Dominio1.BL00010001.pdb', '24.69530'] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM