Product case study · November 24, 2026
Ask Claude Code to preserve leading zeros in CSV IDs
Reproduce a CSV export that collapses distinct customer IDs, then give Claude Code a bounded repair and verify exact values with Python tests.

Treat identifiers as text at every step. Padding zeros back onto a number cannot recover an ID whose original width is unknown.
00127 and 127 can belong to different customers. Turn both into integers during an export and you still have two rows, but you have lost the distinction that makes them useful.
This is a good task for Claude Code because the patch can be tiny while the acceptance criteria do the hard work. My preference: make the agent preserve the original identifier, rather than invent a rule for putting the zeros back.
The exercise uses fictional customer records, Bash, Python 3 and an installed, signed-in Claude Code. The Python baseline and reference repair were tested locally; the agent prompt is an authored exercise, not a recorded Claude session. Official sources were checked November 24, 2026.
Create an export that quietly changes an ID
Run this in a Bash terminal:
id_lab=$(mktemp -d /tmp/claude-csv-ids.XXXXXX)
cd "$id_lab"
cat > customers.csv <<'CSV'
customer_id,name
00127,Ada
127,Lin
CSV
cat > exporter.py <<'PY'
import csv
def export_customers(source, destination):
with open(source, encoding="utf-8", newline="") as stream:
rows = list(csv.DictReader(stream))
for row in rows:
row["customer_id"] = str(int(row["customer_id"]))
with open(destination, "w", encoding="utf-8", newline="") as stream:
writer = csv.DictWriter(stream, fieldnames=["customer_id", "name"])
writer.writeheader()
writer.writerows(rows)
PY
python3 - <<'PY'
from exporter import export_customers
export_customers("customers.csv", "export.csv")
print(open("export.csv", encoding="utf-8").read(), end="")
PY
The broken export contains 127 in both rows. Python’s default CSV reader returns strings; the numeric conversion was added by our code. Calling int() keeps the numeric value, not the original spelling. See the CSV reference and integer conversion reference.
Adding .zfill(5) afterward would make both IDs 00127. That merely chooses a different wrong answer. Once the original string has been discarded, the number alone cannot tell you which customer it belonged to.
Write the contract before the repair
Save this as SPEC.md:
Export a UTF-8 CSV with customer_id,name headers to a different path.
Preserve each customer_id and name exactly as parsed strings.
Preserve row order and row count; do not merge equivalent-looking IDs.
IDs may contain leading zeros, letters or surrounding spaces.
Do not trim, case-fold, pad or convert IDs to numbers.
Do not alter the source file. CSV quoting may change in the output,
but reading it back must produce the same field values.
Change only exporter.py. Keep this specification and tests fixed.
Use only the Python standard library.
For this lab, inputs have exactly those two headers and two populated fields per record. Source and destination must be different files, and the destination must be disposable. Validation of malformed rows, overwrite protection and atomic writes are separate jobs. This little exporter does not implement them.
The unusual-looking space rule is deliberate: we are testing preservation, not deciding which identifiers your business should accept. If a real system forbids spaces, add a separate validation rule that rejects them explicitly.
Save this as test_exporter.py:
import csv
import tempfile
import unittest
from pathlib import Path
from exporter import export_customers
class ExportTests(unittest.TestCase):
def round_trip(self, rows):
with tempfile.TemporaryDirectory() as folder:
source = Path(folder) / "source.csv"
destination = Path(folder) / "export.csv"
with source.open("w", encoding="utf-8", newline="") as stream:
writer = csv.writer(stream)
writer.writerow(["customer_id", "name"])
writer.writerows(rows)
original = source.read_bytes()
try:
export_customers(source, destination)
with destination.open(encoding="utf-8", newline="") as stream:
result = list(csv.reader(stream))
self.assertEqual(result[0], ["customer_id", "name"])
self.assertEqual(result[1:], rows)
finally:
self.assertEqual(source.read_bytes(), original)
def test_distinct_numeric_spellings(self):
self.round_trip([["00127", "Ada"], ["127", "Lin"]])
def test_letters_and_quoted_name(self):
self.round_trip([["A001", "Lee, Jo"]])
def test_spaces_are_preserved(self):
self.round_trip([[" 00127 ", "Zoë"]])
def test_plain_numeric_id(self):
self.round_trip([["127", "Lin"]])
Run:
python3 -m unittest -v test_exporter
The local baseline ran four tests: two failed, one errored and one passed. The numeric conversion removes zeros and spaces, and raises ValueError for A001. The ordinary 127 case passes, showing why a sample with only plain numeric IDs misses the defect.
Give Claude the failing case and a narrow job
Start claude in the practice folder and paste:
Read SPEC.md, exporter.py and test_exporter.py.
Run python3 -m unittest -v test_exporter and report the baseline.
Explain where the original identifier spelling is lost.
Change only exporter.py to meet the contract. Do not change the tests,
rewrite customers.csv, install packages, commit or delegate.
Do not pad IDs to a guessed width or normalize whitespace.
Run the same tests after the repair and report the actual result.
Explain why 00127 and 127 remain distinct in the output.
Anthropic’s common workflows describes using reproducible bugs and tests to guide a repair. Review the proposed edits and commands in your active permission mode; the prompt’s file scope is an instruction, not an isolation mechanism.
If Claude proposes a fixed ID width, point it back to the first test. If it changes the expected rows to integers, it has changed the requirement instead of fixing the export. If the baseline unexpectedly passes, check that the test imports the exporter.py in this folder.
Review the reference repair
The reference implementation removes the conversion loop:
import csv
def export_customers(source, destination):
with open(source, encoding="utf-8", newline="") as stream:
rows = list(csv.DictReader(stream))
with open(destination, "w", encoding="utf-8", newline="") as stream:
writer = csv.DictWriter(stream, fieldnames=["customer_id", "name"])
writer.writeheader()
writer.writerows(rows)
All four tests passed locally with this implementation. Keeping newline="" lets the CSV module handle newline conventions. The check compares parsed field values, so harmless differences in CSV serialization do not masquerade as data loss.
Exit the agent and rerun the tests yourself. Then regenerate export.csv with the earlier demo command: its first ID should be 00127, and its second should remain 127.
Use the parsed values as your evidence. If a downstream application displays something different, inspect the saved CSV with Python before editing it again. This test establishes what the exporter writes; it does not establish how every receiving application will interpret the file.
If the input already contains 127 where the original system held 00127, this patch cannot reconstruct the missing characters. Recover the identifier from an authoritative export or mapping. Guessing a width is a data change that deserves its own requirement.
Sources
Checked November 24, 2026:
- Python: csv — default string fields, dictionary readers and writers, and newline handling.
- Python: int — conversion from a string representation to an integer value.
- Anthropic: Common workflows — scoping bug fixes and checking them with tests.
Put this to work
Why numeric conversion destroys identifier spelling and how to give an agent a testable data contract.
Try
Ask Claude Code to repair one CSV transformation in a disposable Python folder.
Prove it worked
Run four tests against the broken implementation and the repair, then compare parsed output IDs.
Keep in view
- Reproduce an export that changes 00127 into 127.
- Specify exact identifier preservation before asking the agent to repair it.
- Check distinct IDs, quoted fields and unchanged input bytes.