esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • Book a callContact
  • Download ResumeContact
Let’s talk

I’ll build the agent for it.

Machine Learning Foundations / Beginner / 01.51

Other data formats: XML, YAML, SQLite, images, audio and more

Beyond CSV, JSON and Parquet, data also hides in config files, one-file databases, pictures and sound. Here is how to recognise each one, and the one format you should never open from a stranger.

13 min readFree

Why more formats?

You have met CSV, JSON and Parquet. Real projects are messier. A team might get a settings file, an old export, a folder of photos, a pile of voice recordings or a saved model. Each comes in its own format.

A file format is an agreement about how information is written into bytes. Know the agreement and you can read the file. This lesson is a short tour of the other formats you will meet most often.

Key words

File format
The agreed way a file writes its information down, so that any program that knows the agreement can read it.
Configuration file
A small file of settings that tells a program how to behave, such as how fast a model should learn.
Channel
One layer of numbers inside a picture or a sound. A colour picture has red, green and blue channels. A stereo sound has a left and a right channel.
Lossless / lossy
Lossless compression shrinks a file and can give back every original bit. Lossy compression shrinks it more by throwing some detail away for good.
Serialization
Turning something that lives in a program's memory into bytes that can be saved or sent.

XML: data wrapped in tags

XML stands for Extensible Markup Language. The W3C, the group that writes web standards, says it 'describes a class of data objects called XML documents and partially describes the behavior of computer programs which process them.' In plain words, it is a way to write data as text, using tags that you invent yourself.

A tag opens with a name in angle brackets and closes with the same name and a slash. Tags nest like boxes inside boxes. An attribute is a name and value inside the opening tag; id below is one.

Python reads XML with no extra install. The Python documentation says fromstring() 'parses XML from a string directly into an Element, which is the root element of the parsed tree.'

Python
import xml.etree.ElementTree as ET

text = """<houses>
  <house id="1"><rooms>3</rooms><price>250000</price></house>
  <house id="2"><rooms>4</rooms><price>310000</price></house>
</houses>"""

root = ET.fromstring(text)
for house in root:
    print(house.tag, house.attrib["id"], house.find("rooms").text, house.find("price").text)

Output

house 1 3 250000
house 2 4 310000

The loop prints each house's tag, id and two child values. Every value arrives as text: the price is the string '250000', not a number. Turning text into numbers comes later, in preprocessing.

Tip

When you are handed XML, the usual goal is to turn it into a table and move on.

YAML: settings you can read

YAML is a format for writing data in a way people find easy to read. Its own specification calls it 'a data serialization language designed to be human-friendly and work well with modern programming languages for common everyday tasks.' It says YAML is useful for 'programming needs ranging from configuration files to internet messaging to object persistence to data auditing and visualization.'

In machine learning you mostly see YAML as a configuration file: a learning rate lives in one small file instead of deep in code.

Text
model:
  name: house-prices
  learning_rate: 0.1
  layers:
    - 8
    - 4
data:
  path: houses.csv
  test_share: 0.2

Nesting is shown by indentation, the spaces at the start of a line. A dash starts a list item. A name, a colon and a value make one setting.

Python's standard library has no YAML reader, so this sample is shown as plain text and not run.

Quick check

Your team wants one small file holding the learning rate and the file path for a training run, easy for anyone to edit. Which format fits best?

SQLite: a whole database in one file

Many databases run on a separate server. SQLite does not. Its own site says: 'SQLite does not have a separate server process. SQLite reads and writes directly to ordinary disk files.' It describes itself as 'a self-contained, serverless, zero-configuration, transactional SQL database engine.'

The SQLite file-format document says 'The complete state of an SQLite database is usually contained in a single file on disk.' Inside are tables you can search with SQL, the standard language for asking a database questions.

Python's sqlite3 module works with these files. This sample makes a small database in a temporary folder, adds two rows, asks for the average price, then reads the file's first 16 bytes.

Python
import os
import sqlite3
import tempfile

path = os.path.join(tempfile.mkdtemp(), "houses.db")
con = sqlite3.connect(path)
con.execute("CREATE TABLE houses (rooms INTEGER, price INTEGER)")
con.executemany("INSERT INTO houses VALUES (?, ?)", [(3, 250000), (4, 310000)])
con.commit()
print(con.execute("SELECT AVG(price) FROM houses").fetchone()[0])
con.close()

with open(path, "rb") as f:
    print(f.read(16))

Output

280000.0
b'SQLite format 3\x00'

