我正在尝试使用就地修改文件来编写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进行就地正则表达式替换?
答案 0 :(得分:8)
在Python中,没有原始操作可以“就地”修改文件;代替,
有一个辅助类fileinput
的功能,给人一种幻觉
首先通过将文件复制到备份文件来修改文件,然后
读取备份文件并将处理结果写入原始文件。来自manual:
可选的就地过滤:如果传递关键字参数
inplace=1
到fileinput.input()
或FileInput
构造函数,文件被移动 到备份文件,标准输出定向到输入文件 (如果已存在与备份文件同名的文件, 它将被默默地替换)。 这使得编写一个可以重写其输入文件的过滤器成为可能。 如果给出了备份参数(通常为backup ='。'), 它指定备份文件的扩展名,并保留备份文件 周围;默认情况下,扩展程序是' .bak'并在输出时删除它 文件已关闭。读取标准输入时禁用就地过滤。
因此,在Common Lisp中执行此操作的方法是模仿
Python code,首先将文件复制到备份文件,例如使用此function my-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))))))