代码之家  ›  专栏  ›  技术社区  ›  Stefano Borini

如何在python中获取当前打开的文件行?

  •  7
  • Stefano Borini  · 技术社区  · 16 年前

    假设您打开一个文件,然后在文件中的某个位置执行seek(),您如何知道当前的文件行?

    (我个人用一个特殊的文件类解决了这个问题,该类在扫描文件后将搜索位置映射到行,但我希望看到其他提示,并将这个问题添加到stackoverflow,因为我在Google上的任何地方都找不到这个问题)

    2 回复  |  直到 16 年前
        1
  •  4
  •   Mark Rushakoff    16 年前

    下面是我如何处理这个问题,尽可能多地使用懒惰:

    from random import randint
    from itertools import takewhile, islice
    
    file = "/etc/passwd"
    f = open(file, "r")
    
    f.seek(randint(10,250))
    pos = f.tell()
    
    print "pos=%d" % pos
    
    def countbytes(iterable):
        bytes = 0
        for item in iterable:
            bytes += len(item)
            yield bytes
    
    print 1+len(list(takewhile(lambda x: x <= pos, countbytes(open(file, "r")))))
    

    要获得可读性稍差但更懒惰的方法,请使用 enumerate dropwhile :

    from random import randint
    from itertools import islice, dropwhile
    
    file = "/etc/passwd"
    f = open(file, "r")
    
    f.seek(randint(10,250))
    pos = f.tell()
    
    print "pos=%d" % pos
    
    def countbytes(iterable):
        bytes = 0
        for item in iterable:
            bytes += len(item)
            yield bytes
    
    print list(
            islice(
                dropwhile(lambda x: x[1] <= pos, enumerate(countbytes(open(file, "r"))))
                , 1))[0][0]+1
    
        2
  •  6
  •   unutbu    16 年前

    使用seek()时,python将使用指针偏移量跳转到文件中所需的位置。但是为了知道当前的行号,您必须检查到该位置的每个字符。所以您也可以放弃seek()而选择read():

    替换

    f = open(filename, "r")
    f.seek(55)
    

    具有

    f = open(filename, "r")
    line=f.read(55).count('\n')+1
    print(line)
    

    也许您不希望使用f.read(num),因为如果num非常大,这可能需要大量的内存。在这种情况下,您可以使用这样的生成器:

    import itertools
    import operator
    line_number=reduce(operator.add,( f.read(1)=='\n' for _ in itertools.repeat(None,num)))
    pos=f.tell()
    

    这相当于 f.seek(num) 给你额外的好处 line_number .

    推荐文章