נבנה על ידיBuilt by Ariel Yohimovich
true/false/null במקום True/False/Nonetrue/false/null instead of True/False/Nonetrue, false ו-null באותיות קטנות, ומחרוזות תמיד במירכאות כפולות. json.dumps הופך אובייקט פייתון למחרוזת JSON, ו-json.loads עושה את ההפך.JSON is the world's most common way to store and send structured data. It looks almost like Python dicts and lists, with three small differences: true, false and null in lowercase, and strings always in double quotes. json.dumps turns a Python object into a JSON string, and json.loads does the reverse.import json
print(json.loads("{'title': 'Dune'}"))json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1): ב-JSON רק מירכאות כפולות. אל תכתבו JSON ביד: תנו ל-json.dumps לייצר אותו.json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1): JSON allows only double quotes. Don't hand-write JSON: let json.dumps produce it.json.dump(data, f) כותב לקובץjson.dump(data, f) writes to a filejson.load(f) קורא מקובץjson.load(f) reads from a fileindent=2, ensure_ascii=False לקובץ קריא בעבריתindent=2, ensure_ascii=False for a readable file in Hebrewjson.dump כותבת ו-json.load קוראת. indent=2 מסדר את הקובץ בשורות ורווחים, ו-ensure_ascii=False שומר עברית כמו שהיא ולא כ-\u05d3. כך המדף נשמר כולו, עם כל המבנה, בשורה אחת של קוד.Without the s at the end, the functions work directly with a file: json.dump writes and json.load reads. indent=2 lays the file out in lines and spaces, and ensure_ascii=False keeps Hebrew as it is instead of \u05d3. That saves the whole shelf, structure and all, in one line of code.list(s), asdict(b), d.isoformat()Convert first: list(s), asdict(b), d.isoformat()isoformat(), ו-dataclass למילון עם asdict (מפגש 18). עוד הפתעה: מפתחות של מילון ב-JSON הם תמיד מחרוזות.JSON knows only the basic types. A set, a date or an object of your own class must be converted first: a set to a list, a date to a string with isoformat(), and a dataclass to a dict with asdict (session 18). One more surprise: dictionary keys in JSON are always strings.csv.DictReader → מילון לכל שורהcsv.DictReader → a dict per rowsplit(","), כי ערך יכול להכיל פסיק (בתוך מירכאות). המודול csv מטפל בזה. DictReader משתמש בשורת הכותרות ונותן מילון לכל שורה. שימו לב: כל הערכים מגיעים כמחרוזות.CSV is the spreadsheet format: each line a record, commas between values. Don't split it yourself with split(","), because a value can contain a comma (inside quotes). The csv module handles that. DictReader uses the header row and gives a dict per row. Note: every value arrives as a string.csv.DictWriter(f, fieldnames=[...])csv.DictWriter(f, fieldnames=[...])writeheader() ואז writerows(rows)writeheader() then writerows(rows)newline="" מונע שורות ריקות ב-Windowsnewline="" prevents blank lines on WindowsDictWriter כותב רשימת מילונים לטבלה. קובעים את סדר העמודות ב-fieldnames, כותבים כותרות, ואז את השורות. פותחים את הקובץ עם newline="", אחרת ב-Windows תופיע שורה ריקה בין כל שתי שורות. מכאן אפשר לפתוח את הקובץ באקסל.DictWriter writes a list of dicts as a table. Set the column order in fieldnames, write the headers, then the rows. Open the file with newline="", otherwise Windows gets a blank line between rows. You can then open the file in Excel.json.JSONDecodeError לקובץ שבורjson.JSONDecodeError for a broken filejson.JSONDecodeError עם הודעה ברורה. ובשמירה: כותבים קודם לקובץ זמני ורק אז מחליפים עם Path.replace, כך שקריסה באמצע לעולם לא תשאיר קובץ חצוי.Files break: someone edited by hand and forgot a comma, or the program stopped mid-write. Load inside try and catch json.JSONDecodeError with a clear message. When saving: write to a temporary file first and only then swap with Path.replace, so a crash midway never leaves a half-written file.true/false/null במקום True/False/Nonetrue/false/null instead of True/False/Nonejson.dump(data, f) כותב לקובץjson.dump(data, f) writes to a filejson.load(f) קורא מקובץjson.load(f) reads from a fileindent=2, ensure_ascii=False לקובץ קריא בעבריתindent=2, ensure_ascii=False for a readable file in Hebrewlist(s), asdict(b), d.isoformat()Convert first: list(s), asdict(b), d.isoformat()csv.DictReader → מילון לכל שורהcsv.DictReader → a dict per rowcsv.DictWriter(f, fieldnames=[...])csv.DictWriter(f, fieldnames=[...])writeheader() ואז writerows(rows)writeheader() then writerows(rows)newline="" מונע שורות ריקות ב-Windowsnewline="" prevents blank lines on Windowsjson.JSONDecodeError לקובץ שבורjson.JSONDecodeError for a broken filejson.dump(data, f) כותב לקובץjson.dump(data, f) writes to a filecsv.DictWriter(f, fieldnames=[...])csv.DictWriter(f, fieldnames=[...])json.JSONDecodeError לקובץ שבורjson.JSONDecodeError for a broken fileנבנה על ידיBuilt by Ariel Yohimovich