转到Python中的特定行?

转到Python中的特定行?,python,Python,我想转到.txt文件中的第34行并读取它。在Python中您将如何做到这一点?您只需读取所有行,然后为后面的行编制索引即可 line = open('filename').readlines()[33] 有两种方法: 一行一行地读文件,到了你想要的那一行就停下来 使用f.readlines()将整个文件读入内存,并将其作为行列表返回,然后从该列表中提取第34项 解决方案1 好处:你只在内存中保留你想要的特定行 代码: 解决方案2 优点:代码少得多 缺点:将整个文件读入内存 问题:如果列表中的元

我想转到.txt文件中的第34行并读取它。在Python中您将如何做到这一点?

您只需读取所有行,然后为后面的行编制索引即可

line = open('filename').readlines()[33]
有两种方法:

  • 一行一行地读文件,到了你想要的那一行就停下来
  • 使用
    f.readlines()
    将整个文件读入内存,并将其作为行列表返回,然后从该列表中提取第34项
  • 解决方案1 好处:你只在内存中保留你想要的特定行

    代码:

    解决方案2 优点:代码少得多
    缺点:将整个文件读入内存
    问题:如果列表中的元素少于34个,将崩溃,需要错误处理

    line = f.readlines()[33]
    

    使用Python标准库的模块:


    你应该做你想做的事。您甚至不需要打开文件--
    linecache
    为您完成所有操作

    一种不会读取超过必要数量的文件的解决方案是

    for linenum,line in enumerate(open("file")):
        if linenum+1==34: print line.rstrip()
    
    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))
    
    一个非常简单的解决方案是

    line_number = 34
    
    with open(filename) as f:
        f.readlines()[line_number - 1]
    

    此代码将打开文件,读取行并打印它

    # 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)
    

    我就此事发了一条线,并没有得到任何帮助,所以我自己处理了这件事

    这里没有任何复杂的代码

    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.
    
    如果要在python中完成此操作,请确保文件(.py)和(.txt)位于同一位置,否则python将无法检索此文件,除非指定文件位置。例如

    linecache.getline(“C:/Directory/Folder/Quotes.txt

    当文件位于您正在使用的.py文件之外的其他文件夹中时,将使用此选项


    希望这有帮助!

    请注意,如果您遵循打开文件的众多解决方案之一,您希望使用
    with
    块(或
    尝试
    /
    最后在2.5之前的版本中使用
    )以确保文件被关闭。许多解决方案都可能引发异常,例如,如果您的文件意外短于34行。python:如何跳转到大型文本文件中的特定行?您希望第34行为
    [33]
    。+1表示优雅的解决方案,而我们(或至少我)学到了一些新的东西。不错,但是如果其他行需要其他处理,仍然需要打开文件。@ghostdog,如果“处理”"在特定编号的行上,只需继续使用
    linecache
    ——顾名思义,它为您缓存东西。一些行按编号访问,而其他行需要循环的奇怪混合可以以不同的方式处理,但这种奇怪的混合几乎不是常见的用例。+1您是天才Alex。我至少了解到
    2
    每天从你那里得到新的东西:D@MachineCharmer嘿,阿谀奉承会把你带到任何地方;-)。我不明白。有多少行?如果OP想停在34号线,就休息一下。这仍然比将整个文件读入内存要好。
    # 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)
    
    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.