pythonheadersource-separation

Separating headers when importing a txt-file in python


As the title states I'm having some troubles separating headers from a txt-file that has the following structure:

...
02-10-29    133.50
02-10-30    140.00
02-10-31    139.00
02-11-01    134.50
AstraZeneca
02-08-01    339.50
02-08-02    360.50
02-08-05    353.00
02-08-06    333.50
...

Now the header in this case is "AstraZeneca". The code I have been using is:

with open("kurser.txt") as kurser:
namnrad = kurser.readline()
kurslista_ericsson = []
kurslista_electrolux = []
radnr = 0
for rad in kurser:
    if 36 < radnr < 67:
        info = rad.strip().split("\t")
        kurs = info[1]
        kurslista_ericsson.append(kurs)
    if radnr == 67:                  #<--- This is my attempted solution
        namnrad_2 = kurser.readline
    if 67 < radnr < 135:
        info_2 = rad.strip().split("\t")
        kurs_2 =info_2[1]
        kurslista_electrolux.append(kurs_2)
    radnr = radnr + 1

print(namnrad)
print(namnrad_2)

The problem is, when I try and print the first header "namnrad" everything works as it should, but when I attempt to print the second header "namnrad_2" I get "built-in method readline of file object at 0x109c38270". What should I do to solve this problem? Thank you in advance!


Solution

  • You are missing the parentheses here: namnrad_2 = kurser.readline()