从其他帖子中,我了解到“ \ n”表示添加到txt文件时的换行符。我正在尝试这样做,但是当属性恰好在新行之前时,我无法找出正确的语法。
我正在尝试的代码如下所示:
for item in list:
with open("file.txt", "w") as att_file:
att_file.write(variable\n)
您可能会看到,我正在尝试将列表中每个项目的变量添加到txt文件的新行中。正确的方法是什么?
最佳答案
您只需要将换行符指定为字符串:
例如:
with open("file.txt", "w") as att_file:
for item in list:
att_file.write(attribute + "\n")