-3

I want to read multiples lines from a .txt file. Each line represents a question that is going to be answered by a NLTK Python script, then the answer will be written in another .txt file. I succeeded to make this mechanism work, but only for one line (one question) in the Question.txt file (the file from where the questions are being taken).

As I see the scenario, I would like to read a line from the Question.txt file, then the script will answer and write the answer in the Answer.txt file. Then the second line will be read from the Question.txt file and so on.

myFile = open("Questions.txt", 'r')
user_response=str(myFile.splitlines())  # Convert the content of the txt file from list format to string format in order to be able to lower the characters later

#I also made this little implementation in order for the code not to run infinitely, so to count the number of lines :
numOfLines = len(user_response.splitlines())
numofLines -= 1
petezurich
  • 9,280
  • 9
  • 43
  • 57
MirceaSabau
  • 1
  • 1
  • 5
  • Does this answer your question? [How to read a file line-by-line into a list?](https://stackoverflow.com/questions/3277503/how-to-read-a-file-line-by-line-into-a-list) – Tomerikoo May 10 '20 at 07:05

2 Answers2

1

Python 3

What you have to do:

  1. Read all lines
  2. Calculate all the answers
  3. Write all answers back to the file

I assumed your method to answer a question is called answer_question(question):

# 1
with open("Questions.txt") as questions_file:
    questions = questions_file.read().splitlines()

# 2
answers = []
for question in questions:
    answer = answer_question(question)  # or use question.lower() if you want to 
    answers.append(answer)

# 3
with open("Answers.txt", mode="w") as answers_file:
    answers_file.write("\n".join(answers))
winklerrr
  • 13,026
  • 8
  • 71
  • 88
  • I really tried this method, but now it doesn't return anything at all. I also have some problems regarding the splitlines() method, because it is not available for the list type of variable, only for string. So I am trying to cast the data that is read from the file into string. – MirceaSabau Jun 26 '19 at 15:51
  • But I am still not able to read multiple lines from the text file. I have to say that I am reading the file and I store the questions in a variable. Would it be a better way to store the questions in an array and pass them one by one to the function that will return me the response ? If yes, then how can I do so ? – MirceaSabau Jun 26 '19 at 15:53
  • @MicreaSabau please provide your code in your question, so I can have a look at it. In my code `questions_file.read()` returns a string therefore `splitlines()` works as excepted. Do you maybe use the method `readlines()` insead of `read()`? Because `readlines()` returns a `list`. – winklerrr Jun 26 '19 at 16:09
  • @MirceaSabau if you already have your questions inside a list just use a `for-in` loop like in my code at position `#2` – winklerrr Jun 26 '19 at 16:11
1

In python, files are iterators, so in order to avoid big arrays stored in memory, your simple solution is:

with open("Questions.txt") as q_f, open("Answers.txt", 'w') as a_f:
    for question in q_f:
        answer = solve(question)
        a_f.write(answer+"\n")

Here you iterate the file, line by line, answer the question and save it in another file without saving big lists in memory

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61