Search HoldMyTwist

Browse all posts

Episode 38

The Comma in the Jam Jar

Duck has three places on his order rack. He has four pieces of paper. Robot spots the sneaky extra comma in the name of the jam. In CSV text columns are separated by commas.

Episodes

Computing Basics

    Duck is leaning on the counter at the jam shop. He's holding a feather pen in his wing. The order form has three columns.
    The scanner sweeps a light beam over the order form. Below it are a picture of the customer, a picture of the flavour and two little jars.
    Duck raises one jar of jam. On the label, plum and pear are together.
    Duck is indicating that the clear strip above the order rack is empty.
    The unquoted value has four values: Duck, plum, space followed by pear and 2. Note that the space before pear is still present as the 3rd value.
    Duck is trying to insert the four parsed fields into the three column order rack. But there's no column for one of the fields.
    Robot points at card in center of rack. Card is held at both ends by a brass clip.
    The scanner just read over the middle card. Both sets of clips are still attached.
    Three tiles flip out. Middle one has both plum and pear.
    Duck places all 3 tiles in the rack. The middle slot of the rack holds the 2 fruits.
    Duck is showing off two cards. One is the text stored, the other is the decoded value. He's standing next to a jam jar.
    Duck compares encoded, adjacent double quote marks to the single decoded quote mark they represent in a quoted field.
    Duck shows 1 recipe card with a red stripe above a blue stripe on the same recipe card.
    Duck has new order strip with jam jars. Coloured wooden rack is set.
    The scanner halts at the newly arrived order, without showing the field count or the middle value after decoding.
    The same fruit tile is shared by both fruits.
    Two correctly encoded orders with CR-LF between them (not within any quoted field).
    Robot sees the three slots align.

    Episode transcript

    The extra divider

    Duck is leaning on the counter at the jam shop. He's holding a feather pen in his wing. The order form has three columns.

    Diagram values: Duck,plum,2

    Duck: I have a three column list for my jam order. What's the harm in a comma?

    The scanner sweeps a light beam over the order form. Below it are a picture of the customer, a picture of the flavour and two little jars.

    Diagram values: Duck | plum | 2

    Robot: In RFC4180 we use commas to separate fields.

    Duck raises one jar of jam. On the label, plum and pear are together.

    Diagram values: FLAVOUR:plum, pear

    Duck: Plum + Pear can both be in the same jar. One flavour.

    Duck is indicating that the clear strip above the order rack is empty.

    Diagram values: Duck,plum, pear,2

    Duck: What if I drop the quotes?

    Put quotes around the field

    The unquoted value has four values: Duck, plum, space followed by pear and 2. Note that the space before pear is still present as the 3rd value.

    Diagram values: [Duck][plum][ pear][2]

    Robot: We needed three. With the added comma, we had four.

    Duck is trying to insert the four parsed fields into the three column order rack. But there's no column for one of the fields.

    Duck: My flavour went over the barrier!

    Robot points at card in center of rack. Card is held at both ends by a brass clip.

    Diagram values: Duck,"plum, pear",2

    Robot: Double quote the whole flavour field.

    The scanner just read over the middle card. Both sets of clips are still attached.

    Diagram values: "plum, pear"

    Duck: Will that comma still split up my jam?

    Three tiles flip out. Middle one has both plum and pear.

    Diagram values: [Duck][plum, pear][2]

    Robot: No. The flavour value gets a comma.

    Quotes and line breaks

    Duck places all 3 tiles in the rack. The middle slot of the rack holds the 2 fruits.

    Diagram values: 3 FIELDS

    Duck: Both fruits in the same tag.

    Duck is showing off two cards. One is the text stored, the other is the decoded value. He's standing next to a jam jar.

    Diagram values: VALUE:He said "hi"; CSV:"He said ""hi"""

    Robot: I want a quote mark to appear on my jam label. How do I make it stay?

    Duck compares encoded, adjacent double quote marks to the single decoded quote mark they represent in a quoted field.

    Diagram values: "" → "

    Duck: Two written quotes will hold only one quote in the value.

    Duck shows 1 recipe card with a red stripe above a blue stripe on the same recipe card.

    Diagram values: "red<CRLF>blue"; ONE QUOTED FIELD

    Robot: Even a field you've quoted can have a line break in it. Not all physical lines are entire records.

    A fresh jam order

    Duck has new order strip with jam jars. Coloured wooden rack is set.

    Diagram values: Pip,"pear, plum",4

    Duck: Another order! It's time for the pear to take the lead.

    The scanner halts at the newly arrived order, without showing the field count or the middle value after decoding.

    Diagram values: Pip,"pear, plum",4

    Robot: How many fields? What's the exact middle value?

    The same fruit tile is shared by both fruits.

    Diagram values: [Pip][pear, plum][4]

    Duck: Three fields. The central field is pear, plum. No outer quotes.

    Two correctly encoded orders with CR-LF between them (not within any quoted field).

    Diagram values: Duck,"plum, pear",2<CRLF>Pip,"pear, plum",4

    Robot: Commas separate fields for each order.

    Robot sees the three slots align.

    Diagram values: RFC4180 EXAMPLE

    Duck: I'll use a csv parser to take care of the dividers.

    The lesson

    Diagram values: CSV = comma-separated values; CRLF = carriage return followed by line feed

    RFC4180 notes fields separated by commas. Record separators are CRLF. If fields contain commas, double quotes, or line breaks, wrap the field in double quotes. Embedded double quotes in a quoted field should be escaped (doubled). The double quotes around the field are syntactical and not part of the decoded value. A double quoted CRLF can appear in the field. So you can't just break a file at each physical line or comma. Spaces are part of the value. RFC4180 is informational. CSV dialects vary. Use a parser tuned for your format. This is a story about RFC4180 examples, not all formats that use the label CSV.

    Try it

    Diagram values: Ada,"say ""yes"", now",7

    How many fields does the scanner return? What's the exact middle value, including punctuation?

    Answer

    Diagram values: Field 1 = Ada; Field 2 = say "yes", now; Field 3 = 7

    Three fields. Embedded double quotes in a quoted field should be escaped (doubled).

    Sources

    RFC 4180 section 2, Definition of the CSV Format (opens in a new tab)