{% extends "base.html" %} {% block title %}Data Flow & Encoding - Data Science Lab{% endblock %} {% block content %}
Data must be serialized (converted to bytes) for storage and transmission. The format choice impacts performance, readability, and interoperability.
| Format | Type | Pros | Cons | Use Case |
|---|---|---|---|---|
| CSV | Text | Human-readable, universal | No schema, slow for big data | open data portal downloads |
| JSON | Text | Flexible, API-friendly | Verbose, no native types | open data portal API responses |
| Parquet | Binary | Columnar, compressed, fast | Not human-readable | Large-scale analytics |
| Avro | Binary | Schema evolution, row-based | Complex setup | Kafka event streams |
| Protocol Buffers | Binary | Compact, fast, typed | Requires .proto files | gRPC microservices |
import pandas as pd
import json
# CSV: Human-readable flat files
df = pd.read_csv('data/housing_data.csv')
df.to_csv('output/data_clean.csv', index=False)
# JSON: API-compatible structured data
data = df.head(5).to_dict(orient='records')
with open('output/data_sample.json', 'w') as f:
json.dump(data, f, indent=2)
# Parquet: Columnar storage for analytics (10x smaller, 100x faster)
df.to_parquet('output/housing_data.parquet', engine='pyarrow')
df_parquet = pd.read_parquet('output/housing_data.parquet')
# Size comparison (typical for 1M rows):
# CSV: ~150 MB | JSON: ~250 MB | Parquet: ~15 MBMultilingual environments makes encoding critical.
# Always specify UTF-8 for data
df = pd.read_csv('data/housing_data.csv', encoding='utf-8')
# Common encoding issues:
# - Street names with Chinese characters
# - Malay place names with diacritics
# - Tamil school names
# Safe reading pattern
for encoding in ['utf-8', 'utf-8-sig', 'cp1252', 'latin-1']:
try:
df = pd.read_csv(filepath, encoding=encoding)
break
except UnicodeDecodeError:
continueRequest-Response (Synchronous): Client ββrequestββ> Server ββresponseββ> Client Example: GET /api/housing β JSON response Publish-Subscribe (Async): Producer ββeventββ> Message Queue ββeventββ> Consumer(s) Example: transit crowding event β Kafka topic β Dashboard + Alert service Batch File Transfer: Source ββfileββ> Staging ββprocessββ> Warehouse Example: Monthly housing CSV β Python ETL β SQLite star schema
1. Convert the housing CSV data to JSON format and compare file sizes.
2. Create a data flow diagram for: API β Validate β Transform β Store β Query β Chart.
3. What encoding would you use for a dataset containing both English and Chinese street names?