gpt4 book ai didi

emacs - 从文件读入Emacs Lisp列表

转载 作者:行者123 更新时间:2023-12-03 15:31:11 26 4
gpt4 key购买 nike

我有以下文件data.txt

A
B
C
D

我想将此文件的内容读入Lisp list中,例如
(defun read-list-from-file (fn)
(interactive)
(list "A" "B" "C" "D"))

(defun my-read ()
(interactive)
(let (( mylist (read-list-from-file "data.txt")))
(print mylist t)))

如何修改 read-list-from-file,使其返回相同的列表,但从作为输入参数 fn的文件中读取。.文件中的每一行都应是列表中的单独项目。

最佳答案

这段代码:

(with-current-buffer
(find-file-noselect "~/data.txt")
(split-string
(save-restriction
(widen)
(buffer-substring-no-properties
(point-min)
(point-max)))
"\n" t))

UPD:

这是 insert-file-contents的版本:

(defun slurp (f)
(with-temp-buffer
(insert-file-contents f)
(buffer-substring-no-properties
(point-min)
(point-max))))

(split-string
(slurp "~/data.txt") "\n" t)

关于emacs - 从文件读入Emacs Lisp列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20747190/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com