python写入文件自动换行

我们在使用python 写入文件时,会遇到用空格和换行的地方。

在python中我们会使用open()函数来打开文件,open()函数接受两个参数,第一个参数时文件的路径,第二个参数时打开文件的模式,

例如

with open("1.txt", mode="a", encoding='utf-8') as f:

我们在打开文件后,我们可以使用write()函数来将内容写入文件中,

例如

            f.write(na)

write()括号内是写入文件的内容

添加换行符:

 f.write('\n')

\n就是换行符

完整示例

        with open("1.txt", mode="a", encoding='utf-8') as f:
            f.write(na)
            f.write('\t')#空格
            f.write(hr)#写入的内容
            f.write('\n')#换行
            f.close()#关闭

你可能感兴趣的:(python,python,前端,html)