This repository contains a simple Python script that demonstrates how to read client data from a CSV file, process it, and generate JSON output.
Learning simple data formatting in Python from CSV file into JSON file.
This script:
- Reads a CSV file containing client information,
- Calculates additional fields (e.g., applying VAT),
- Builds a JSON structure for each client,
- Prints the JSON output to the console (for testing and debugging).
β
Reads CSV input line by line
β
Processes data and calculates a new field (amount with VAT)
β
Outputs JSON objects (one per client)
- Python 3.8+
- No external dependencies (only builtβin libraries:
csvandjson)
The script expects a clients.csv file in the same directory, with at least the following columns:
| Column name | Description | Example |
|---|---|---|
name |
Clientβs full name | John Doe |
channel |
Preferred channel (email or paper) |
email |
amount_ht |
Base amount (without VAT) | 250.75 |
Example clients.csv:
name,channel,amount_ht
John Doe,email,250.75
Jane Smith,paper,120.00
The script will output a JSON object per line in the console:
{"clientName": "John Doe", "preferredChannel": "email", "amount": 300.9}
{"clientName": "Jane Smith", "preferredChannel": "paper", "amount": 144.0}If you want to save the JSON output to a file, you can delete or comment the print(...) line and uncomment the following lines:
#results = []#results.append(output)#with open('output.json', 'w', encoding='utf-8') as json_file:
# json.dump(results, json_file, ensure_ascii=False, indent=4)MIT License