0

I'm trying to read from file. Everything works, but I got the key error in spite of the fact that they key exist in debugger. I saw very similiar codes working but I don't have any idea why I got KeyError when I reach "id_samolotu". My code:

import csv
from lot import DatabaseofLoty, Lot

def read_from_csv(path):
    loty = []
    with open(path,"r") as file_handle:
        reader = csv.DictReader(file_handle)
        for row in reader:
            numer_lotu = row["numer_lotu"]
            id_samolotu = row["id_samolotu"]
            czas_lotu = row['czas_lotu']
            trasa = row['trasa']
            wolne_miejscaek = row['wolne_miejscaek']
            wolne_miejscabiz = row['wolne_miejscabiz']
            wolne_miejscapr = row['wolne_miejscapr']
            bramka = row['bramka']
            lot = Lot(numer_lotu, id_samolotu, czas_lotu, trasa, wolne_miejscaek,
                      wolne_miejscabiz, wolne_miejscapr, bramka)
            loty.append(lot)
            database = DatabaseofLoty(loty)

read_from_csv("loty.txt")

loty.txt file:

numer_lotu, id_samolotu, czas_lotu, trasa, wolne_miejscaek, wolne_miejscabiz, wolne_miejscpr, bramka
1, 3, 3:52, Amsterdam-Berlin, 129, 92, 192, 8
martineau
  • 119,623
  • 25
  • 170
  • 301
Paul
  • 1
  • 3
  • It looks like the delimiters of your CSV file have and extra space following them, which is non-standard and would become part of the keys and values in the dictionary `DictReader` returns. You can verify this by printing the value of `row` in the `for` loop. – martineau Dec 28 '21 at 14:52
  • Thank you. I was thinking that the problem is much more complicated. Thank you :) – Paul Dec 28 '21 at 15:00

0 Answers0