乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 88|回复: 10

[编程交流] Lisp从外部文件读取

[复制链接]

59

主题

327

帖子

268

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
295
发表于 2022-7-6 09:45:38 | 显示全部楼层 |阅读模式
今天就是这样的一天。。。我找不到问题。
 
我的目标:
读取外部文件并获取“列表列表”。
 
我的Lisp程序:
 
  1. (defun Test (/ MyFile MyOpenFile Line AllMyFile)
  2. (setq MyFile "C:\\MyExternalFile.txt")
  3. (if MyFile
  4.    (progn
  5.      (setq MyOpenFile (open MyFile "r"))
  6.      (setq AllMyFile nil)
  7.      (while
  8. (setq Line (read (read-line MyOpenFile)))
  9. (setq AllMyFile (append AllMyFile (list Line)))
  10.      )
  11.      (close MyOpenFile)
  12.    ) ;_progn
  13.    (alert "No external file found !!")
  14. )
  15. (princ)
  16. ) ;_defun

 
我的外部文件:
  1. ("aName" "anAdress1" "SomeOtherData1")
  2. ("bName" "anAdress2" "SomeOtherData2")
  3. ("cName" "anAdress3" "SomeOtherData3")
  4. ("dName" "anAdress4" "SomeOtherData4")

 
分析:
  1. I want to get this:
  2. (("aName" "anAdress1" "SomeOtherData1") ("bName" "anAdress2" "SomeOtherData2") ("cName" "anAdress3" "SomeOtherData3") ("dName" "anAdress4" "SomeOtherData4"))

 
但它返回的是:
  1. Command: (test)
  2. ; error: bad argument type: stringp nil

 
我知道这是因为它试图读取一个空行,但我认为它只会在该行不是零的情况下读取该行。
因此错误为“stringp nil”。
 
