🟡 Advanced Python  ·  Lesson 25

JSON and CSV Handling

What is JSON and CSV Handling?

JSON and CSV Handling means jSON and CSV are common formats for storing and exchanging structured data.

In real programs, this topic helps in reading CSV files. Learn the idea first, then type the program yourself and compare the output.

💡 At a Glance
PointDetails
Course AreaAdvanced Python
Professional concepts used to make code reusable, clean and project-ready.
Main Usereading CSV files
Example Filejson-csv.py
Practice FocusRun, change values, and explain the output line by line.

Why should you learn this?

  • It is useful for reading CSV files.
  • It connects with working with API JSON.
  • It improves your ability to read, write and debug Python programs.

Important Terms

These terms are used directly in this lesson. Understand them before memorising the code.

TermMeaning
jsonText-based format commonly used in APIs and configuration files.
csvComma-separated format commonly used for spreadsheets.
dictionarydictionary is an important term in this topic.
readerTool used to read CSV rows.
writerTool used to write CSV rows.

Syntax / Basic Pattern

The simple pattern is: prepare data, apply the concept, then show the result.

Basic Pattern
import json
student = {"name": "Riya", "marks": 94}
json_text = json.dumps(student, indent=2)
print(json_text)
back_to_dict = json.loads(json_text)
print(back_to_dict["name"])

Complete Example Program

Python – json-csv.py
import json

student = {"name": "Riya", "marks": 94}
json_text = json.dumps(student, indent=2)
print(json_text)

back_to_dict = json.loads(json_text)
print(back_to_dict["name"])

Expected Output

{ "name": "Riya", "marks": 94 } Riya

Program Explanation

  • import json imports ready-made features from a module/library.
  • student = {"name": "Riya", "marks": 94} stores a value in student.
  • json_text = json.dumps(student, indent=2) stores a value in json_text.
  • print(json_text) displays information or calculated result on the screen.
  • back_to_dict = json.loads(json_text) stores a value in back_to_dict.
  • print(back_to_dict["name"]) displays information or calculated result on the screen.

Where will you use it?

  • Reading csv files.
  • Working with api json.
  • Exporting reports.

Common Mistakes

  • Making code complex when a simple function or class is enough.
  • Not handling possible errors or edge cases.
  • Mixing project dependencies instead of using a virtual environment.

Practice Tasks

  1. Type the program in json-csv.py and run it.
  2. Change input values or sample data and observe the new output.
  3. Create one example related to reading CSV files.
  4. Write 5 lines explaining the logic in your own words.

Summary

JSON and CSV Handling is not a theory-only topic. You should be able to explain the meaning, write the example, run it successfully, and use it in a small practical program.

← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.