The average is 280000. The last line is the interesting one. The SQLite documentation says every valid database file 'begins with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00', the text 'SQLite format 3' plus a zero byte. That is what we read: a fixed label lets a program recognise the file.

Images: a grid of numbers

A picture is a grid of pixels, tiny dots, and each pixel is a few numbers: the amounts of red, green and blue. Each of the three is a channel. A picture 2 pixels wide and 2 tall has 4 pixels and 12 numbers.

  • PNG: lossless

    The PNG specification describes a format 'for the lossless, portable, well-compressed storage of static and animated raster images.' It supports 'indexed-color, greyscale, and truecolor images' plus 'an optional alpha channel', which holds how see-through each pixel is.

  • JPEG: lossy

    Wikipedia describes JPEG as 'a commonly used method of lossy compression for digital images, particularly for those images produced by digital photography.' It adds that the degree of compression can be adjusted, 'allowing a selectable trade off between storage size and image quality.'

  • What it means for ML

    A lossless format keeps the exact pixels; a lossy one does not. Check that all your pictures were saved the same way.

You can look inside a PNG without any image library. The PNG specification says: 'The first eight bytes of a PNG datastream always contain the following hexadecimal values: 89 50 4E 47 0D 0A 1A 0A.' The next sample builds a tiny 2 by 2 truecolour PNG in memory, with the standard library modules struct and zlib, and checks its first eight bytes against that rule.

Python
import struct
import zlib

SIGNATURE = bytes.fromhex("89504E470D0A1A0A")  # from section 5.2 of the PNG spec

def chunk(kind, data):
    body = kind + data
    return struct.pack(">I", len(data)) + body + struct.pack(">I", zlib.crc32(body))

# A 2x2 picture. Colour type 2 = truecolour: 3 channels (red, green, blue), 8 bits each.
header = struct.pack(">IIBBBBB", 2, 2, 8, 2, 0, 0, 0)
rows = b"\x00" + bytes([255, 0, 0, 0, 255, 0]) + b"\x00" + bytes([0, 0, 255, 255, 255, 255])
png = SIGNATURE + chunk(b"IHDR", header) + chunk(b"IDAT", zlib.compress(rows)) + chunk(b"IEND", b"")

print(png[:8].hex(" ").upper())
print(png[:8] == SIGNATURE)
width, height = struct.unpack(">II", png[16:24])
print(width, "x", height, "pixels,", width * height * 3, "numbers")

Output

89 50 4E 47 0D 0A 1A 0A
True
2 x 2 pixels, 12 numbers

The first line prints the signature, the second says it matches the specification, and the last reads the size from the header: 2 x 2 x 3 = 12 numbers. Real projects use a library to read pictures.

Audio: a list of measurements

Sound is a list of measurements. A microphone measures the sound wave again and again; each measurement is a sample, and the sample rate is how many are taken per second. Python's wave module calls it the frame rate, and its documentation says getframerate() 'returns sampling frequency.'

The most common simple sound file is WAV. Wikipedia says it is 'an audio file format standard for storing an audio bitstream', and that its usual encoding is 'the linear pulse-code modulation (LPCM) format', meaning plain, uncompressed measurements. It gives audio CDs as an example: 'two-channel LPCM audio sampled at 44.1 kHz with 16 bits per sample.' Two channels is stereo. The Python documentation says getnchannels() returns '1 for mono, 2 for stereo.'

This sample writes half a second of silence as a WAV in memory, then reads its details back.

Python
import io
import wave

buffer = io.BytesIO()
with wave.open(buffer, "wb") as out:
    out.setnchannels(1)       # mono
    out.setsampwidth(2)       # 2 bytes = 16 bits per sample
    out.setframerate(16000)   # 16,000 samples per second
    out.writeframes(b"\x00\x00" * 8000)  # half a second of silence

buffer.seek(0)
with wave.open(buffer, "rb") as src:
    print("channels:", src.getnchannels())
    print("sample rate:", src.getframerate())
    print("frames:", src.getnframes())
    print("seconds:", src.getnframes() / src.getframerate())

Output

channels: 1
sample rate: 16000
frames: 8000
seconds: 0.5

A rate of 16000 means 16,000 measurements a second, so half a second needs 8,000. Check the rate in your own data: mixing recordings with different rates is a classic mistake.

Tip

Before training on sound or pictures, print the basics for a few files: sample rate and channels, or width, height and channels.

Plain text corpora

Much AI work starts with plain text. A collection of texts used for a project is a corpus. Wikipedia defines a text corpus as 'a dataset, consisting of natively digital and older, digitalized, language resources, either annotated or unannotated.' Annotated means someone added notes, such as labels.

