Bob
Bob

Reputation: 41

Skip line in python

I have a text file that is laid out like this:

Text Text
Text Text Text Text Text
Text Num Num Num Num
Text Num Num Num Num
Text Num Num Num Num

My code is:

def readData():
    myList =[]
    myFile = open("football.txt", "r")
    for lines in myFile:
        league = lines.split()
        myList.append(league)
    return myList

How can I skip the first two row so I can store everything else into my list?

Upvotes: 1

Views: 21373

Answers (3)

Kalyssa J.
Kalyssa J.

Reputation: 21

To skip lines in Python 3...

print('\n')

Upvotes: 1

sameera sy
sameera sy

Reputation: 1718

You can also use the readlines() for this purpose

myFile = open("football.txt", "r")
lines = myFile.readlines()[2:] #To skip two lines.
#rest of the code

You could also specify the number of lines you want to skip.

Upvotes: 1

Sede
Sede

Reputation: 61293

You can use next()

def readData():
    with open("football.txt", "r") as MyFile:
        for i in range(2):
            next(myFile) # skip line
        myList = [lines.split() for lines in myFile]
    return myList

Upvotes: 5

Related Questions