Существует org-table-convert-region
(связанный с C-c |
), который может выполнить преобразование довольно просто. Единственная хитрость - указать ;
в качестве разделителя. Вы можете сделать это, вызвав его с правильным аргументом префикса - строка документа говорит:
(org-table-convert-region BEG0 END0 &optional SEPARATOR)
Convert region to a table.
The region goes from BEG0 to END0, but these borders will be moved
slightly, to make sure a beginning of line in the first line is included.
SEPARATOR specifies the field separator in the lines. It can have the
following values:
(4) Use the comma as a field separator
(16) Use a TAB as field separator
(64) Prompt for a regular expression as field separator
integer When a number, use that many spaces, or a TAB, as field separator
regexp When a regular expression, use it to match the separator
nil When nil, the command tries to be smart and figure out the
separator in the following way:
- when each line contains a TAB, assume TAB-separated material
- when each line contains a comma, assume CSV material
- else, assume one or more SPACE characters as separator.
Значение (64)
- это всего три C-u
подряд, поэтому процесс выглядит следующим образом:
- вставить файл CSV с помощью
C-x i
.
C-x C-x
для отметки вставленного содержимого в качестве активной области.
C-u C-u C-u C-c | ; RET
Что еще круче, если оставить пустую строку в CSV-файле между первой и остальными строками, то первая строка автоматически станет заголовком таблицы.
И вы также можете заключить его в блок кода:
#+begin_src elisp :var file="/tmp/foo.csv" :results raw
(defun csv-to-table (file)
(with-temp-buffer
(erase-buffer)
(insert-file file)
(org-table-convert-region (point-min) (point-max) ";")
(buffer-string)))
(csv-to-table file)
#+end_src
#+RESULTS:
| a | b | c |
|---+---+---|
| d | e | f |
| g | h | i |