
A missing customer label and a deliberately empty label can mean different things. A CSV export may preserve that distinction, yet the next reader can erase it. Checking the file exists—or that the row count stayed the same—will not catch the loss.
Make null handling a contract between writer and consumer. Reserve a text marker that cannot appear as a real value, configure the writer to emit it for nulls, and decode only that marker as null. Keep empty text as empty text. This small notebook fixture tests the whole path, including a comma and a quotation mark inside real values.
import csv
from pathlib import Path
from tempfile import TemporaryDirectory
null_text = "__KF_NULL__"
expected = [(1, None), (2, ""), (3, "a,b"), (4, 'say "hi"')]
df = spark.createDataFrame(expected, "id INT, label STRING")
assert df.filter(df.label == null_text).count() == 0
with TemporaryDirectory() as folder:
output = str(Path(folder) / "export")
(df.write.option("header", True)
.option("nullValue", null_text).csv(output))
decoded = []
for part in Path(output).glob("*.csv"):
with part.open(encoding="utf-8", newline="") as stream:
for row in csv.DictReader(stream):
label = row["label"]
decoded.append((
int(row["id"]),
None if label == null_text else label
))
assert sorted(decoded) == expected
print(sorted(decoded))
The example uses the notebook’s existing spark session and Python’s standard csv reader. It writes to a disposable local folder, reads every CSV part with its own header, converts the integer field explicitly and compares all four decoded rows. The reserved-marker check runs before writing: a real label equal to __KF_NULL__ must stop the export rather than become indistinguishable from missing data.
At KratosForge app commit d5980e9, the local CSV writer emits __KF_NULL__ for the null label and quoted empty text for the empty label. The consumer above returns None and an empty string respectively. We executed the exact example through the local notebook kernel and separately confirmed that a literal marker triggers the assertion. PR #3565 added support for nullValue on local CSV writes; five selected writer regression tests passed in this review.
Do not assume another reader preserves the same result. In our separate probe at that commit, spark.read with the same schema, header and nullValue options returned None for both the null and the quoted empty string. The bytes were distinct; that local reader’s interpretation collapsed them. The standard-library consumer in this example is intentional. Test your actual downstream reader before using this export for a workflow that depends on the distinction.
CSV also leaves types to the reader. Agree on the schema, delimiter, encoding, quoting and null marker together. The one-label collision check here fits this fixture; an export with more text columns must check each relevant column. If a marker cannot be reserved reliably, use a format and consumer that preserve typed nulls directly, then verify that path too.
This is verified local main-branch behavior, not a new Stable promise or a live Fabric/Spark-cluster result. The official Stable release record was still last updated November 2 when checked. Add null, empty text, the literal marker and escaped punctuation to your export tests, and assert the decoded values. The consumer’s answer is the result that matters.
← All publications