هەلگرتنا داتایێ ب JSON
JSON شێوەیەکە بۆ گوهۆڕینا داتایێ د ناڤبەرا بەرنامان دا.
د ڤێ لاپەڕێ دا
بیرۆکا وانەیێ
loads دەقێ دکەتە بهایێ پایتۆنێ؛ dumps بهایان دکەتە دەق.
نموونەیا کۆدی
import json
from pathlib import Path
from tempfile import TemporaryDirectory
student = {"name": "Sara", "scores": [80, 95], "active": True}
text = json.dumps(student, ensure_ascii=False, indent=2)
print(text)
restored = json.loads(text)
print(restored["name"])
with TemporaryDirectory(prefix="python_json_") as temporary:
path = Path(temporary) / "student.json"
with path.open("w", encoding="utf-8") as file:
json.dump(student, file, ensure_ascii=False, indent=2)
with path.open(encoding="utf-8") as file:
loaded = json.load(file)
print(loaded == student)ئەنجامی بشوپینە
داتا ب ناڤێ Sara و نمرەیان ب شێوەیێ JSON دهێتە نڤیسین. پشتی خوێندنەڤە، ب کلیلێ scores دگەهیە لیستا نمرەیان.
{
"name": "Sara",
"scores": [
80,
95
],
"active": true
}
Sara
Trueهەڵەیەکا بەربەلاڤ
loads بۆ نڤیسینا JSON یە. بۆ فایلەکا ڤەکری load بکار بینە.