Data files · text fields
Quoting Strings: Surrounding Quotes and Embedded Quotes
Wrapping a string in double quotes sounds like string concatenation, and for simple text it is. The trouble starts when the string itself contains a quote, a comma, or a line break. A quoting routine has to decide what those characters turn into, and the reading routine has to undo exactly that decision. This page covers adding surrounding quotes, removing them, and the two common ways of escaping what is inside. It sits within Data and files because most quoting problems appear when text is written to or read from a file.
Adding surrounding quotes
The simplest quoting step is to place a quote character at each end: the text Smith, John becomes “Smith, John”. That keeps the comma from being read as a field separator. But if the text already holds a quote, as in 5” pipe, the result “5” pipe” ends the quoted region early and leaves pipe” dangling outside it.
Two conventions solve this. The first, used by comma-separated files, doubles each embedded quote, so 5” pipe becomes “5"" pipe”. The second, used in most programming languages and in JSON, puts a backslash in front: “5” pipe”. Pick one and apply it to every field. A file that mixes both cannot be read back reliably.
def quote_csv(s):
return '"' + s.replace('"', '""') + '"'
def quote_backslash(s):
return '"' + s.replace('\\', '\\\\').replace('"', '\\"') + '"'
print(quote_csv('5" pipe')) # "5"" pipe"
print(quote_backslash('5" pipe')) # "5\" pipe"
In the backslash version the backslash itself must be escaped first. Reversing the order would double the backslashes that the quote step just inserted.
Removing surrounding quotes
Stripping is the mirror image, and a careless version does damage. Removing the first and last character blindly turns the unquoted text hello into ell. Check first that the string has at least two characters and that both ends are quote characters, then strip them and reverse the escaping.
For the doubling convention, the order is: strip the outer pair, then replace each pair of quotes with a single one. Applying the replacement before stripping can produce a stray quote at the edge. A worked case: the stored text is “a""b”. After stripping the outer pair it reads a""b, and halving the doubled quote gives a”b, the original value.
Fields that need quotes and fields that do not
Quoting everything is always safe, but it bloats files and makes them awkward to read by eye. A common compromise quotes a field only when it contains the separator, a quote, or a newline. Numbers never need quotes. If a column holds text that looks like a number, such as a postal code with a leading zero, quoting does not protect it once the reader converts it. The reader has to be told to keep that column as text, because quotes are removed before type guessing happens in many tools.
Embedded line breaks
A quoted field may span several lines. A reader that splits a file on newline characters before looking at quotes will cut such a field in half. The reliable approach is to read character by character with a small state machine: inside quotes or outside, and in the inside state a doubled quote is a literal quote while a single quote ends the field.
Testing the pair
Write a test that feeds awkward strings through quote and unquote and checks that the output equals the input: an empty string, a single quote character, two adjacent quotes, a string ending in a backslash, and one containing a comma and a newline. If all of those survive, the pair is sound.
Parsing the unquoted result into numbers is covered under ASCII to double conversion, and spreadsheet exports with their own quoting habits appear under reading spreadsheet files into a matrix.