1

我目前正在使用 pyCLIPS 在 python 中编写程序。

clips 模块允许我简单地使用以下命令将多行输出打印到终端:clips.PrintFacts()

但是,我想将其输出到文件以保存结果。我正在使用以下代码:

def Print():    
    f1=open('/var/log/combined/test.log', 'a')
    print >>f1, '- Facts -\n'
    print >>f1, clips.PrintFacts()
    print >>f1, '\n- Rules -\n'
    print >>f1, clips.Print.Rules()

第一个和第三个打印命令成功地将它们的字符串打印到文件中,但第二个和第四个打印命令仍然只将剪辑结果输出到终端。下面是一个输出示例:

=============

root@ubuntu:/home/user/Desktop# python program.py
f-0    (initial-fact)
f-1    (duck)
f-2    (quack)
For a total of 3 facts.
MAIN:
Rule1
Rule2
Rule3
Rule4
Rule5
root@ubuntu:/home/user/Desktop# cat /var/log/combined/test.log
 - Facts -
None
 - Rules -
None
root@ubuntu:/home/user/Desktop#

=============

clips.PrintFacts()部分从“f-0”开始,而clips.PrintRules()从“MAIN”开始。

提前致谢!

4

2 回答 2

1

用于a附加到文件:

f1 = open('/var/log/combined/test.log', 'a+')
print >>f1, clips.PrintFacts()

您正在使用覆盖每一行w

于 2014-07-27T12:38:51.020 回答
-1

(这个回答很自发,不知道是不是真的,我只是有个想法。)

我认为它写了每一行,然后在上面写了另一行。您应该将所有内容保存到字符串 (string = string + clips.printFacts()),然后将其保存到文件中。

于 2014-07-27T12:16:13.550 回答