但话说回来,我做错了什么?这一定是个小问题。旁注:不知怎的,我怀疑这个部分”(读(读行…)因为省略第一个“read”确实有效。然而,它返回了一个充满“的列表,如果不进行解析,我将无法使用它”。
 
一如既往,我们非常感谢您的帮助。
回复

使用道具 举报

62

主题

466

帖子

404

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
310
发表于 2022-7-6 09:57:14 | 显示全部楼层
------外部文件从这里开始---------
1.
“测试”
“测试1”
 
2.
“测试2”
“测试3”
--------到此结束---------
 
(_读取“文件位置”)
将返回
((1(“testing”“testing1”))(2(“testing2”“testing3”))
 
 
  1. (defun _read ( filename / fname line lst temp newlist )
  2. (if (findfile filename)
  3.    (progn
  4.      (setq fname (open filename "r"))
  5.      (while (setq line (read-line fname))
  6.        (if (not (wcmatch line ""))
  7.          (setq lst (append (list line) lst))
  8.        )
  9.      )
  10.      (setq fname (close fname))
  11.      (if lst
  12.        (foreach x (mapcar 'read lst)
  13.          ;|Big thanks to Alanjt|;
  14.          (if (numberp x)
  15.            (setq temp (not (setq newlist (cons (list x temp) newlist))))
  16.            (setq temp (cons x temp))
  17.          )
  18.        )
  19.      )
  20.      (if newlist
  21.        newlist
  22.        (mapcar 'read lst)
  23.      )
  24.    )
  25. )
  26. )
回复

使用道具 举报

59

主题

327

帖子

268

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
295
发表于 2022-7-6 09:59:09 | 显示全部楼层
谢谢你的回复,丹中尉的腿,但我不知道你的意思
此外,我想了解为什么我的lisp在没有使用完全不同的lisp的情况下无法工作。
除非我的Lisp程序是完全错误的,当然。。。
回复

使用道具 举报

62

主题

466

帖子

404

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
310
发表于 2022-7-6 10:05:27 | 显示全部楼层
  1. (defun _Test ( MyFile / MyOpenFile Line AllMyFile)
  2. (if (findfile MyFile)
  3.    (progn
  4.      (setq MyOpenFile (open MyFile "r"))
  5.      (while
  6.        (setq Line (read-line MyOpenFile))
  7.        (if (not (wcmatch line ""))
  8.          (setq AllMyFile (append AllMyFile (list line)))
  9.        )
  10.      )
  11.      (close MyOpenFile)
  12.    )  
  13. )
  14. AllMyFile
  15. )

 
尝试
(\u Test“C:\\MyExternalFile.txt”)
回复

使用道具 举报

59

主题

327

帖子

268

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
295
发表于 2022-7-6 10:11:05 | 显示全部楼层
我尝试了这个例程,它返回以下内容:
 
  1. ("("dName" "anAdress4"
  2. "SomeOtherData4")" "("cName" "anAdress3" "SomeOtherData3")" "("bName"
  3. "anAdress2" "SomeOtherData2")" "("aName" "anAdress1"
  4. "SomeOtherData1")")

 
应该是这样的:
 
  1. (("dName" "anAdress4" "SomeOtherData4") ("cName" "anAdress3" "SomeOtherData3")("bName" "anAdress2" "SomeOtherData2") ("aName" "anAdress1" "SomeOtherData1"))

 
使用额外的“read”进行了尝试,但仍然出现以下错误:
  1. ; error: bad argument type: stringp nil

 
基本上代码可以工作,但当它到达外部文件中内容的末尾时,它崩溃了。代码
  1. (read(read-line MyOpenFile)))
无法处理nil或“”。。。。
 
即使它处于一个while循环中,也会崩溃。很明显,我错过了一件重要的事情,但是什么??
回复

使用道具 举报

62

主题

466

帖子

404

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
310
发表于 2022-7-6 10:16:34 | 显示全部楼层
抱歉,这么长时间才回复
 
 
read无法处理nil
简单测试。尝试
(读取零)
 
  1. (defun _Test ( MyFile / MyOpenFile Line AllMyFile newlist temp)
  2. (if (findfile MyFile)
  3.    (progn
  4.      (setq MyOpenFile (open MyFile "r"))
  5.      (while
  6.        (setq Line (read-line MyOpenFile))
  7.        (if (not (wcmatch line ""))
  8.          (setq AllMyFile (append (list line) AllMyFile ))
  9.        )
  10.      )
  11.      (close MyOpenFile)
  12.      (foreach x allmyfile
  13.        (if (wcmatch (strcase x) "*NAME*")
  14.          (setq temp (not (setq newlist (cons (cons x temp) newlist))))
  15.          (setq temp (cons x temp))
  16.        )
  17.      )
  18.    )  
  19. )
  20. (if newlist newlist allmyfile)
  21. )

 
(\u Test“C:\\MyExternalFile.txt”)
回复

使用道具 举报

59

主题

327

帖子

268

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
295
发表于 2022-7-6 10:27:10 | 显示全部楼层
没问题!目前我无法访问autocad,但我会尽快尝试代码。
谢谢你抽出时间。
 
通过电话发送的消息,抱歉出错。
回复

使用道具 举报

54

主题

3755

帖子

3583

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
438
发表于 2022-7-6 10:33:14 | 显示全部楼层
  1. (defun _read (file / r l)
  2. (if (and (findfile file) (setq file (open file "R")))
  3.    (progn (while (setq r (read-line file)) (setq l (cons (read r) l))) (close file) (reverse l))
  4. )
  5. )
回复

使用道具 举报

54

主题

3755

帖子

3583

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
438
发表于 2022-7-6 10:38:42 | 显示全部楼层
顺便说一句,(append(list))比(cons)慢得多,最后使用reverse。
回复

使用道具 举报

59

主题

327

帖子

268

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
295
发表于 2022-7-6 10:42:05 | 显示全部楼层
@丹的腿:
谢谢你的努力,我现在可以理解这个问题了,确实“read”不能处理nil,用(read nil)测试。从一开始,我对“阅读”有一些奇怪的感觉,但它带走了“符号”。太糟糕了,因为我离得很近,所以我自己无法到达那里。
 
 
@Alanjt:
谢谢你的代码,我不想使用其他方法,因为我想知道我的代码出了什么问题。但是在阅读了你的代码后,我惊讶地发现你的代码有多么短,多么容易理解。
 
 
我一直在读这个和这个,发现两者有些相同。至少如果你读了它,它看起来是一样的。那么为什么你说使用Cons比Append更快呢?当然,我毫不怀疑你是错的:-)
 
无论如何,我现在可以继续了。
谢谢你们俩!
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-6 23:09 , Processed in 0.536883 second(s), 83 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表