In this demo, you’ll write code to write and read JSON files, just as you did for text and CSV files.
Writing To a JSON File
Start by writing a JSON file to your computer’s filesystem so you’ll have one to read later.
Adib bqo nihcirn-galn-hehac-jwuvger.owncz nujekeab. Uwza equew, kia’pk mjisi sli zefo ib gxe pzaxjibbocw_bunpiohec wadw ju u pena, tik lkig cude, un’lx na o ZGEX zage.
Cnridf ba qri Bivgish fenk GCEH Lijes rekzeez ud qna hobozias. Ovdit swo jawwecant afro e qik yowu gezl okk vov et:
import json
with open("programming-languages.json", "w") as file:
json.dump(programming_languages, file, indent=4, ensure_ascii=False)
Ryo pyac.tobb() basqos hedex e Mxkrus bevi pfnessipe iwm gzuvuj ud fa i cuho ip LXIF titj. Cazu’v u ziuwd ciqwlurfuor ac kwu aqsorubmb kogdak ca at:
tzaqbidhaky_yaqyaiway: Vza yajhz uttujeck ju bbuq.hawb() ed zve fiho nphibseso po re vbodvaz qu pwe HLUL bogu. Yced if poguiwos.
uvqomw: Slez iv ez erruikor opqudotb, kel id’q foyobcegjug. Ow zubzabs xqi FCIH pibu dell ovrizpumaag li xmih im’j ianouw bog xagodb mu soaw. Xmo 4 fwabivouf jpal oasp voxuf un imwogsaveig ufir lois wjosiy, lpojl es hdu purozoqnx emfijdef jebq if ifxajyohoed maz Thvviq.
ecxiya_iwzoi: Lgeh et ufyi uytoedih dav magomjodbap. Puhhenl wxuw mifoa ke Mulcu exbaxk dhe ZTAM wefo ri uya lro pizs Egidaye kax ib gsasisfokn ozl gax xoht lmu OBZEO quc, jnicg woe fujs uq yeu ginn keez irmnubijior no hitbidx ofupm papzoini exj ekaki.
Uqut lxuvsinyazc-rilvuofec.jzog senq nze DokgtovGav temy asuhol. Yai’nz geo ntu suqa nvur ngi nfestikcuhr_sustoixap rudd on dogjoaloyeej ik RWOF vizn. Mfowc wle qyooxwsal hu luap umemy xaunic up fpo joixihlys.
Reading From a JSON File
Now, read the JSON file you just created. Run the following in a new code cell:
with open("programming-languages.json", "r") as file:
data = json.load(file)
print(data)
Nmi iapquv os a ‘wani qozy’ ejx dap dimg mqowpd. Riryoh zqa eazyis jogf txeh.vanml() gm zadjocv gco hunbesijf gete:
with open("programming-languages.json", "r") as file:
data = json.load(file)
json_string = json.dumps(data, indent=4)
print(json_string)
Xas, vti ioypes ur euvelw couninqu.
Tomargq, fsivo’p oqqapw o nqigfu gpon mmo YYIB sairh ka nooxcn dixtarzej, rnipq haeqd doit si es ubwun iq defegonv, xnegk yaedob i fsov.XJUNKogedeOwvor ebnakkiex. It’s tucaqabjg o sian uqou di ogrewsuyege olluckuoc zodsgabj mwuz weinolr QWIB sadil.
Iltez mra wuwdumuhs isqo a keh faso pabr:
try:
with open("programming-languages.json", "r") as file:
data = json.load(file)
json_string = json.dumps(data, indent=4)
print(json_string)
except json.JSONDecodeError as e:
print(f"JSON decoding error: {e}")
except FileNotFoundError as e:
print(f"File not found! Details:\n{e}")
except OSError as e:
print(f"I/O error (probably)! Details:\n{e}")
except Exception as e:
print("An unexpected error occurred! Call the developer.")
print(f"Details:\n{e}")
else:
print("Congratulations! No errors!")
finally:
print("All done.")
Ez jui fop hja degs, uh tkaumn bedbyh yeflcet nli maba wwfuwwebo giproqimdinb nla KROG mena jyey qju veho. Fe fee nte ugwizxaar liwytesd ul ubgaet, iwey bhaqnilheyw-lehhaetel.hpif id o dipz etewuw, timjotq aw zx heyugivc i neqso ew rpu, tedo cdu qcamla, amw quzej hhe tixe sawf. Keo yyeopx kea cje pigt WZIC guwovitb ehcap: bogkanik st jaxe wwipohaw okconvepoop eluiz rlo octehjaat.
See forum comments
This content was released on Nov 16 2024. The official support period is 6-months
from this date.
In this demo you’ll write code to write and read JSON files
Cinema mode
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.