svorgodne 发表于 2022-7-6 10:21:36

将文本导入变量Autolis

如何使用autolisp将TXT文件导入变量?
 
到目前为止,我已经能够做这样的事情:
 
(setq x(打开“C:/Documents and Settings/sgonzalez/My Documents/new_02.txt”)
 
但结果是:
 
 
命令:!x
#
 
而不是txt文件的真实内容。有人能帮我吗?像往常一样,这很紧急
 
谢谢

Se7en 发表于 2022-7-6 10:30:39

(defun iniread (str file)
;; iniread
;;
;; Returns all valid entries in a file. (see below)
;;
;; file to be read must be structured in a way that
;; all lines begining with a `` ; '' is considered
;; to be a comment.
;;
;; EX:
;; Example file: Example.ini
;;
;; ; this is an example ini file.
;; var1=val1
;; var2=val2
;; ; var3=val3
;; var4=val4
;;
;;      ( (lambda ( / f my-list )
;;          (setq f (open "c:\\Example.ini" "R"))
;;          (setq my-list
;;            (apply
;;                'append
;;                   (iniread (read-line f) f)))
;;          (close f)
;;          my-list ) )
;;   
;; => ("var1=val1" "var2=val2" "var4=val4")
;;
;; By: Se7en
;;
( (lambda (s f)
   (if s
       (cons
         (if (not (wcmatch s "*;*"))
         (list s))
         (iniread (read-line f) f))))
str
file ) )

BlackBox 发表于 2022-7-6 10:42:57

首先,请在帖子中使用代码标签。
 
打开文件时,请记住添加模式(读、写、附加),在这种情况下,您将使用以下方法:
 

(setq x (open "C:/Documents and Settings/sgonzalez/My Documents/new_02.txt" "R"))
 
 
 
尝试使用read line函数获取所需内容,然后记住始终对文本文件使用close函数。
 
希望这有帮助!

svorgodne 发表于 2022-7-6 10:55:33

谢谢你们俩。。无论如何,到目前为止,我只是忘记在前面的消息中添加模式,但我在编程中没有添加,因此最终结果仍然是:
 
 
命令:!x
#
 
显然,txt文件有一个列表,我想与图形中的其他信息进行比较。另一方面,据我所知,您的消息中唯一有效的编程是:
 
(defun iniread(str文件)
((λ(s f)
(如果s
(缺点
(if(not(wcmatch s“*;*”))
(列表)
(iniread(读取行f)f)))
str公司
文件)
 
我说得对吗?其余部分只是注释,当我运行“iniread”函数时,结果如下:
 
命令:(iniread)
; 错误:参数太少
 
 
还有其他想法吗?
再次提前感谢

Lee Mac 发表于 2022-7-6 11:02:42

为什么递归Se7en?当然,这会在堆栈上转储大量信息,如果文件大小足够,那么会很快被烧掉?
 

(defun _read ( filename / openfile line lst )
(cond
   ( (setq openfile (open filename "r"))

   (while (setq line (read-line openfile))
       (setq lst (cons line lst))
   )

   (close openfile)

   (reverse lst)
   )
)
)

svorgodne 发表于 2022-7-6 11:09:26

谢谢大家,我刚刚解决了这个问题,这里太简单了,这是我使用的代码
 
(setq x(打开“C:/Documents and Settings/sgonzalez/My Documents/new_02.txt”“r”))
(setq y(读取第x行))
(关闭x)
 
干杯

BlackBox 发表于 2022-7-6 11:21:33

 
好极了
 
我重申。。。
 

Se7en 发表于 2022-7-6 11:27:22

 
嗯,当然,我想可以,但我想需要一个很大的文件。此外,递归并不都是坏事*哈哈,这个过程可能使用递归,但它仍然非常快。举个例子,我把你的和我的放在一个有10000行的文件里,我就在你的尾巴上。
 
 
;;;;;;;;; --- P R O F I L E   L I S P --- ;;;;;;;;;;;
;;   by (C.) Vladimir Nesterovsky, December 1998.
;;   Free for *non-commercial* personal use only
;;         with this notice unchanged
------------------------------------------------------
TIMER_FOR_LM:READ: n=1, t=0.0100181, t/call=0.0100181.
TIMER_FOR_J7:READ: n=1, t=0.0119895, t/call=0.0119895.

Fastest abstraction: "LM:READ" With a time of: 0.0100181
------------------------------------------------------
...
------------------------------------------------------
TIMER_FOR_LM:READ: n=1, t=0.00901222, t/call=0.00901222.
TIMER_FOR_J7:READ: n=1, t=0.0109836, t/call=0.0109836.

Fastest abstraction: "LM:READ" With a time of: 0.0090122
------------------------------------------------------
页: [1]
查看完整版本: 将文本导入变量Autolis