【问题标题】:Reading answers from separate file从单独的文件中读取答案
【发布时间】:2014-07-13 12:11:08
【问题描述】:

我被这段代码卡住了。 所以,我正在制作一个 IRC 机器人来回答人们的问题。当有人提出问题时,我将其写入 .txt 文件;

main.py

Import os
Import hi
Question_file = 'question.txt'
Answer_file = 'answer.txt'

while True:

  Question = raw_input ("Question?").lower()

  with open(Question_file, "w") as q:
                   q.write(Question.lower())
                   q.close()

  with open(Answer_file, "r") as m:
    answer = m.read()

  if 'hi' in Question:
     print ("hi")

  else:
    print answer

现在一切都好。问题在于 hi.py

嗨.py

Import os
Import string
Question_file = 'question.txt'
Answer_file = 'answer.txt'


with open (Question_file, "r") as f:
  question = f.read()

  if "what year is it" in question:
     with open(Answer_file, "w") as r:
       r.write("2014")
  if "what month is it" in question:
     with open(Answer_file, "w") as r:
       r.write("July")

问题是,Hi.Py 不会写出正确答案?它只写第一个问题的答案,并在每次提出问题时打印答案?

【问题讨论】:

  • 那些脚本甚至不应该运行 - Python 区分大小写。你的缩进搞砸了。还有其他几个问题,但除非您提供Minimal, Complete, Tested and Readable example,否则我认为回答这个问题没有任何意义。
  • 我知道我的压痕可能不是最好的,这就是我来寻求帮助的原因。
  • “不是最好的”不是这里的问题。由于大量的语法错误和一个(明显的)缩进错误,您发布的脚本甚至无法运行。运行这些脚本时,您不会得到您所描述的行为。
  • 让我快速测试一下。对此感到抱歉。
  • 另外,你为什么要标记 Python 3.x 和 Python-idle 这个问题?

标签: python python-2.7 python-idle python-import


【解决方案1】:

当你这样做时

import hi

文件hi.py 将被执行(一次),并且它的对象可以被导入程序的命名空间访问。

让我们看看hi.py做了什么:

with open (Question_file, "r") as f:   # Read entire file question.txt
  question = f.read()                  # into the variable "question"

  if "what year is it" in question:    # Look for this text;
     with open(Answer_file, "w") as r: # if found, *CREATE A NEW FILE* answers.txt
       r.write("2014")                 # and write "2014" to it.
  if "what month is it" in question:   # Same for the next text -
     with open(Answer_file, "w") as r: # if that's present, overwrite the file
       r.write("July")                 # with the text "July"

所以你可能想使用a 而不是w,所以其他单词不会覆盖之前的单词。我猜你还应该在每个单词后面写一些分隔符(\n)。

现在让我们看看main.py 做了什么(它执行了hi.py 之后什么也没做):

while True:
  Question = raw_input ("Question?").lower() # Get question from user

  with open(Question_file, "w") as q:        # Create a new file with the question
                   q.write(Question.lower()) # in lowercase
                   q.close()                 # no need for close() here!

  with open(Answer_file, "r") as m:          # Read the answer file
    answer = m.read()                        # regardless of what's in it

  if 'hi' in Question:
     print ("hi")                            # Python 3 syntax?

  else:
    print answer                             # Python 2 syntax?

这将一直持续到您点击 Ctrl-C

【讨论】:

  • 我希望它在每次提出问题时覆盖并加载新答案
  • 问题是 Main.py 只读取 answer.txt 一次并存储该答案,我需要它不断检查新答案
  • 不,它会在每个循环中读取 answer.txt,并且不会在任何地方存储任何内容。 hi.py 只执行一次,这是唯一写入 answer.txt 的程序。
  • 可以让hi.py循环运行吗?
  • imports 只运行一次。但是您可以将 hi.py 中的代码移动到一个函数中并多次调用它。这也意味着它只会在您调用函数时执行,而不是在导入期间执行。您似乎在为一些非常基本的 Python 概念而苦苦挣扎。你读过Python tutorial,尤其是关于导入模块的部分吗?
猜你喜欢
  • 2017-04-15
  • 1970-01-01
  • 2021-12-09
  • 1970-01-01
  • 2015-02-09
  • 2023-03-20
  • 2011-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多