Import CSV to Org Properties

I would like to import CSV into Org mode. Others have already asked about importing CSV into Org mode tables. This is not what I'm trying to do. I need to import CSV into Org mode properties.

For example, CSV, for example:

Name,Tel,Mobile,Fax John,11111,22222,33333 

should become:

 :PROPERTIES: :Name: John :Tel: 11111 :Mobile: 22222 :Fax: 33333 :END: 

Do you happen to know how to do this painlessly?

+4
source share
3 answers

The easiest approach I see is to mark the data rows as a region, then use the search in the regular expression and replace:

> Mx replace-regexp RET \(.*\),\(.*\),\(.*\),\(.*\) RET :PROPERTIES: Cq Cj :Name: \1 Cq Cj :Tel: \2 Cq Cj :Mobile: \3 Cq Cj :Fax: \4 Cq Cj :END: RET

If you need to do this for many variables of CSV files with different headers and column numbers, I would probably approach it using keyboard macros .

user310031 answer will be a good basis for this. A macro can narrow the buffer to each line, insert a header line above it, execute csv-transpose (which requires CSV mode ) do a search + replace, add lines :PROPERTIES: and :END: expand the buffer again and leave a dot in the line before the next data string. Then simply mark the remaining data rows as a region and enter Cx Ck r .

+3
source

use csv-mode, transpose rows and columns csv-transpose and format with replace-regexp:

search \ (. * \), \ (. * \)

replace for:: \ 1: \ 2

+2
source

You can do something like this. Your example is not too clear. If there is more than one row, it will simply set the properties in the same header again and again. you can use the name to create a new title, and then set the properties in the title. The code below works for well formatted CSV files.

 (let ((lines (with-temp-buffer (insert-file-contents "data.csv") (split-string (buffer-string) "\n"))) (properties) (values)) (setq properties (split-string (car lines) ",")) (loop for line in (cdr lines) do (setq values (split-string line ",")) (loop for property in properties for value in values do (org-entry-put (point) property value)))) 
0
source

Source: https://habr.com/ru/post/1306070/


All Articles