我目前正在用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”开始。
提前谢谢!
发布于 2014-07-27 12:38:51
使用a追加到文件:
f1 = open('/var/log/combined/test.log', 'a+')
print >>f1, clips.PrintFacts()您正在使用w覆盖每一行。
发布于 2014-07-27 12:16:13
(这个答案是非常自然的,我不知道这是不是真的,我只是有一个想法。)
我认为它写了每一行,然后在上面写了另一行。您应该将所有内容保存到一个字符串中(string = string + clips.printFacts()),然后将其保存到文件中。
https://stackoverflow.com/questions/24980790
复制相似问题