A corpus is often a folder of .txt files, with no columns and no types. The sample treats two lines as a tiny corpus and counts words with Counter from Python's collections module.

Python
from collections import Counter

corpus = """The cat sat on the mat.
The dog sat on the log."""

lines = corpus.splitlines()
words = corpus.lower().replace(".", "").split()
print(len(lines), "lines,", len(words), "words")
print(Counter(words).most_common(3))

Output

2 lines, 12 words
[('the', 4), ('sat', 2), ('on', 2)]

There are 2 lines and 12 words; 'the' appears four times. Real corpora also need cleaning, which preprocessing covers.

Formats made for numbers and tables

These formats are binary: not for people to read, but compact and quick for computers. You only need to know they exist.

  • NumPy .npy

    NumPy's documentation calls it 'the standard binary file format in NumPy for persisting a single arbitrary NumPy array on disk.' It stores 'all of the shape and dtype information necessary to reconstruct the array correctly even on another machine with a different architecture.' A file starts with the six bytes of the string \x93NUMPY. Several arrays go together in an .npz file, which is a zip of .npy files.

  • Feather

    The Apache Arrow documentation says Feather 'is a portable file format for storing Arrow tables or data frames (from languages like Python or R).' Version 2 supports compression with LZ4 or ZSTD.

  • HDF5

    The HDF Group says HDF5 'supports n-dimensional datasets and each element in the dataset may itself be a complex object', and is 'a self-describing file format, meaning everything all data and metadata can be passed along in one file.'

Two more are built for big data systems. Apache Avro's specification says its container file has a schema, 'and all objects stored in the file must be written according to that schema, using binary encoding.' Avro stores its objects in blocks that may be compressed. Apache ORC describes itself as 'a self-describing type-aware columnar file format designed for Hadoop workloads', so like Parquet it stores data column by column. You will see Avro and ORC when you join a team with a large data platform.

Quick check

You trained a NumPy array of image features and want to save just that one array so a teammate on another computer gets the same shape and type back. What fits?

Pickle, and why to be careful

The Python documentation says the pickle module 'implements binary protocols for serializing and de-serializing a Python object structure.' Saving is pickling; loading is unpickling. One line saves almost any Python object.

It also has a serious danger, and the documentation puts it in a warning box: 'The pickle module is not secure. Only unpickle data you trust. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling. Never unpickle data that could have come from an untrusted source, or that could have been tampered with.'

In plain words, opening a stranger's pickle is like running their program. The same page notes that pickle is Python-specific, and that 'unlike pickle, deserializing untrusted JSON does not in itself create an arbitrary code execution vulnerability.' For plain settings and records, JSON is the safer choice.

This sample saves and loads our own small dictionary, then does the same as JSON.

Python
import json
import pickle

model_settings = {"learning_rate": 0.1, "layers": [8, 4]}

blob = pickle.dumps(model_settings)
print(type(blob).__name__, pickle.loads(blob) == model_settings)

text = json.dumps(model_settings)
print(text)
print(json.loads(text) == model_settings)

Output

bytes True
{"learning_rate": 0.1, "layers": [8, 4]}
True

Both give back an equal object. The pickle is Python-only bytes; the JSON is readable text.

How to choose

Ask what is in it, who reads it, and whether you trust where it came from. Then walk through the steps.

  1. 1

    Is it settings for people to edit?

    Use YAML. It is built to be easy for people, and its specification names configuration files as a use.

  2. 2

    Is it several linked tables you want to ask questions about?

    Use a SQLite file. It is one file you can search with SQL, with no server to run.

  3. 3

    Is it one NumPy array, or a table passed between Python and R?

    Use .npy for the array. Use Feather for the table. Parquet, from the last lesson, is another choice for tables.

  4. 4

    Is it a picture or a sound?

    For pictures, PNG keeps every pixel and JPEG makes smaller files by losing detail. For sound, WAV keeps plain measurements. Always record the sample rate or image size with it.

  5. 5

    Did it come from someone you do not trust?

    Do not unpickle it. Prefer JSON, CSV or another plain format for anything from outside.

Often the data simply arrives in a format. The skill is to recognise it and read it safely, which is why signature bytes like those in SQLite and PNG help.

Quick check

Someone sends you a file called model.pkl by chat. You do not know them. What should you do?

Next: preprocessing — getting data ready for a model.

Sources

How is this lesson?

Last updated on 16 November 2026

Learn with me

Every lesson is free. I add new ones as I learn. Have a question, or want help with your own work? Book a short call.