如何在 python 中跳过文件的第一行

how to skip the first line of a file in python

我需要编写一个代码来读取一个.txt文件,它是一个如下所示的矩阵,并将其转换为一个新的整数列表矩阵。但是,我想跳过此 .txt 文件的第一行而不手动删除该文件。我不知道如何去做。 我写了一些代码。它能够显示矩阵,但我无法摆脱第一行:

def display_matrix(a_matrix):
    for row in a_matrix:
        print(row)
    return a_matrix

def numerical_form_of(a_list):
    return [int(a_list[i]) for i in range(len(a_list))]

def get_scoring_matrix():
    scoring_file = open("Scoring Matrix")
    row_num = 0
    while row_num <= NUMBER_OF_FRAGMENTS:
        content_of_line = scoring_file.readline()
        content_list = content_of_line.split('   ')
        numerical_form = numerical_form_of(content_list[1:])
        scoring_matrix = []
        scoring_matrix.append(numerical_form)
        row_num += 1
        #print(scoring_matrix)
        display_matrix(scoring_matrix)
    # (Complement): row_num = NUMBER_OF_FRAGMENTS
    return scoring_matrix

get_scoring_matrix()

Scoring Matrix is a .txt file: 
    1   2   3   4   5   6   7
1   0   1   1   1   1   1   1
2   0   0   1   1   1   1   1
3   0   0   0   1   1   1   1
4   0   0   0   0   1   1   1
5   0   0   0   0   0   1   1
6   0   0   0   0   0   0   1
7   0   0   0   0   0   0   0

The result of my code: 
[1, 2, 3, 4, 5, 6, 7]
[0, 1, 1, 1, 1, 1, 1]
[0, 0, 1, 1, 1, 1, 1]
[0, 0, 0, 1, 1, 1, 1]
[0, 0, 0, 0, 1, 1, 1]
[0, 0, 0, 0, 0, 1, 1]
[0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0]

只需在 while 循环之前放置一个 scoring_file.readline()。

我建议使用自动化工具:

import pandas
df = pandas.read_table("Scoring Matrix", delim_whitespace = True)

如果坚持自己做,就改while循环;

while row_num <= NUMBER_OF_FRAGMENTS:
        content_of_line = scoring_file.readline()
        if row_num == 0:
            content_of_line = scoring_file.readline()