Programming · free software · the open webSubscribe via RSS

Could You Build a CSV Unit Normalizer?

From the earlier reference collection. Read the current software blog.

Normalize selected temperature columns while retaining original values, units, and enough metadata to reverse or audit the conversion.

Map columns to units explicitly

A CSV unit normalizer should convert only columns the user identifies. A header such as water_temp does not reveal whether its values are Celsius or Fahrenheit, so require a mapping from column name to source unit and a selected output unit. Reject unknown units rather than guessing.

Keep the input file unchanged and write a new output file. Include the chosen column mapping in a sidecar note or output metadata supported by the project. For a simple CSV, additional metadata can be placed in a separate manifest, because adding prose rows to the table may break programs that expect the original header on line one.

Use a transparent conversion

For Fahrenheit to Celsius, subtract 32 and multiply by five ninths. For Celsius to Fahrenheit, multiply by nine fifths and add 32. For example, 68°F converts to 20°C; converting the result back gives 68°F before any display rounding. Keep calculations at sufficient precision, and round only when formatting output if the user has specified a precision.

Preserving the source values is valuable for audit and comparison. One approach is to add a new column such as air_temp_c and leave air_temp_f untouched. Another is to write converted values into a separate output table while retaining a manifest that records the source column, units, conversion, and output precision. Make the choice visible in the command’s options.

Handle missing and malformed cells

An empty cell should remain empty, not become zero. If a nonempty cell cannot be parsed as a number, report its row and column and either stop or preserve it according to an explicit policy. Silent substitution can turn a text error into a plausible but false measurement.

Use a CSV library so quoted commas and escaped quotes survive the round trip. Preserve non-temperature fields exactly where possible, and keep row order stable. If the input has duplicate column names, require a column index or stop with an ambiguity message instead of converting an arbitrary match.

Make provenance travel with the result

A normalized value is easier to interpret when readers can recover its source context. Record which file was processed, which columns were converted, the original and target units, and the conversion date or program version if relevant to the workflow. Do not imply that unit conversion improves measurement accuracy; it changes representation, not the sensor’s calibration.

The useful endpoint is a new file whose converted columns are clearly named and whose original values remain available. Before using it downstream, inspect a few known examples, including a missing value and a negative temperature. Those checks catch unit-direction mistakes and ensure the output remains a faithful transformation of the source.

Suggest a correction · All posts