仅读取文件的第一行?

问题:仅读取文件的第一行?

如何使用Python仅将文件的第一行作为字符串?

How would you get only the first line of a file as a string with Python?


回答 0

使用.readline()方法(Python 2文档Python 3文档):

with open('myfile.txt') as f:
    first_line = f.readline()

一些注意事项:

  1. 如文档中所述,除非它是文件中的唯一一行,否则从中返回的字符串f.readline()将包含尾随换行符。您可能希望f.readline().strip()改用删除换行符。
  2. with语句在块结束时自动再次关闭文件。
  3. with语句仅在Python 2.5及更高版本中有效,而在Python 2.5中,您需要使用from __future__ import with_statement
  4. 在Python 3中,您应该为打开的文件指定文件编码。阅读更多…

Use the .readline() method (Python 2 docs, Python 3 docs):

with open('myfile.txt') as f:
    first_line = f.readline()

Some notes:

  1. As noted in the docs, unless it is the only line in the file, the string returned from f.readline() will contain a trailing newline. You may wish to use f.readline().strip() instead to remove the newline.
  2. The with statement automatically closes the file again when the block ends.
  3. The with statement only works in Python 2.5 and up, and in Python 2.5 you need to use from __future__ import with_statement
  4. In Python 3 you should specify the file encoding for the file you open. Read more…

回答 1

infile = open('filename.txt', 'r')
firstLine = infile.readline()
infile = open('filename.txt', 'r')
firstLine = infile.readline()

回答 2

fline=open("myfile").readline().rstrip()
fline=open("myfile").readline().rstrip()

回答 3

应该这样做:

f = open('myfile.txt')
first = f.readline()

This should do it:

f = open('myfile.txt')
first = f.readline()

回答 4

要返回打开文件的开头,然后返回第一行,请执行以下操作:

my_file.seek(0)
first_line = my_file.readline()

To go back to the beginning of an open file and then return the first line, do this:

my_file.seek(0)
first_line = my_file.readline()

回答 5

first_line = next(open(filename))
first_line = next(open(filename))

回答 6

这里还有很多其他答案,但是要精确回答您所提出的问题(在@MarkAmery去编辑原始问题并更改含义之前):

>>> f = open('myfile.txt')
>>> data = f.read()
>>> # I'm assuming you had the above before asking the question
>>> first_line = data.split('\n', 1)[0]

换句话说,如果您已经读入文件(如您所说),并且在内存中有一大块数据,那么要有效地从中获取第一行,请对换行符执行一次split()仅,并从结果列表中获取第一个元素。

请注意,这不包括该\n行末的字符,但是我假设您还是不希望使用该字符(单行文件甚至可能没有该字符)。还要注意,尽管它很短而且很快,但是它确实可以复制数据,因此对于很大的内存块,您可能不会认为它“有效”。和往常一样,这取决于…

Lots of other answers here, but to answer precisely the question you asked (before @MarkAmery went and edited the original question and changed the meaning):

>>> f = open('myfile.txt')
>>> data = f.read()
>>> # I'm assuming you had the above before asking the question
>>> first_line = data.split('\n', 1)[0]

In other words, if you’ve already read in the file (as you said), and have a big block of data in memory, then to get the first line from it efficiently, do a split() on the newline character, once only, and take the first element from the resulting list.

Note that this does not include the \n character at the end of the line, but I’m assuming you don’t want it anyway (and a single-line file may not even have one). Also note that although it’s pretty short and quick, it does make a copy of the data, so for a really large blob of memory you may not consider it “efficient”. As always, it depends…


回答 7

f1 = open("input1.txt", "r")
print(f1.readline())
f1 = open("input1.txt", "r")
print(f1.readline())