Python 2.7.15 在 Windows 中添加了一个新行到末尾,但不在 Linux 中。我该如何解决?

Python 2.7.15 adds a new line in Windows to the end but not on Linux. How do I fix this?

不是一个典型的"how do I strip a new line or spaces"问题...

总的来说,我是 python 的新手。但我知道

print ("test", end="")
print ("test", end="")

对于 Python 3 和

print "test",
print "test",

对于Python 2

Python 3 实现将在 Linux 和 Windows 机器上正确打印;然而 Python 2 实现将在基于 Windows 的机器上执行结束时添加一个额外的行(但不是 Linux,它打印正确)。有什么办法可以去掉这条新线吗?

我四处搜索,似乎找不到任何人谈论这个特定问题。这是演示的屏幕截图:

所以,按照print documentation

Standard output is defined as the file object named stdout

我们可能假设 python I\O 是系统相关的,所以我们可以尝试猜测这种情况的解释,甚至认为打印文档指出:

A '\n' character is written at the end, unless the print statement ends with a comma.

OR 原因是 Windows & Linux 威胁 print 声明不同(因为 print 是一个声明Python 2,function call 在 Python 3).

回到问题,如何去掉这一行:
我使用 future 语句来打印函数:

from __future__ import print_function

print('test', end=' ')
print('test', end='')

如果我找到任何合理的解释,我会更新答案(应该在某个地方!)。

在与一些人谈过这件事并做了一些研究之后。看来解决此问题的最直接方法是直接使用:

sys.stdout.write()

而不是打印。然后,您可以像 C/C++ 和 Java 的工作方式一样格式化您的输出。