明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 573|回复: 8

[讨论] 有没有方便操作文档第几行的函数?

[复制链接]
发表于 2023-12-28 11:23 | 显示全部楼层 |阅读模式
是否除了 read-line和write-line ,就没了??、太麻烦了!!。。
发表于 2023-12-29 19:14 | 显示全部楼层
  1. ;; 多重方式 (ReadnumLines "11111.txt" '(3 7 21))
  2. (defun ReadNumLines (filename nlst / fn tx i tmp)
  3.   (setq fn (open filename "r")
  4.         tmp '()
  5.         i   1
  6.   )
  7.   (while (and nlst (setq tx (read-line fn)))
  8.     (if (member i nlst)
  9.       (setq tmp (cons tx tmp)
  10.             nlst (vl-remove i nlst)
  11.       )
  12.     )
  13.     (setq i (1+ i))
  14.   )
  15.   (close fn)
  16.   (reverse tmp)
  17. )
回复 支持 1 反对 0

使用道具 举报

发表于 2023-12-29 23:07 | 显示全部楼层
  1. (defun AllLines(filename / fn tx tmp)
  2.   (setq fn(open filename "r"))
  3.   (while(setq tx(read-line fn))
  4.     (setq tmp(cons tx tmp)))
  5.   (close fn)
  6.   (reverse tmp))
  7. (defun GetNUmsLine(nlst txts / lines)
  8.   (vl-every(function(lambda(x)(setq lines(cons(nth x txts)lines))))nlst)
  9.   (reverse lines))
  10. (defun Modlines(ntxts filename / txts fn old)
  11.   (setq txts(AllLines filename)
  12.         old(GetNUmsLine(mapcar(function car)ntxts)txts)
  13.         fn(open filename"w"))
  14.   (vl-every(function(lambda(a b)(setq txts(subst a(cadr b)txts))))old ntxts)
  15.   (write-line(substr(apply'strcat(mapcar(function(lambda(x)(strcat"\n"x)))txts))2)fn)
  16.   (close fn))
  17. (defun c:tt(/ filename);读取所有行
  18.   (if(setq filename(getfiled""""""4))
  19.     (AllLines filename)))
  20. (defun c:t1(/ filename nlst);读取指定行
  21.   (if(setq filename(getfiled""""""4))
  22.     (GetNUmsLine'(0 7 12)(AllLines filename))))
  23. (defun c:t2(/ filename ntxts);修改指定行
  24.   (if(setq filename(getfiled""""""4))
  25.     (Modlines'((0 "ABC")(7"ABCD")(12"ABCDE"))filename)))
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2023-12-28 14:36 | 显示全部楼层
liuhe 发表于 2023-12-28 12:04
太麻烦 说明你的文档不行,你用专业数据库储存数据,你又嫌弃学起来慢

想想也对,,,顺便弄了两个函数一个读取第几行,,一个写入第几行。。。
  1. (defun ReadnumLine (filename num / file line lines)  
  2.   (setq file (open filename "r"))  
  3.   (setq lines (list))  
  4.   (while (setq line (read-line file))  
  5.     (progn (setq lines (cons line lines)))  
  6.     (if (= (length lines) num) (setq line nil)))
  7.   (close file)  
  8.   (nth (- num 1) (reverse lines))
  9. )  
  10. ;(WritenumLine file_path  "11111" 3)
  11. (defun WritenumLine (filename text num / file line lines n)  
  12.   (setq file (open filename "r")) ; 打开文件进行读写  
  13.   (setq lines (list) n 0)  
  14.   (while (setq line (read-line file))  
  15.   (progn (setq lines (cons (cons n line) lines) n (1+ n)))  
  16.   )  
  17.   (if (>= (length lines) num)      ; 检查行数是否足够  
  18.     (progn  
  19.     (setq lines (subst (cons (- num 1) text) (assoc (- num 1) lines) lines)) ; 替换第3行
  20.     (setq file (open filename "w"))
  21.     (mapcar'(lambda (line) (write-line (cdr line) file)) (reverse lines)) ; 将修改后的行写回文件  
  22.     )  
  23.   (progn
  24.     (setq file (open filename "a"))
  25.     (repeat (- (- num 1) (length lines))
  26.       (write-line "" file)
  27.     )
  28.     (write-line text file)
  29.   )   
  30.   )  
  31.   (close file)  
  32. )

回复 支持 1 反对 0

使用道具 举报

发表于 2023-12-28 11:55 | 显示全部楼层
  1. ;; 加载 @lisp函数库
  2. (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)))))
  3. ;; 加载 当前用户文件夹下的 abc.txt 文件内容按每行内容分隔成表到 contents
  4. (setq contents (string:to-list (file:read-stream (strcat (getenv "userprofile") "\\" "abc.txt") "utf-8") "\n"))

  5. (princ (nth 3 contents));;输出文件的第2行内容
发表于 2023-12-28 12:04 | 显示全部楼层
太麻烦 说明你的文档不行,你用专业数据库储存数据,你又嫌弃学起来慢
发表于 2023-12-28 21:40 | 显示全部楼层
;; (ReadnumLine "11111.txt" 10)
(defun ReadnumLine (filename num / fn tx i)
  (setq fn (open filename "r")i  0)
  (while (and (< i num)(setq tx (read-line fn)))
    (setq i (1+ i))
  )
  (close fn)
  tx
)
 楼主| 发表于 2023-12-29 09:18 | 显示全部楼层
xyp1964 发表于 2023-12-28 21:40
;; (ReadnumLine "11111.txt" 10)
(defun ReadnumLine (filename num / fn tx i)
  (setq fn (open filen ...

谢谢院长,,!!!
发表于 2024-2-1 06:57 来自手机 | 显示全部楼层
本帖最后由 pxt2001 于 2024-2-1 06:59 编辑

这么多高手回复,楼主应该感激滴零。院长这个虽然很不错,但只读不写差点意思,读取多行实际意义不大,不止一个数据就用表,每行一个表,例如某一行内容:("文本框":((1 "距离系数" 1.1) (2 "图层名称" "文本外框")))
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-6-3 23:48 , Processed in 0.206206 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表