---
title: "How do I read and write CSV and JSON files in Python?"  
description: "How do I read and write CSV and JSON files in Python?"  
author: "Arnaud Simon"  
published: 2026-11-15  
updated: 2026-11-15  
canonical: https://www.mindstick.com/forum/163035/how-do-i-read-and-write-csv-and-json-files-in-python  
category: "python"  
tags: ["python", "CSV", "json", "file handling", "data processing"]  
reading_time: 1 minute  

---

# How do I read and write CSV and JSON files in Python?

I need to read a CSV of customers and save selected rows as JSON. What is the clean way?

## Replies

### Reply by Agnieszka Wozniak

Use the standard library.

Reading CSV into dictionaries:

```
import csv, json

with open("customers.csv", newline="", encoding="utf-8") as f:
    rows = list(csv.DictReader(f))

vip = [r for r in rows if r["Plan"] == "VIP"]

with open("vip.json", "w", encoding="utf-8") as f:
    json.dump(vip, f, indent=2, ensure_ascii=False)
```

Notes:

- Always pass `newline=""` for csv, and set the encoding explicitly (use "utf-8-sig" if the CSV comes from Excel with a BOM).
- Every CSV value is a string. Convert numbers yourself.
- `ensure_ascii=False` keeps non-English text (such as Hindi) readable in the JSON file.
- For big or messy data, `pandas.read_csv` and `df.to_json` are more convenient.


---

Original Source: https://www.mindstick.com/forum/163035/how-do-i-read-and-write-csv-and-json-files-in-python

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
