A weather service exports its rain gauges as CSV text. The header names the columns (their order may change between exports), and one of them is station, another rain_mm. Station names can contain commas, and are then written in double quotes, as CSV allows: "Hilltop, North".
Write rain_by_station(text) that returns a dictionary from each station name to its total rainfall in millimetres, as a float. In the rain_mm column, T means a trace of rain and counts as 0.0, and an empty cell means the gauge was not read and is skipped. Every station that appears in the data is in the result, even if all its cells are empty.
The helper rain_csv(n, seed) generates an export with n rows; try print(rain_csv(5, 1)) with Run.
Examples
Input: text = 'station,rain_mm\nRiverside,2.5\n"Hilltop, North",T\nRiverside,1.0\n"Hilltop, North",4\n'
Output: {"Riverside": 3.5, "Hilltop, North": 4.0}
Input: text = 'rain_mm,station\n,Airport\n'
Output: {"Airport": 0.0}
Constraints
- The text has a header line; it may end lines with
"\n"or"\r\n". - Up to
10**5rows; non-emptyrain_mmcells areTor a valid number.
Goals
- Read CSV text with the `csv` module instead of splitting on commas
- Find columns by their header names with `csv.DictReader`
- Convert string fields and handle special values
- Add up amounts per key with `defaultdict(float)`