Emacs:将 CSV 导入组织模式

发布于 2024-07-30 13:50:24 字数 246 浏览 4 评论 0原文

Emacs 有一个非常好的扩展,名为 org-mode。

我希望能够轻松地将 CSV 文件加载到组织模式中,而不会造成太大的麻烦。 我所能找到的只是表导入或表捕获,简单地说,它们甚至不能很好地工作。

请注意,我的问题的一部分是其中包含逗号的文本字符串。 1,2,3,4 与 1,2,"3,4" 不同。

是否有一个函数或一个 perl 脚本可以运行将 csv 文件转换为组织模式格式?

谢谢!

Emacs has a very nice extension by the name of org-mode.

I would like to be able to easily load CSV files into org-mode without significant grief. All I've been able to find is table-import or table-capture, which, simply put, don't work even approximately well.

Note that part of my issue is text strings with a comma within them. 1,2,3,4 is different than 1,2,"3,4".

Is there a function out there or a perl script that one could run to transform a csv file into org-mode format?

Thanks!

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

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

发布评论

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

评论(5

极度宠爱 2024-08-06 13:50:24

来自组织模式手册:

抄送| 将活动区域转换为
桌子。 如果每行至少包含
一个 TAB 字符,功能
假设材料是选项卡
分开了。 如果每行包含一个
逗号、逗号分隔值 (CSV)
被假设。 如果没有,线路将被分割
在空白处进入字段。 您可以使用
强制特定的前缀参数
分隔符:Cu 力 CSV、Cu Cu
强制 TAB 和数字参数 N
表示至少连续N个
空格,或者制表符将是
分隔符。 如果没有活跃的
区域,此命令创建一个空的
组织表。

因此,只需将数据粘贴到组织文件中,选择它,然后执行 Cu Cc |

From the org-mode manual:

C-c | Convert the active region to
table. If every line contains at least
one TAB character, the function
assumes that the material is tab
separated. If every line contains a
comma, comma-separated values (CSV)
are assumed. If not, lines are split
at whitespace into fields. You can use
a prefix argument to force a specific
separator: C-u forces CSV, C-u C-u
forces TAB, and a numeric argument N
indicates that at least N consecutive
spaces, or alternatively a TAB will be
the separator. If there is no active
region, this command creates an empty
Org table.

So just paste the data into an org file, select it, and do C-u C-c | .

是你 2024-08-06 13:50:24

看一下:

Ch f org-table-convert-region

我总是转换 csv,所以我将其添加到我的 .emacs 中。

(defun org-convert-csv-table (beg end)
  (interactive (list (mark) (point)))
  (org-table-convert-region beg end ",")
  )

(add-hook 'org-mode-hook
      (lambda ()
    (define-key org-mode-map (kbd "<f6>") 'org-convert-csv-table)))

更新

这是另一个也考虑引用逗号的函数:

 a,"12,12",b --> a | 12,12 |b

随意改进它:-)。

(defun org-convert-csv-table (beg end)
  ; convert csv to org-table considering "12,12"
  (interactive (list (point) (mark)))
  (replace-regexp "\\(^\\)\\|\\(\".*?\"\\)\\|," (quote (replace-eval-replacement
                            replace-quote (cond ((equal "^" (match-string 1)) "|")
                                                   ((equal "," (match-string 0)) "|")
                                                   ((match-string 2))) ))  nil  beg end)

Have a look at:

C-h f org-table-convert-region

I'm always converting csv so I added this to my .emacs.

(defun org-convert-csv-table (beg end)
  (interactive (list (mark) (point)))
  (org-table-convert-region beg end ",")
  )

(add-hook 'org-mode-hook
      (lambda ()
    (define-key org-mode-map (kbd "<f6>") 'org-convert-csv-table)))

update

Here is another function that considers the quoted commas as well :

 a,"12,12",b --> a | 12,12 |b

feel free to improve it :-).

(defun org-convert-csv-table (beg end)
  ; convert csv to org-table considering "12,12"
  (interactive (list (point) (mark)))
  (replace-regexp "\\(^\\)\\|\\(\".*?\"\\)\\|," (quote (replace-eval-replacement
                            replace-quote (cond ((equal "^" (match-string 1)) "|")
                                                   ((equal "," (match-string 0)) "|")
                                                   ((match-string 2))) ))  nil  beg end)
谈下烟灰 2024-08-06 13:50:24

我假设您想将 CSV 专门转换为组织模式表格。 如果情况并非如此,您可能需要更明确地了解问题中的输出格式。

像这样的事情应该可以做到,或者至少给你一个可以破解的起点:

  #!/usr/bin/perl

  use strict;
  use warnings;

  use Text::CSV;

  my $csv = Text::CSV->new();

  while ( my $line = <DATA> ) {
    if ( $csv->parse( $line )) {
      my $str = join '|' , $csv->fields();
      print "|$str|\n";
    }
  }


  __DATA__
  1,2,3,4
  1,2,"3,4"

I'm assuming you want to convert your CSV specifically into org-mode tables. If that's not the case, you may want to be more explicit about output format in your question.

Something like this should do it, or at least get you a starting point you can hack on:

  #!/usr/bin/perl

  use strict;
  use warnings;

  use Text::CSV;

  my $csv = Text::CSV->new();

  while ( my $line = <DATA> ) {
    if ( $csv->parse( $line )) {
      my $str = join '|' , $csv->fields();
      print "|$str|\n";
    }
  }


  __DATA__
  1,2,3,4
  1,2,"3,4"
别再吹冷风 2024-08-06 13:50:24

尝试这个:

;; Insert a file and convert it to an org table
(defun aleblanc/insert-file-as-org-table (filename)
  "Insert a file into the current buffer at point, and convert it to an org table."
  (interactive (list (ido-read-file-name "csv file: ")))
  (let* ((start (point))
    (end (+ start (nth 1 (insert-file-contents filename)))))
    (org-table-convert-region start end)
    ))

Try this:

;; Insert a file and convert it to an org table
(defun aleblanc/insert-file-as-org-table (filename)
  "Insert a file into the current buffer at point, and convert it to an org table."
  (interactive (list (ido-read-file-name "csv file: ")))
  (let* ((start (point))
    (end (+ start (nth 1 (insert-file-contents filename)))))
    (org-table-convert-region start end)
    ))
倾`听者〃 2024-08-06 13:50:24

这是一个黑客工作,但它有效。

导出 CSV 文件时,请在每个条目周围加引号,然后将所有 "," 替换为竖线。

最后,我制作了一个执行类似以下操作的宏:(

C-a    ; Beginning-of-line
|      ; Self-insert-char
C-e    ; end-of-line
|      ; Self-insert-char
<down> ; Down one line

我不能 100% 确定 | 是否是自插入字符,因此最好记录您自己的宏)

点击表格中间某处的选项卡,并且 org-mode 正确格式化它。 我发现在狭窄的区域内更容易做到这一点。

警告:如果您的输入中有竖线……它可能无法正常工作。 类似的情况应该很容易发现,并且相对容易修复。

一般来说,当将类似文本的内容导入组织模式时,我发现一些智能宏编写和搜索替换的组合是

我希望有帮助的最简单的方法。

Here is a bit of a hack-job, but it works.

when you export the CSV file, force quotes around each entry, then replace all "," as a vertical bar.

Finally, I make a macro that does something like this:

C-a    ; Beginning-of-line
|      ; Self-insert-char
C-e    ; end-of-line
|      ; Self-insert-char
<down> ; Down one line

(I am not 100% sure if | is a self-insert-char or not, so its best to record your own macro)

Hit tab somewhere in the middle of the table, and org-mode formats it properly. I find this easier to do in a narrowed region.

Caveat: If you have a vertical bar in your input.. it probably won't work quite right. Situations like that should be easy to spot, and relatively easy to fix.

Generally, when importing something text-like into org-mode, I have found a combination of some smart macro writing, and search-replace to be the easiest way

I hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文