merhaba,
json dosyasında geçen her urune ait 11 elementi çekmek için bu kodu yazdim
import json
with open("18july.json", encoding="UTF-8") as f:
jsondata = json.load(f)
satici = jsondata["Value"][0]["CN"]
marka = jsondata["Value"][0]["L"]
website = jsondata["Value"][0]["S"]
ulke = jsondata["Value"][0]["O1"]
sehir = jsondata["Value"][0]["O2"]
fiyat1 = jsondata["Value"][0]["E"][0]["C"]
fiyat2 = jsondata["Value"][0]["E"][1]["C"]
fiyat3 = jsondata["Value"][0]["E"][2]["C"]
fiyat4 = jsondata["Value"][0]["E"][8]["C"]
fiyat5 = jsondata["Value"][0]["E"][8]["P"]
fiyat6 = jsondata["Value"][0]["E"][9]["C"]
for urun in jsondata["Value"]:
satici = urun["CN"]
marka = urun["L"]
website = urun["S"]
ulke = urun["O1"]
sehir = urun["O2"]
fiyat1 = urun["E"][0]["C"]
fiyat2 = urun["E"][1]["C"]
fiyat3 = urun["E"][2]["C"]
fiyat4 = urun["E"][8]["C"]
fiyat5 = urun["E"][8]["P"]
fiyat6 = urun["E"][9]["C"]
print(satici, marka, website, ulke, sehir, fiyat1, fiyat2, fiyat3, fiyat4, fiyat5, fiyat6)
json dosyasi icindeki dizinlerde her zaman satici, marka, website bilgisi var.
ancak geri kalan ulke, sehir, fiyat1, ....... , fiyat6 bilgileri her urun için bulunmayabiliyor ve kod bu hatayi veriyor.
Traceback (most recent call last):
File "c:\Users\monst\Downloads\python_files\urunler.py", line 30, in <module>
fiyat6 = urun["E"][9]["C"]
IndexError: list index out of range
dizinde bulamadigi element olduğu zaman output ekraninda "None" şeklinde belirtip diğeriine geçmesini ve kodun çalışmaya devam etmesini istiyorum.
bu loop döngüsü kodunu nasıl düzeltebilirm?
tşk ederim.