简体   繁体   English

Python-从文本文件中获取行,行xy

[英]Python - grab lines from text file, lines x-y

The way to grab the first 10 lines of a text file could be done this way: 可以通过以下方式完成获取文本文件的前10行的方法:

with open("myfile.txt") as myfile:
        lines = [next(myfile).strip() for x in xrange(10)]

This will print the first 10 lines 这将打印前10行

But what if I only wanted lines 5-10? 但是,如果我只想要5-10行怎么办? How would I do that? 我该怎么做? The file isn't small, it's going to be big. 文件不小,它将很大。 I need an efficient way to do this. 我需要一种有效的方法来做到这一点。

Thank you. 谢谢。

By far the easiest way to do this is with itertools.islice : 到目前为止,最简单的方法是使用itertools.islice

with open('myfile.text') as f:
    lines = islice(f, 5, 11)

The nice part about this is that it won't ever load the entire file; 这样做的好处是,它永远不会加载整个文件。 it'll do only as much work as it needs to. 它只会完成所需的工作。

Note that the functions in itertools are lazy, so you need to either finish working with the lines before you close the file, or wrap the islice in a list() to force evaluation. 请注意, itertools中的函数是惰性的,因此您需要关闭文件之前完成使用行的操作,或者将islice包装在list()以强制求值。

How about slicing the list that you already have. 如何切片您已经拥有的列表。

with open("myfile.txt") as myfile:
    lines = [next(myfile).strip() for x in xrange(10)][5:10]

If you know where you want to start just change the range in the for loop for example. 如果您知道要从哪里开始,则只需更改例如for循环中的范围。

var1 = 5
var2 = 10
for x in range(var1, var2):

would loop from 5 to 10, of course you can tweak your variables further. 会从5循环到10,当然您可以进一步调整变量。

I'd do something like this: 我会做这样的事情:

>>> with open("myfile.txt") as myfile:
>>>  lines = myfile.read().split('\n')[5:10]

Split will separate it line by line into an array of lines, and the [5:10] will return array elements from 5 to 10, the variable "lines" will be an array, however, so to just get a string of lines 5 to 10, you could: Split将把它一行一行地分隔成一个行数组,[5:10]将返回从5到10的数组元素,但是变量“ lines”将是一个数组,因此,只得到一行字符串5到10,您可以:

>>> with open("myfile.txt") as myfile:
>>>  lines = '\n'.join(myfile.read().split('\n')[5:10])

The only difference here, is the '\\n'.join() part, which just takes each element of the array of lines, and joins them into a string seperated by a newline. 这里唯一的区别是'\\ n'.join()部分,该部分仅获取行数组的每个元素,并将它们连接到由换行符分隔的字符串中。

Reads the file and takes lines x to y as a list to content variable: 读取文件并将第x到y行作为内容变量的列表:

with open('content.txt') as f:
    content=f.read().split('\n')[x-1:y]

Take care of '\\n' and '\\r\\n'. 照顾'\\ n'和'\\ r \\ n'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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