Eric
Eric

Reputation: 29

Python Ignore lines in file that start with

I am new to python. Within my python script I open a file for reading, and want to process certain lines. I want to ignore lines that start with either '*' or '#'. Here is my code:

line = DLF_P.readline()

while line:

if not line.startswith('*') or not line.startswith('#'):

    time = line.split()[0]
    print time
    time = datetime.strptime(time, FMT)

    if start < bdoyend:
        print time

line = DLF_P.readline()

I get the error that the first line (which contains an asterisk) does not match the format 'HH:MM:SS". I thought my code would ignore these types of lines.

ValueError: time data '**' does not match format '%H:%M:%S'

Am I doing this wrong?

Upvotes: 2

Views: 6252

Answers (2)

Debanik Dawn
Debanik Dawn

Reputation: 799

Since you are checking if the line doesn't start with EITHER * or #, you need to do this:

if not line.startswith('*') and not line.startswith('#')

Upvotes: 0

damienfrancois
damienfrancois

Reputation: 59310

The line

if not line.startswith('*') or not line.startswith('#'):

should be

if not line.startswith('*') and not line.startswith('#'):

or

if not (line.startswith('*') or line.startswith('#')):

if you want to ignore both lines starting with * and those starting with #

Upvotes: 3

Related Questions