Reading specific lines only

后端 未结 28 1519
天命终不由人
天命终不由人 2020-11-22 05:08

I\'m using a for loop to read a file, but I only want to read specific lines, say line #26 and #30. Is there any built-in feature to achieve this?

Thanks

相关标签:
28条回答
  • 2020-11-22 05:31

    Some of these are lovely, but it can be done much more simply:

    start = 0 # some starting index
    end = 5000 # some ending index
    filename = 'test.txt' # some file we want to use
    
    with open(filename) as fh:
        data = fin.readlines()[start:end]
    
    print(data)
    

    That will use simply list slicing, it loads the whole file, but most systems will minimise memory usage appropriately, it's faster than most of the methods given above, and works on my 10G+ data files. Good luck!

    0 讨论(0)
  • 2020-11-22 05:32
    file = '/path/to/file_to_be_read.txt'
    with open(file) as f:
        print f.readlines()[26]
        print f.readlines()[30]
    

    Using the with statement, this opens the file, prints lines 26 and 30, then closes the file. Simple!

    0 讨论(0)
  • 2020-11-22 05:33

    File objects have a .readlines() method which will give you a list of the contents of the file, one line per list item. After that, you can just use normal list slicing techniques.

    http://docs.python.org/library/stdtypes.html#file.readlines

    0 讨论(0)
  • 2020-11-22 05:33

    To print desired line. To print line above/below required line.

    def dline(file,no,add_sub=0):
        tf=open(file)
        for sno,line in enumerate(tf):
            if sno==no-1+add_sub:
             print(line)
        tf.close()
    

    execute---->dline("D:\dummy.txt",6) i.e dline("file path", line_number, if you want upper line of the searched line give 1 for lower -1 this is optional default value will be taken 0)

    0 讨论(0)
  • 2020-11-22 05:34

    I prefer this approach because it's more general-purpose, i.e. you can use it on a file, on the result of f.readlines(), on a StringIO object, whatever:

    def read_specific_lines(file, lines_to_read):
       """file is any iterable; lines_to_read is an iterable containing int values"""
       lines = set(lines_to_read)
       last = max(lines)
       for n, line in enumerate(file):
          if n + 1 in lines:
              yield line
          if n + 1 > last:
              return
    
    >>> with open(r'c:\temp\words.txt') as f:
            [s for s in read_specific_lines(f, [1, 2, 3, 1000])]
    ['A\n', 'a\n', 'aa\n', 'accordant\n']
    
    0 讨论(0)
  • 2020-11-22 05:35

    Reading files is incredible fast. Reading a 100MB file takes less than 0.1 seconds (see my article Reading and Writing Files with Python). Hence you should read it completely and then work with the single lines.

    What most answer here do is not wrong, but bad style. Opening files should always be done with with as it makes sure that the file is closed again.

    So you should do it like this:

    with open("path/to/file.txt") as f:
        lines = f.readlines()
    print(lines[26])  # or whatever you want to do with this line
    print(lines[30])  # or whatever you want to do with this line
    

    Huge files

    If you happen to have a huge file and memory consumption is a concern, you can process it line by line:

    with open("path/to/file.txt") as f:
        for i, line in enumerate(f):
            pass  # process line i
    
    0 讨论(0)
提交回复
热议问题