[英]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.当数据保存到文件时,我需要像这样保存:each_item,student_grade 所以这可能是 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.只需使用它来将其放在新行上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.