在编写文件时,如何在Python中指定换行符? [英] How do I specify new lines on Python, when writing on files?

查看:254
本文介绍了在编写文件时,如何在Python中指定换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与Java(以字符串形式)相比,您将执行类似"First Line\r\nSecond Line"的操作.

In comparison to Java (in a string), you would do something like "First Line\r\nSecond Line".

那么,为了在常规文件中写多行,您将如何在Python中做到这一点?

So how would you do that in Python, for purposes of writing multiple lines to a regular file?

推荐答案

这取决于您要达到的正确程度. \n通常可以完成工作.如果您确实想解决问题,请在 os包中查找换行符. (实际上称为linesep.)

It depends on how correct you want to be. \n will usually do the job. If you really want to get it right, you look up the newline character in the os package. (It's actually called linesep.)

注意:使用Python API写入文件时,请勿使用os.linesep.只需使用\n; Python会自动将其转换为适合您平台的换行符.

Note: when writing to files using the Python API, do not use the os.linesep. Just use \n; Python automatically translates that to the proper newline character for your platform.

这篇关于在编写文件时,如何在Python中指定换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