yaojing38 发表于 2023-12-28 11:23:09

有没有方便操作文档第几行的函数?

是否除了 read-line和write-line ,就没了??、太麻烦了!!。。

xyp1964 发表于 2023-12-29 19:14:51

;; 多重方式 (ReadnumLines "11111.txt" '(3 7 21))
(defun ReadNumLines (filename nlst / fn tx i tmp)
(setq fn (open filename "r")
      tmp '()
      i   1
)
(while (and nlst (setq tx (read-line fn)))
    (if (member i nlst)
      (setq tmp (cons tx tmp)
            nlst (vl-remove i nlst)
      )
    )
    (setq i (1+ i))
)
(close fn)
(reverse tmp)
)

llsheng_73 发表于 2023-12-29 23:07:46

(defun AllLines(filename / fn tx tmp)
(setq fn(open filename "r"))
(while(setq tx(read-line fn))
    (setq tmp(cons tx tmp)))
(close fn)
(reverse tmp))
(defun GetNUmsLine(nlst txts / lines)
(vl-every(function(lambda(x)(setq lines(cons(nth x txts)lines))))nlst)
(reverse lines))
(defun Modlines(ntxts filename / txts fn old)
(setq txts(AllLines filename)
        old(GetNUmsLine(mapcar(function car)ntxts)txts)
        fn(open filename"w"))
(vl-every(function(lambda(a b)(setq txts(subst a(cadr b)txts))))old ntxts)
(write-line(substr(apply'strcat(mapcar(function(lambda(x)(strcat"\n"x)))txts))2)fn)
(close fn))
(defun c:tt(/ filename);读取所有行
(if(setq filename(getfiled""""""4))
    (AllLines filename)))
(defun c:t1(/ filename nlst);读取指定行
(if(setq filename(getfiled""""""4))
    (GetNUmsLine'(0 7 12)(AllLines filename))))
(defun c:t2(/ filename ntxts);修改指定行
(if(setq filename(getfiled""""""4))
    (Modlines'((0 "ABC")(7"ABCD")(12"ABCDE"))filename)))

yaojing38 发表于 2023-12-28 14:36:49

liuhe 发表于 2023-12-28 12:04
太麻烦 说明你的文档不行,你用专业数据库储存数据,你又嫌弃学起来慢
想想也对,,,顺便弄了两个函数一个读取第几行,,一个写入第几行。。。
(defun ReadnumLine (filename num / file line lines)
(setq file (open filename "r"))
(setq lines (list))
(while (setq line (read-line file))
    (progn (setq lines (cons line lines)))
    (if (= (length lines) num) (setq line nil)))
(close file)
(nth (- num 1) (reverse lines))
)
;(WritenumLine file_path"11111" 3)
(defun WritenumLine (filename text num / file line lines n)
(setq file (open filename "r")) ; 打开文件进行读写
(setq lines (list) n 0)
(while (setq line (read-line file))
(progn (setq lines (cons (cons n line) lines) n (1+ n)))
)
(if (>= (length lines) num)      ; 检查行数是否足够
    (progn
    (setq lines (subst (cons (- num 1) text) (assoc (- num 1) lines) lines)) ; 替换第3行
    (setq file (open filename "w"))
    (mapcar'(lambda (line) (write-line (cdr line) file)) (reverse lines)) ; 将修改后的行写回文件
    )
(progn
    (setq file (open filename "a"))
    (repeat (- (- num 1) (length lines))
      (write-line "" file)
    )
    (write-line text file)
)   
)
(close file)
)

vitalgg 发表于 2023-12-28 11:55:15

;; 加载 @lisp函数库
(progn (VL-LOAD-COM) (SETQ S STRCAT H "http" O (vlax-create-object (S "win" H ".win" H "request.5.1")) V vlax-invoke E EVAL R READ) (V O (QUOTE OPEN) "get" (S H "://atlisp." "cn/cloud") :vlax-true) (V O (QUOTE SEND)) (V O (QUOTE WAITFORRESPONSE) 1000) (E (R (vlax-get O (QUOTE RESPONSETEXT)))))
;; 加载 当前用户文件夹下的 abc.txt 文件内容按每行内容分隔成表到 contents
(setq contents (string:to-list (file:read-stream (strcat (getenv "userprofile") "\\" "abc.txt") "utf-8") "\n"))

(princ (nth 3 contents));;输出文件的第2行内容

liuhe 发表于 2023-12-28 12:04:02

太麻烦 说明你的文档不行,你用专业数据库储存数据,你又嫌弃学起来慢

xyp1964 发表于 2023-12-28 21:40:03

;; (ReadnumLine "11111.txt" 10)
(defun ReadnumLine (filename num / fn tx i)
(setq fn (open filename "r")i0)
(while (and (< i num)(setq tx (read-line fn)))
    (setq i (1+ i))
)
(close fn)
tx
)

yaojing38 发表于 2023-12-29 09:18:34

xyp1964 发表于 2023-12-28 21:40
;; (ReadnumLine "11111.txt" 10)
(defun ReadnumLine (filename num / fn tx i)
(setq fn (open filen ...

谢谢院长,,!!!

pxt2001 发表于 2024-2-1 06:57:27

本帖最后由 pxt2001 于 2024-2-1 06:59 编辑

这么多高手回复,楼主应该感激滴零。院长这个虽然很不错,但只读不写差点意思,读取多行实际意义不大,不止一个数据就用表,每行一个表,例如某一行内容:("文本框":((1 "距离系数" 1.1) (2 "图层名称" "文本外框")))
页: [1]
查看完整版本: 有没有方便操作文档第几行的函数?