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)