#150 · Parsing · Advanced · 135 XP · about 25 min
A CSV file stores a table as text, one row per line, with commas between the values. Splitting on every comma breaks as soon as a value contains a comma itself, so CSV lets you wrap a value in double quotes. Write split_csv(line) that splits one line into a list of field strings, following these rules:
"") stand for one double quote. The wrapping quotes are not part of the value.split_csv('id,name,city') # returns ['id', 'name', 'city'] split_csv('7, "Smith, Jo" , London') # returns ['7', 'Smith, Jo', 'London'] split_csv('"She said ""hi""",,end') # returns ['She said "hi"', '', 'end']
line has 0 to 10,000 characters and no line breaks. An empty line holds one empty field.Ready?
Accept the challenge to start writing. A small timer counts the time this page is in view.
Output appears here.