简体   繁体   中英

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?

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

Just use this to put it on a new line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM