Brief
Read a small CSV and JSON file, recognize headers and types, spot an unreadable value and keep the received file as a source.
In the story, Voss shows a slide where E9 has attempt = 0, claiming your ID launched another attempt. Mara hands you the received copy: its CSV says E7 / NOCTIS-01 / 2, E8 / TEST-02 / 1 and E9 / NOCTIS-01 / unknown. The attached JSON says {"event_id":"E7","attempt":2}. Imani demands a literal reading before contacting the vendor.
Your mission
- Name the header and three CSV records.
- Explain the type of E7’s
attemptin CSV and in JSON. - Say what happens when
unknownis converted withint()and what the report should say. - Explain whether Voss’s 0 appears in these files and which sources to preserve.
How to check your work
- The header is
event_id,vehicle,attempt; rows E7, E8 and E9 are recognized. - CSV reads
'2'as text; JSON reads2as an integer. int('unknown')raisesValueError; E9 stays a raw unreadable value.- No 0 for E9 appears in the supplied files; preserve raw CSV/JSON, source and receipt date.
Course example
import csv
import json
from io import StringIO
csv_text = 'event_id,vehicle,attempt\nE7,NOCTIS-01,2\nE9,NOCTIS-01,unknown\n'
json_text = '{"event_id":"E7","attempt":2}'
with StringIO(csv_text) as file:
rows = list(csv.DictReader(file))
with StringIO(json_text) as file:
record = json.load(file)
print(rows[0]['attempt'], type(rows[0]['attempt']).__name__)
print(record['attempt'], type(record['attempt']).__name__)
try:
int(rows[1]['attempt'])
except ValueError:
print('E9: unreadable attempt')