私はエラーが発生します:キーエラー: '1'なぜか分かりませんか?
Traceback (most recent call last):
File "C:\Users\RoszkowskiM\Desktop\win5.py", line 120, in <module>
busses_in_year = mydict[Year]
KeyError: '1'
私はそれが私の辞書とは何かを持っていると仮定しています。このコードの目的は、Year
とdata_location
と一致させることで、3セットのデータを出力します。データはcsvシートから取得されています。 Excel Sheet
data = list(csv.reader(open(LOAD_GEN_DATAFILE)))
mydict = {}
for row in data:
Year, busnum, busname, scaled_power, tla, data_location, empty, year_link, from_, to, digit, name2, tla_2, min_value, max_value = row[0:15]
busses_in_year = mydict[Year]
#Add the bus to the list of busses that stop at this location
busses_in_year[data_location].append((busnum,busname,scaled_power))
if Year in mydict and data_location in mydict[Year]:
busses_in_year = mydict[Year]
print("Here are all the busses at that location for that year and the new LOAD TOTAL: ")
print("\n")
output='Bus #: {}\t Area Station: {}\t New Load Total: {} MW\t'
print(output.format(busnum,busname,scaled_power))
else:
exit
'mydict'が空なので、どこでやりますこの構造を埋める? – Daniel
あなたは 'mydict [Year]'にアクセスしようとしていますが、 'mydict'は空ですので、そのようなキーが辞書に存在しないことを伝えています。 –
私は、添付されているイメージがあなたがロードしているCSVファイルだとします。しかし、内容は 'mydict'ではなく' data'にあります。 – Daniel