FIND YOUR NEXT FEW LINES

The snippet library.

Small, practical examples. Search by task or narrow things down below.

125 snippets foundClear filters
PythonDates & time zones

Parse an ISO calendar date

Python: Use date.fromisoformat and inspect the calendar components.

Python✓ Tested
PythonStrings & dates

Parse and format a date

Convert a date string into a datetime object, then format it in a different, human-readable order.

date✓ Tested
PythonStructured data & serialization

Parse quoted CSV from an in-memory string

Python: Use StringIO with csv.reader so quoted commas are handled correctly.

Python✓ Tested
PythonFunctions & error handling

Preconfigure some function arguments

Python: Use partial to produce a callable with selected arguments already bound.

Python✓ Tested
PythonGetting Started

Print a number with two decimal places

Python: Format a small decimal value consistently for display.

Python✓ Tested
PythonStructured data & serialization

Produce compact deterministic JSON

Python: Sort dictionary keys and remove separator whitespace for a reproducible representation.

Python✓ Tested
PythonFunctions & error handling

Raise and catch a domain-specific exception

Python: Give a validation failure its own exception type so callers can handle it narrowly.

Python✓ Tested
PythonFile operations & archives

Read a file size in bytes

Python: Use stat().st_size to read the logical byte length without loading the file.

Python✓ Tested
PythonJSON & CSV

Read a JSON file

Load a JSON document from disk into Python objects using an explicitly selected text encoding.

json✓ Tested
PythonStructured data & serialization

Read a small INI configuration

Python: Use ConfigParser typed accessors for configuration values stored as text.

Python✓ Tested
PythonStructured data & serialization

Read a tab-separated table

Python: Select a delimiter explicitly rather than splitting lines manually.

Python✓ Tested
PythonFiles & paths

Read a text file

Open a UTF-8 text file and read its contents. A context manager closes the file automatically, even if reading fails.

file✓ Tested