File size: 2,150 Bytes
b199558
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# `canon` — canonicalise a CSV export

Downstream tools need one boring, byte-stable TSV. Write an executable script at
`bin/canon` (shebang, `chmod +x`) that is invoked as:

```
./bin/canon <input.csv> <output.tsv>
```

## Input

UTF-8 CSV, possibly with a byte-order mark, possibly CRLF, RFC 4180 quoting:
fields may be quoted, may contain commas, doubled quotes (`""`) and embedded
newlines. The first record is the header.

## Output

UTF-8 **without** a BOM, `\n` line endings, tab-separated, one line per record,
header first. Fields are written verbatim — never re-quoted, never trimmed —
except for the numeric normalisation below. Column order is preserved.

## Numeric normalisation

A field is numeric when the whole field matches
`^[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$`. Numeric fields are rewritten in
plain decimal notation with no exponent, no leading `+`, no leading zeros, and
no trailing zeros after the decimal point:

| input | output |
| --- | --- |
| `1.50` | `1.5` |
| `1.0` | `1` |
| `1e3` | `1000` |
| `2.5e-3` | `0.0025` |
| `+5` | `5` |
| `007` | `7` |
| `-0` | `0` |

Every other field is copied unchanged.

## Ordering

Sort the data records by field 0, then by field 1, comparing the **normalised
strings** in byte order (so `"10"` sorts before `"9"`). The sort must be stable.

## Errors

On any error, print exactly one line to stderr, exit with the given code, and do
**not** create the output file:

| condition | stderr line | exit |
| --- | --- | --- |
| input path does not exist | `no such file: <input path as given>` | `2` |
| two header names collide case-insensitively after trimming | `duplicate column: <name as written in the later header field>` | `3` |
| a record's field count differs from the header's | `malformed row: line <n>` | `4` |

`<n>` is the 1-based physical line number where the offending record starts
(so a record whose first line is the third line of the file reports `3`).

On success exit `0` and print nothing to stderr.

## Notes

- `python` and `python3` are both on `PATH`; standard library only, no network.
- `data/orders.csv` is a sample with a BOM and CRLF endings.