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.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Use the standard library.
Reading CSV into dictionaries:
Notes:
newline=""for csv, and set the encoding explicitly (use "utf-8-sig" if the CSV comes from Excel with a BOM).ensure_ascii=Falsekeeps non-English text (such as Hindi) readable in the JSON file.pandas.read_csvanddf.to_jsonare more convenient.