我如何跳过标题行并开始从第2行读取文件?


当前回答

f = open(fname,'r')
lines = f.readlines()[1:]
f.close()

其他回答

f = open(fname,'r')
lines = f.readlines()[1:]
f.close()

如果切片可以在迭代器上工作……

from itertools import islice
with open(fname) as f:
    for line in islice(f, 1, None):
        pass
f = open(fname).readlines()
firstLine = f.pop(0) #removes the first line
for line in f:
    ...

为了概括读取多个标题行的任务并提高可读性,我将使用方法提取。假设您想要标记coordinate .txt的前三行作为标题信息。

例子

coordinates.txt
---------------
Name,Longitude,Latitude,Elevation, Comments
String, Decimal Deg., Decimal Deg., Meters, String
Euler's Town,7.58857,47.559537,0, "Blah"
Faneuil Hall,-71.054773,42.360217,0
Yellowstone National Park,-110.588455,44.427963,0

然后,方法提取允许您指定想要对标题信息做什么(在本例中,我们只是基于逗号对标题行进行标记,并将其作为列表返回,但还有空间可以做更多的事情)。

def __readheader(filehandle, numberheaderlines=1):
    """Reads the specified number of lines and returns the comma-delimited 
    strings on each line as a list"""
    for _ in range(numberheaderlines):
        yield map(str.strip, filehandle.readline().strip().split(','))

with open('coordinates.txt', 'r') as rh:
    # Single header line
    #print next(__readheader(rh))

    # Multiple header lines
    for headerline in __readheader(rh, numberheaderlines=2):
        print headerline  # Or do other stuff with headerline tokens

输出

['Name', 'Longitude', 'Latitude', 'Elevation', 'Comments']
['String', 'Decimal Deg.', 'Decimal Deg.', 'Meters', 'String']

如果coordinates.txt包含另一个标题行,只需更改numberheaderlines。最重要的是,__readheader(rh, numberheaderlines=2)所做的事情很清楚,我们避免了必须弄清楚或评论为什么接受答案的作者在他的代码中使用next()的歧义。

如果你想要第一行,然后你想对文件执行一些操作,这段代码将会很有帮助。

with open(filename , 'r') as f:
    first_line = f.readline()
    for line in f:
            # Perform some operations