Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
A partner sends a CSV export. It looks simple until a customer has a comma in their name, at which point every hand-rolled parser in the pipeline shifts that row one column to the right and nothing errors.
Write parse_csv(text, required). It returns the parsed records and the rows it rejected for missing a required field.
Function to write
parse_csv(text: str, required: list[str]) -> dictA dict with the parsed `records` and the `rejected` rows, both in input order.
How to approach it
Run the starter and look at what note contains on the first data row.
Sample cases
+ 2 held back until you submit
a quoted comma, an escaped quote and a missing field
The three things split(',') cannot do: a comma inside quotes, a doubled quote, and a required field that is blank.
Input
Argument 1
'id,name,note
1,"Marsh, Eve",ok
2,Farid,"said ""hi"""
3,,missing name
'Argument 2
[
'id',
'name'
]Returns
{
'records': [
{
'id': '1',
'name': 'Marsh, Eve',
'note': 'ok'
},
{
'id': '2',
'name': 'Farid',
'note': 'said "hi"'
}
],
'rejected': [
{
'line': 2,
'missing': [
'name'
]
}
]
}ordinary rows with no quoting
The plain case still has to work, and the values come back trimmed.
Input
Argument 1
'id,name
1,Amir
2,Cara
'Argument 2
[
'id'
]Returns
{
'records': [
{
'id': '1',
'name': 'Amir'
},
{
'id': '2',
'name': 'Cara'
}
],
'rejected': []
}empty input
No text at all returns empty lists rather than raising on the missing header.
Input
Argument 1
''Argument 2
[
'id'
]Returns
{
'records': [],
'rejected': []
}Constraints
{'records': <list of dicts>, 'rejected': <list>}.required field is missing or blank is rejected as {'line': <0-based data-row index>, 'missing': [<fields>]} and does not appear in records.Worked example
The second data row holds the name Marsh, Eve in quotes and the third has a blank name.
Splitting on commas gives the first row four fields instead of three, so note silently becomes Eve" and every downstream column is shifted. The row still parses, still writes, and is still wrong — which is why this belongs to the csv module rather than to a one-liner.
What this tests
That CSV is a format with quoting rules rather than a line of text with commas in it, and that a row failing a contract is reported as data with enough context to find it rather than dropped or raised.
parse_csv(text: str, required: list[str]) -> dictSubmit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.