elisp: read file into list of lists

久未见 提交于 2019-12-11 10:23:59

问题


I need to read a file contents into a two-dimensional list, split by newlines and spaces. For example,

a b
c d

needs to become

(list (list "a" "b") (list "c" "d"))

Currently I only know how to read the contents into a simple list determined by newlines. Whenever I need to use an element from that list, I have to split it by spaces everytime, but preferably this should be done only once beforehand.


回答1:


Like this:

(with-current-buffer (find-file-noselect "~/foo")
  (mapcar (lambda (x) (split-string x " " t))
          (split-string
           (buffer-substring-no-properties (point-min) (point-max))
           "\n"))) 



回答2:


While abo-abo's answer above is fine, it creates a temporary string with the full contents of the file, which is inefficient. If the file is very large, it is better to walk a buffer collecting data line-by-line:

(defun file-to-matrix (filename)
  (with-temp-buffer
    (insert-file-contents filename)
    (let ((list '()))
      (while (not (eobp))
        (let ((beg (point)))
          (move-end-of-line nil)
          (push (split-string (buffer-substring beg (point)) " ") list)
          (forward-char)))
      (nreverse list))))

Note the use of with-temp-buffer, which avoids leaving a buffer lying around, and the use of insert-file-contents, which avoids interfering with any other buffer that might be visiting the same file.




回答3:


With dash, s and f third-party libraries:

(--map (s-split " " it) (s-lines (s-chomp (f-read "FILE.TXT"))))

or:

(->> "FILE.TXT" f-read s-chomp s-lines (--map (s-split " " it)))

which are the same thing.



来源:https://stackoverflow.com/questions/28043763/elisp-read-file-into-list-of-lists

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!