将 CSV 导入组织模式属性

发布于 2024-08-27 14:24:20 字数 296 浏览 9 评论 0原文

我想将 CSV 导入组织模式。其他人已经询问过如何将 CSV 导入组织模式表。这不是我想做的。我需要将 CSV 导入到组织模式属性。

例如,像这样的 CSV:

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

应该变成:

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

你碰巧知道一种轻松的方法吗?

I would like to import a CSV into Org-mode. Others have already asked about importing CSV to Org-mode tables. That's not what I am trying to do. I need to import CSV to Org-mode properties.

For example, a CSV like this:

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 a painless way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静待花开 2024-09-03 14:24:20

我能看到的最简单的方法是将数据行标记为区域,然后使用正则表达式搜索和替换:

Mx replace-regexp RET \(.*\),\(.*\),\(.*\),\(.*\) RET :属性:< /code> Cq Cj :姓名:\1 Cq Cj :电话:\2 Cq Cj :手机:\3 Cq Cj :传真:\4 Cq Cj :结束: RET

如果您需要对许多具有不同标题和列数的可变 CSV 文件执行此操作,那么我可能会使用 键盘宏

user310031 的回答将为此奠定良好的基础。该宏可以将缓冲区缩小到每一行,在其上方插入标题行,执行 csv-transpose (这似乎需要 CSV 模式)执行搜索+替换,添加 :PROPERTIES::END: 行,加宽再次缓冲区,并将点保留在下一个数据行之前的行上。然后只需将剩余的数据行标记为区域,并输入 Cx Ck r

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

M-x replace-regexp RET \(.*\),\(.*\),\(.*\),\(.*\) RET :PROPERTIES: C-q C-j :Name: \1 C-q C-j :Tel: \2 C-q C-j :Mobile: \3 C-q C-j :Fax: \4 C-q C-j :END: RET

If you needed to do this for many variable CSV files with different headers and numbers of columns, then I would probably approach it with keyboard macros.

user310031's answer would make a good basis for that. The macro could narrow the buffer to each row, insert the header row above it, perform the csv-transpose (which appears to require CSV mode) do the search+replace, add in the :PROPERTIES: and :END: lines, widen the buffer again, and leave point on the line before the next data row. Then just mark the remaining data rows as the region, and type C-x C-k r.

七七 2024-09-03 14:24:20

使用 csv 模式,通过 csv 转置转置行和列,并使用替换正则表达式格式化:

搜索 \(.*\),\(.*\)

替换为: :\1 : \2

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

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

replace for: :\1: \2

掀纱窥君容 2024-09-03 14:24:20

你可以做这样的事情。你的例子不太清楚。如果有多行,它只会一遍又一遍地设置同一标头中的属性。您可能想要使用名称创建新标题,然后设置标题的属性。下面的代码适用于格式良好的 csv 文件。

(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))))

You can do something like this. Your example is not too clear. If there is more than one line, it will just set the properties in the same header over and over. you may want to use the Name to create a new heading, and then set properties on the heading. the code below works for pretty 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))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文