How to Work with Data File Structures in Python

"Stacked red acrylic panels creating an abstract geometric pattern, showcasing modern art and design."

How to Work with Data File Structures in Python

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

When writing programs and scripts, it’s important that you follow standards in the way you structure your data. These standards allow one program to generate data that another program can consume, and enable programs to save data for later use.

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)

Elevate Your IT Efficiency with Expert Solutions

Transform Your Technology, Propel Your Business

Unlock advanced technology solutions tailored to your business needs. At InventiveHQ, we combine industry expertise with innovative practices to enhance your cybersecurity, streamline your IT operations, and leverage cloud technologies for optimal efficiency and growth.

<?xml version="1.0" encoding="UTF-8"?>
<text>
  <line1>Hello World!</line1>
  <line2>Hello World!</line2>
</text>

# Python code to read XML
import xml.etree.ElementTree as xml
tree = xml.parse('test.xml')
myxml = tree.getroot()

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)

Elevate Your IT Efficiency with Expert Solutions

Transform Your Technology, Propel Your Business

Unlock advanced technology solutions tailored to your business needs. At InventiveHQ, we combine industry expertise with innovative practices to enhance your cybersecurity, streamline your IT operations, and leverage cloud technologies for optimal efficiency and growth.

[{
"Name" : "Bob",
"City" : "San Diego",
"State" : "CA"
},
{
"Name" : "Sue",
"City" : "San Francisco",
"State" : "CA"
}]

# Python code to work with JSON
import json
data = json.loads(json_string)
print(json.dumps(data, indent=2))

Advantage: JSON is less verbose than XML, taking less storage space and bandwidth, which translates to faster web applications.

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)

Elevate Your IT Efficiency with Expert Solutions

Transform Your Technology, Propel Your Business

Unlock advanced technology solutions tailored to your business needs. At InventiveHQ, we combine industry expertise with innovative practices to enhance your cybersecurity, streamline your IT operations, and leverage cloud technologies for optimal efficiency and growth.

Name, City, State
bob,San Diego,CA
sue,New York,NY
joe,Miami,FL

# Python code to read CSV
import csv
with open('data.csv', 'r') as file:
    csv_reader = csv.reader(file)
    for row in csv_reader:
        print(row)

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)

Elevate Your IT Efficiency with Expert Solutions

Transform Your Technology, Propel Your Business

Unlock advanced technology solutions tailored to your business needs. At InventiveHQ, we combine industry expertise with innovative practices to enhance your cybersecurity, streamline your IT operations, and leverage cloud technologies for optimal efficiency and growth.

---
Bob:
  City: San Diego
  State: CA
Sue:
  City: New York
  State: NY

# Python code to work with YAML
import yaml
with open('data.yml', 'r') as file:
    data = yaml.safe_load(file)
    print(yaml.dump(data))

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)

Elevate Your IT Efficiency with Expert Solutions

Transform Your Technology, Propel Your Business

Unlock advanced technology solutions tailored to your business needs. At InventiveHQ, we combine industry expertise with innovative practices to enhance your cybersecurity, streamline your IT operations, and leverage cloud technologies for optimal efficiency and growth.

Share this: