Python-为什么换行符是“\n”当我不希望它们被创建时被创建?
我正在使用 python 2.7 将字符串写入文本文档,
直到每行的最后才添加“\n”换行符,但是当我打开文本文档时,会自行创建换行符。 我做错了什么?
我的代码如下所示:
w.write(ID + " " + Name + " " + x + " " + y + " " + "\n")
w.write(ID2 + " " + Name2 + " " + x2 + " " + y2 + " " + "\n")
我的文本文档如下所示:
1----折线1----0.567
----0.837
2----折线2----0.928
----0.253
^^^那些是空格,不是破折号
I am writing strings to a text document using python 2.7
I am not adding then "\n" newline character until the very end of each line, yet when I open up the text document, newlines are being created on their own.
What am I doing wrong?
My code looks like this:
w.write(ID + " " + Name + " " + x + " " + y + " " + "\n")
w.write(ID2 + " " + Name2 + " " + x2 + " " + y2 + " " + "\n")
My text document looks like this:
1----Polyline1----0.567
----0.837
2----Polyline2----0.928
----0.253
^^^Those are spaces, not dashes
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您在阅读后忘记从
x
和x2
中删除换行符。另外,␣和␢。
Because you forgot to strip the newlines from
x
andx2
after reading them.Also, ␣ and ␢.