Problem of the Day
Problem:
The "Comma-Separate Value" (CSV) file format is one in which distinct elements of data on a line are separated by commas. To keep track of Name,Age,Address
data, for example, one might see the line
Elsa Rodriguez,34,21 Broadway Ave
Occasionally, an element of data in a CSV file may include its own comma. The address 21 Broadway Ave, Unit 2b
has a comma that would incorrectly indicate an additional element of data. To accommodate this issue, a comma that is part of the data can be "escaped" by placing a backslash "\" in front of it.
For the incorrect Name, Age, Address
data given here, how many elements of data are suggested? How should that line be written so that it is correctly interpreted?
Rodriguez, Elsa,34,21 Broadway Ave, Unit 2b
incorrect number of elements indicated | corrected CSV line | |
a. | 4 | Rodriguez,\ Elsa,34,21 Broadway Ave,\Unit 2b |
b. | 4 | Rodriguez\, Elsa\,34\,21 Broadway Ave\,Unit 2b |
c. | 5 | Rodriguez, Elsa\,34\,21 Broadway Ave,Unit 2b |
d. | 5 | Rodriguez\, Elsa,34,21 Broadway Ave\, Unit 2b |
The correct answer is d. The commas that we want to keep in the data—the one between Elsa's last and first names, and the one in the address—need to have the backslash character in front of them. The other commas, separating the elements of the data, should not be escaped.