用 Common Lisp 就地替换文件中的正则表达式
Replace regex in a file, in-place, with Common Lisp
我正在尝试编写 Python 的正则表达式搜索和替换的 Common Lisp 版本,并对文件进行就地修改:
import fileinput, re
for line in fileinput.input(inplace=1, backup='.bak'):
line = re.sub(r"foo", "bar", line, re.M)
print (line)
这是我能想到的 Common Lisp 代码:
(require :cl-ppcre)
(defun in-place-subst (file)
(with-open-file (stream file :direction :io :if-exists :overwrite)
(loop for line = (read-line stream nil)
while line do
(write-line (cl-ppcre:regex-replace-all "foo" line "bar") stream))))
有点用。现在,替换文本将附加在文件末尾。我的直接问题是我不知道如何替换 内容。
为了更好地解释,如果 file.txt
包含:
1 foo
2 bar
3 foobar
调用后
(in-place-subst "file.txt")
我得到:
1 foo
2 bar
3 foobar
1 bar
2 bar
3 barbar
而不是正确的替换:
1 bar
2 bar
3 barbar
我尝试了所有可能的 with-open-file
选项(来自 Successful Lisp),但没有成功:
Keyword Value Action if File Exists
---------- ------------------ ---------------------------------------
:IF-EXISTS NIL return NIL
:IF-EXISTS :ERROR signal an error
:IF-EXISTS :NEW-VERSION next version (or error)
:IF-EXISTS :RENAME rename existing, create new
:IF-EXISTS :SUPERSEDE replace file upon CLOSE
:IF-EXISTS :RENAME-AND-DELETE rename and delete existing, create new
:IF-EXISTS :OVERWRITE reuse existing file (position at start)
:IF-EXISTS :APPEND reuse existing file (position at end)
有人可以向我发送正确的方向,以便函数以正确的方式呈现 file.txt
吗?
另外,假设 cl-ppcre
可用,Common Lisp 惯用的 方法是什么?
是否有更简洁的方法来使用 Common Lisp 进行就地正则表达式替换?
在Python中没有“就地”修改文件的原始操作;反而,
有一个助手 class fileinput
的功能,它给人一种错觉
通过首先将文件复制到备份文件来就地修改文件,然后
读取备份文件并将处理结果写入原始文件。来自 manual:
Optional in-place filtering: if the keyword argument inplace=1
is passed
to fileinput.input()
or to the FileInput
constructor, the file is moved
to a backup file and standard output is directed to the input file
(if a file of the same name as the backup file already exists,
it will be replaced silently).
This makes it possible to write a filter that rewrites its input file in place.
If the backup parameter is given (typically as backup='.'),
it specifies the extension for the backup file, and the backup file remains
around; by default, the extension is '.bak' and it is deleted when the output
file is closed. In-place filtering is disabled when standard input is read.
因此,在 Common Lisp 中执行此操作的方式是模仿
Python code, by first copying the file to a backup file, for instance using this functionmy-copy-file
,然后编写如下代码:
(defun in-place-subst (file)
(let ((backup-file (concatenate 'string file ".bak")))
(my-copy-file file backup-file)
(with-open-file (in-stream backup-file)
(with-open-file (out-stream file :direction :output :if-exists :supersede)
(loop for line = (read-line in-stream nil)
while line do
(write-line (cl-ppcre:regex-replace-all "foo" line "bar") out-stream))))))
我正在尝试编写 Python 的正则表达式搜索和替换的 Common Lisp 版本,并对文件进行就地修改:
import fileinput, re
for line in fileinput.input(inplace=1, backup='.bak'):
line = re.sub(r"foo", "bar", line, re.M)
print (line)
这是我能想到的 Common Lisp 代码:
(require :cl-ppcre)
(defun in-place-subst (file)
(with-open-file (stream file :direction :io :if-exists :overwrite)
(loop for line = (read-line stream nil)
while line do
(write-line (cl-ppcre:regex-replace-all "foo" line "bar") stream))))
有点用。现在,替换文本将附加在文件末尾。我的直接问题是我不知道如何替换 内容。
为了更好地解释,如果 file.txt
包含:
1 foo
2 bar
3 foobar
调用后
(in-place-subst "file.txt")
我得到:
1 foo
2 bar
3 foobar
1 bar
2 bar
3 barbar
而不是正确的替换:
1 bar
2 bar
3 barbar
我尝试了所有可能的 with-open-file
选项(来自 Successful Lisp),但没有成功:
Keyword Value Action if File Exists
---------- ------------------ ---------------------------------------
:IF-EXISTS NIL return NIL
:IF-EXISTS :ERROR signal an error
:IF-EXISTS :NEW-VERSION next version (or error)
:IF-EXISTS :RENAME rename existing, create new
:IF-EXISTS :SUPERSEDE replace file upon CLOSE
:IF-EXISTS :RENAME-AND-DELETE rename and delete existing, create new
:IF-EXISTS :OVERWRITE reuse existing file (position at start)
:IF-EXISTS :APPEND reuse existing file (position at end)
有人可以向我发送正确的方向,以便函数以正确的方式呈现 file.txt
吗?
另外,假设 cl-ppcre
可用,Common Lisp 惯用的 方法是什么?
是否有更简洁的方法来使用 Common Lisp 进行就地正则表达式替换?
在Python中没有“就地”修改文件的原始操作;反而,
有一个助手 class fileinput
的功能,它给人一种错觉
通过首先将文件复制到备份文件来就地修改文件,然后
读取备份文件并将处理结果写入原始文件。来自 manual:
Optional in-place filtering: if the keyword argument
inplace=1
is passed tofileinput.input()
or to theFileInput
constructor, the file is moved to a backup file and standard output is directed to the input file (if a file of the same name as the backup file already exists, it will be replaced silently). This makes it possible to write a filter that rewrites its input file in place. If the backup parameter is given (typically as backup='.'), it specifies the extension for the backup file, and the backup file remains around; by default, the extension is '.bak' and it is deleted when the output file is closed. In-place filtering is disabled when standard input is read.
因此,在 Common Lisp 中执行此操作的方式是模仿
Python code, by first copying the file to a backup file, for instance using this functionmy-copy-file
,然后编写如下代码:
(defun in-place-subst (file)
(let ((backup-file (concatenate 'string file ".bak")))
(my-copy-file file backup-file)
(with-open-file (in-stream backup-file)
(with-open-file (out-stream file :direction :output :if-exists :supersede)
(loop for line = (read-line in-stream nil)
while line do
(write-line (cl-ppcre:regex-replace-all "foo" line "bar") out-stream))))))