user12410388
user12410388

Reputation: 7

In Python when writing to a file how can you make sure the data is saved on one line?

I have the following code:

with open("students2.txt", mode="w", encoding="utf-8") as my_file:
    for each_item in names:
         student_grade=input("Enter the grade for " +str(each_item))
         my_file.write(each_item+", "+student_grade)

When the data saves to the file I need it to save like this: each_item,student_grade so this might be Mike,78. Then the next one is saved on a new line

What have I missed?

Upvotes: 0

Views: 38

Answers (1)

Arundeep Chohan
Arundeep Chohan

Reputation: 9969

 my_file.write(each_item+", "+student_grade+"\n");

Just use this to put it on a new line.

Upvotes: 2

Related Questions