简体   繁体   English

从一个文件读取并写入另一个文件

[英]Reading from a file and writing to another file

I have a question about a project I am working on.我对我正在从事的项目有疑问。 I tried to search answers for my questions on the inte.net, but I couldn't find any.我试图在 inte.net 上搜索我的问题的答案,但找不到任何答案。 So here I am.. I have a txt file with questions in it (underneath each other) and I want to ask these questions 1 by 1 to the user.所以我在这里.. 我有一个 txt 文件,里面有问题(在彼此下面),我想一一向用户提出这些问题。 When the question is asked, I want the user to give a input ("Y" or "N").提出问题时,我希望用户提供输入(“Y”或“N”)。 If the answer is "Y" or "N", I want to write the question that is asked and the input that is given, in another empty txt file.如果答案是“Y”或“N”,我想将提出的问题和给出的输入写在另一个空的 txt 文件中。 If the answer is neither a "Y" or "N", I want to print that the given input is not valid, and after the print statement, I want to ask the same question again.如果答案既不是“Y”也不是“N”,我想打印给定的输入无效,在打印语句之后,我想再次问同样的问题。

After the input, I want to repeat this process again, but with the next question, until I ran out of the questions I have in my txt file.输入后,我想再次重复这个过程,但是下一个问题,直到我用完我的 txt 文件中的问题。

I know that it is not much, but this is my code now:我知道它并不多,但现在这是我的代码:

def import_vragenlijst():

    with open ("vragen.txt", "r") as rf:
        lezen = rf.readline()
        print(lezen)
        # with open ("antwoorden_gebruiker.txt", "w") as wf:
        


def main():
    # naam()
    import_vragenlijst()
    
if __name__ == "__main__":
    main()

You want to break up the task into smaller parts that you can work on individually.你想把任务分解成更小的部分,你可以单独处理。

  • Read the question list from one file从一个文件中读取问题列表
  • Write the question/answer lines to another file将问题/答案行写入另一个文件
  • Present a question to the user向用户提出问题
  • Collect the user's answer收集用户的答案
  • Determine if the response is valid确定响应是否有效

Unless you anticipate having an enormous question list, consider reading the whole list into an array.除非您预计会有一个巨大的问题列表,否则请考虑将整个列表读入一个数组。 Likewise, consider writing your answers to an array before trying to write them out to the file, unless there is some reason they need to be written while a user is answering.同样,在尝试将答案写入文件之前考虑将答案写入数组,除非出于某种原因需要在用户回答时写入它们。

so you'd have something like this (not tested, don't assume it will work without some effort on your part):所以你会有这样的东西(未经测试,不要假设它会在你不付出努力的情况下工作):

def query_user(question):
   response = input(question)
   if is_valid(response):
      return response
   else:
      print(f'{response} is not valid, must by "Y" or "N"')
      # recur, asking the user again
      return query_user(question)
   
def is_valid(response):
  return response == "Y" or response == "N"

def main():
   # open up the file, then read all the questions into memory
   with open(question_filename, "r") as questions_file:
       questions = [question for question in questions_file.read()]
   
   # create an array of answers based on user input
   answers = [query_user(question) for question in questions]

   # open the answers file and write the questions and answers out
   with open(answered_questions_filename, "w") as answers_file:
       for question, answer in zip(questions, answers):
           answers_file.writeline(question)
           answers_file.writeline(answer)



声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从一个文件读取并在Python中写入另一个文件 - Reading from one file and writing into another in Python 从文件中读取特定行并在python中写入另一个文件 - Reading specific lines from a file and writing to another file in python 从文件中读取文本,然后写入另一个文件,并标记文本中的重复 - Reading text from a file, then writing to another file with repetitions in text marked 在python中读写文件 - Writing and Reading to/from a file in python 写入和读取JSON文件 - Writing To and Reading From JSON File 在 python 中写入文件和读取文件 - Writing File and Reading from File in python 从zip文件读取文件并将其写入另一个文件时遇到的问题 - Facing Problems when reading a file from a zip file and writing it to another file 在循环中读取多个文件并将每个文件的读取数据写入 Python 中的另一个文件 - Reading from many files in a loop and writing the read data from each file in another file in Python 用Python读取二进制文件并将其写入第三个文件中非零字节的另一个补丁中 - Reading binary file in Python and writing it into another patching by nonzero bytes from the third file 从一个文件读取性能,执行一个动作并写入另一个文件的性能折衷 - Performance Tradeoff Reading From One File, Perfoming An Action and Writing To Another File
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM