Home/Blog/Python Data Structures | XML JSON CSV YAML
Python

Python Data Structures | XML JSON CSV YAML

Explore XML, JSON, CSV, YAML, and database structures with practical examples

Python Data Structures | XML JSON CSV YAML

XML Files

XML (Extensible Markup Language) is a structured way of organizing data using human-readable tags. It works similarly to HTML, with opening tags, content, and closing tags.

# Example MySQL connection
import mysql.connector

db = mysql.connector.connect(
  host="localhost",
  user="myuserrname",
  passwd="mypassword"
)

cursor = db.cursor()
cursor.execute("SELECT * FROM mytable")
results = cursor.fetchall()

for result in results:
  print(result)

JSON (JavaScript Object Notation)

JSON is lightweight, easy to read, and widely used in REST APIs. It consists of key-value pairs and supports nested objects and arrays.

# Example MySQL connection
import mysql.connector

db = mysql.connector.connect(
  host="localhost",
  user="myuserrname",
  passwd="mypassword"
)

cursor = db.cursor()
cursor.execute("SELECT * FROM mytable")
results = cursor.fetchall()

for result in results:
  print(result)

CSV (Comma Separated Values)

CSV files have a row of headings followed by data rows, with each column separated by commas. They’re commonly used for database exports and spreadsheet data.

# Example MySQL connection
import mysql.connector

db = mysql.connector.connect(
  host="localhost",
  user="myuserrname",
  passwd="mypassword"
)

cursor = db.cursor()
cursor.execute("SELECT * FROM mytable")
results = cursor.fetchall()

for result in results:
  print(result)

YAML (YAML Ain’t Markup Language)

YAML is commonly used for configuration files and is similar to JSON but uses indentation instead of brackets. It’s space-sensitive like Python.

# Example MySQL connection
import mysql.connector

db = mysql.connector.connect(
  host="localhost",
  user="myuserrname",
  passwd="mypassword"
)

cursor = db.cursor()
cursor.execute("SELECT * FROM mytable")
results = cursor.fetchall()

for result in results:
  print(result)

Databases

Databases are specialized programs for storing, organizing, and retrieving data at scale. They provide programmatic interfaces and handle data consistency automatically.

# Example MySQL connection
import mysql.connector

db = mysql.connector.connect(
  host="localhost",
  user="myuserrname",
  passwd="mypassword"
)

cursor = db.cursor()
cursor.execute("SELECT * FROM mytable")
results = cursor.fetchall()

for result in results:
  print(result)

Frequently Asked Questions

Find answers to common questions

Speed benchmark (1MB file): CSV fastest (read 0.1s, write 0.08s), JSON middle (read 0.15s, write 0.12s), XML slowest (read 0.8s, write 0.6s). CSV wins for tabular data (pandas.read_csv() 5-10x faster than JSON). Use CSV when: tabular data, Excel compatibility needed, simple structure, large datasets. Use JSON when: nested data, API communication, configuration files, JavaScript interop. Use XML when: required by external system, complex schemas, legacy integration. Memory: CSV smallest (1MB → 1MB), JSON medium (1MB → 1.3MB), XML largest (1MB → 3-5MB with markup). Pandas dominates for CSV/Excel: read_csv() and to_csv() highly optimized. JSON standard library: import json, json.load(f) / json.dump(data, f). XML avoid unless required: use ElementTree if needed. Modern trend: JSON for APIs/configs, Parquet for big data (10x faster than CSV for large datasets).

Automate Your IT Operations

Leverage automation to improve efficiency, reduce errors, and free up your team for strategic work.