Given a list of lists, as returned e.g. by pcsv-parse-buffer, what's a correct way of inserting it to the buffer in CSV format ?

I looked at pcsv.el and csv-mode.el but could not find a function to do that.

I think the main point is to correctly escape commas and double-quotes within each field.


I used the following snippet to do that. It uses a function from Org Mode to quote each field.

  (let ((parsed (let ((pcsv-separator ?\;))
    (goto-char (point-min))
    (insert (mapconcat (lambda (line)
                         (mapconcat #'org-quote-csv-field line ","))
                       parsed "\n"))
    ; delete previous content
    (delete-region (point) (point-max)))
  • 1
    Doesn't this miss the final newline? The outer mapconcat only inserts newlines between lines.
    – npostavs
    Sep 24 '17 at 12:06
  • @npostavs thanks I didn't know a final newline is required. I wonder if there is code that relies on the newline though. At least it worked for me without it.
    – YoungFrog
    Sep 25 '17 at 17:04
  • I think most code should handle it either way, but for example, if you cat a file without a final newline at the shell, the shell prompt will be printed together with the file's last line. And maybe some line-counting code will be off-by-1.
    – npostavs
    Sep 25 '17 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.