Python/Text & files 8 min

JSON and CSV

Parse the two formats data arrives in.

jsoncsvparsing

json.loads turns a JSON string into Python objects and json.dumps goes the other way. Nested structures become nested dicts and lists.

python · editable
loading editor…

Try it: Add a 'score' key set to 10 and print the JSON again.

csv.DictReader reads rows as dictionaries keyed by the header. Here we read from a string so the example runs in the browser; in a script you would pass an open file instead.

python · editable
loading editor…

Always convert CSV values explicitly — everything arrives as a string, and silently summing strings is a classic bug.