marc578 发表于 2022-7-6 10:34:37

将列表写入文件

嗨,谁能给我一个建议,把一个列表写进一个*里。txt文件?
 
(定义c:prueba4()
(setq a(entsel))
(setq b(entget(汽车a)))
(setq pt1(cdr(assoc 10 b)))
(setq f(打开“ppr.txt”“w”))
(写入行pt1 f)
(关闭f)
)
 
使用前面的代码,我得到了以下消息:
“错误的参数类型:stringp(39.198 28.5504 0.0)”

Tharwat 发表于 2022-7-6 10:39:51

问题在于使用(写线)的方式不正确。
 
它应该带有字符串,以便将数据写入文件。例如:
 

(write-line "Welcome to CADTutor." f)

 
试着用前一行替换您的PT1,它就会工作。
 
也许其他人会做点什么。我希望如此。
 
当做

MSasu 发表于 2022-7-6 10:42:26

不幸的是,这并不是OP想要的答案——他的目标是将一个点的坐标记录到一个文件中。为此,必须将列表转换为字符串。可以使用Gile的一个不错的函数来实现这一点:
 
;;; lST2STR
;;; Returns a string which is the concatenation of a list and aseparator
;;; Author: Gile
;;; Arguments
;;; str = the string
;;; sep = the separator pattern
(defun lst2str (lst sep)
(if (cadr lst)
   (strcat (vl-princ-to-string (car lst))
       sep
       (lst2str (cdr lst) sep)
   )
   (vl-princ-to-string (car lst))
)
)
 
使用方法如下:
(LST2STR PT1 "\t")
 
 
当做

David Bethel 发表于 2022-7-6 10:47:15

使用(prin1 pt1 f)将列表打印到文件中
 
将(写入行“f”)用于新行

muthu123 发表于 2022-7-6 10:48:44

我希望这是李先生贴出的最简单的方式。这对于我将字符串转换为列表以及将列表转换为字符串非常有用。
 

(defun L->Ptr (lst) (princ) (vl-string-trim "()" (vl-princ-to-string lst)))
(defun Ptr->L (ptr) (read (strcat "(" ptr ")")))

Lee Mac 发表于 2022-7-6 10:53:18

 
Muthu,正如我在过去向你们解释的那样,我从未打算将这些函数用于此目的。。。它们被构造为用于操纵DCL图块值。

muthu123 发表于 2022-7-6 10:54:46

 
亲爱的,
我同意了。但它也可以用于这一目的,而不是所有情况。
如果你能为这个特殊的目的罚款任何其他wat(从列表到字符串&从字符串到列表),当然这对我和其他人都会非常有帮助。
 
当做
穆图。

muthu123 发表于 2022-7-6 10:57:22

 
 
尊敬的msasu:,
很好。
你还有其他的程序把字符串转换成列表吗?
 
当做
穆图

marc578 发表于 2022-7-6 11:00:14

嗨,朋友们,我写了以下对我来说很好的文章,也许也能帮到你们:
 
****************************************************************
(setq a(entsel))
(setq b(entget(汽车a)))
(setq pt1(cdr(assoc 10 b)))
(setq pt2(cdr(assoc 11 b)))
(setq f(打开“C:\\Documents and Settings\\Marco\\Mis documentos\\AAA.txt”“w”)
;    *******************************************************************************
(setq ptx1(汽车pt1))
(setq pty1(cadr pt1))
(setq ptz1(caddr pt1))
;    *******************************************************************************
(setq ptx1(rtos ptx1 2 4));将数据转换为字符串
(setq pty1(rtos pty1 2 4))
(setq ptz1(rtos ptz1 2 4))
;    *******************************************************************************
(setq xyz1(strcat ptx1”、“pty1”、“ptz1))
;    *******************************************************************************
(写入线xyz1 f)
 
当做

Tharwat 发表于 2022-7-6 11:03:58

 
请将代码放入代码编辑器。阅读以下链接。
 
http://www.cadtutor.net/forum/showthread.php?9184-代码发布指南
 
谢谢
页: [1] 2
查看完整版本: 将列表写入文件