use*_*106 2 python command-line stdout
为什么python脚本输出的末尾有一个百分号?
$ echo "测试测试" | 修剪
TESTTEST%
#!/usr/bin/env python
import sys
if __name__ == "__main__":
for line in sys.stdin:
sys.stdout.write(''.join(line.split()))
Run Code Online (Sandbox Code Playgroud)
在%你看到有可能实际上是你的shell提示符,而不是你的程序输出的一部分。您没有在输出后写一个新行,所以 shell 提示出现在最后一个命令输出的最后。
可能的解决方案:
print代替sys.stdout.write+ "\n"print()在程序末尾添加一个