How do EMACS Lisp programmers read text files for non-editing purposes? -
what emacs lisp programmers do, when want write equivalent of...
for line in open("foo.txt", "r", encoding="utf-8").readlines(): ...(split on ws , call fn, or whatever)...
..?
when in emacs lisp help, see functions opening files text editing buffers -- not intending. suppose write functions visit lines of file, if did that, wouldn't want user see it, , besides, doesn't seem efficient text-processing standpoint.
i think more direct translation of original python code follows:
(with-temp-buffer (insert-file-contents "foo.txt") (while (search-forward-regexp "\\(.*\\)\n?" nil t) ; line in (match-string 1) ))
i think with-temp-buffer
/insert-file-contents
preferable with-current-buffer
/find-file-noselect
, because former guarantees you're working fresh copy of entire file contents. latter construction, if happen have buffer visiting target file, buffer returned find-file-noselect
, if buffer has been narrowed, you'll see part of file when process it.
keep in mind may more convenient not process file line-by-line. example, expression returns list of sequences of consecutive digits in file:
(with-temp-buffer (insert-file-contents "foo.txt") (loop while (search-forward-regexp "[0-9]+" nil t) collect (match-string 0)))
(require 'cl)
first bring in loop
macro.
Comments
Post a Comment