Menu

Postal codes are strings: test a CSV/JSON fixture round trip in Python
📰

Postal codes are strings: test a CSV/JSON fixture round trip in Python

DEV Community: beginners·AddressSample·13 days ago
#6sb9twla
#devtobeginners#devto#dev#json#address
Reading 0:00
15s threshold

A postal code can look like a number without being one. If an importer converts "00123" to 123 , the leading zeros have already disappeared. Converting the result back to a string does not recover them. Canadian-shaped values such as "A1A 1A1" also contain letters and a significant space.

For an address form test, preserving the postal string is only half the job. The street, locality, region and postal field should stay attached to the same record. A valid-looking code independently shuffled into a different locality can make a fixture misleading.

This tutorial builds a small offline check for an address export with a JSON addresses array and a CSV ZIP column. It uses Python's standard library. The included fixtures are invented: their codes and locality names are not assertions about real assigned postal codes or deliverable addresses. No customer records or third-party database is needed.

1. Make the round trip reproducible

Use Python 3.10 or newer. Save the following as postal_roundtrip.py .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.