简体   繁体   English

如何每次在新行中将变量写入文件?

[英]How to write a variable to a file on a new line every time?

I am trying to put a variable in to a file and have it go on a new line every time. 我试图将变量放入文件中,并使其每次都换行。 For example, file.write(variablehere) 例如, file.write(variablehere)

This has already been asked with how to write it as a string like file.write(stringhere) and to do it that way, you just do "/n" but that doesn't work with variables for some reason. 已经有人问过如何将其编写为类似file.write(stringhere)这样的字符串,并以这种方式进行操作,您只需执行“ / n”,但由于某种原因,它不适用于变量。

I've tried this as: 我已经尝试过:

file.write(variable/n)
file.write('variable/n')
file.write(variable + '/n')

None of those worked. 这些都不起作用。 Thanks for the help! 谢谢您的帮助!

您正在使用正斜杠/n并且需要使用反斜杠\\n

You could, if you want skip the \\n business for your specific problem if you use the following 如果您想使用特定的问题跳过\\n业务,则可以使用以下命令

print(variable, file=somefilehandle)

The you can change the separators using sep parameter which defaults to " " and end which defaults to "\\n" . 您可以使用sep参数(默认为" "end (默认为"\\n"来更改分隔符。

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

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