-4

I was trying to write a code only to output the days and months from a log file. But there is a header in the first line. How can I get rid of it?

with open('running.log','r') as file:
    lines = file.readlines()
    data = []
    for line in lines:
        
        fields = line[:-1].split('\t')
        
        date = fields[0].split('-')
        time = fields[1]
        temp = fields[2]
        record = (date, time, temp)
        
        
        data.append(record)
        
        days = date[0]
        months = date[-1]
        print(days)

my output results are:


DATE
7
8
9
10
12
13
14
15
16
17
18
20
22
24
25
26
27
28

So how can I get rid of the DATE in the first line?

Heikki
  • 2,214
  • 19
  • 34
  • 2
    Or in your case that you have `lines = file.readlines()`, why not just do `for line in lines[1:]:`? – Tomerikoo Nov 01 '20 at 14:17
  • 2
    Actually looking better at your code, this is basically an XY problem - what you really should be doing is use the [`csv`](https://docs.python.org/3/library/csv.html) or [`pandas`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html) modules, and not read a tabulated file as a regular text file – Tomerikoo Nov 01 '20 at 14:21

1 Answers1

0

Just skip the first line. Replace

lines = file.readlines()

by

lines = file.readlines()[1:]

Note, that readlines(), read the whole file into memory. This might cause a problem if your file is rather big. A way around this is by reading the file line by line. E.g.

with open('running.log','r') as file:
    next(file)
    for line in file:
        # do your stuff
Stefan
  • 1,697
  • 15
  • 31