python脚本输出末尾的百分号 [英] Percent sign at the end of the output of python script
本文介绍了python脚本输出末尾的百分号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么在python脚本输出的末尾有一个百分号?
Why is a percent sign at the end of the output of the python script?
$ echo"TEST TEST" |修剪
TESTTEST%
$ echo "TEST TEST" | trim
TESTTEST%
#!/usr/bin/env python
import sys
if __name__ == "__main__":
for line in sys.stdin:
sys.stdout.write(''.join(line.split()))
推荐答案
您看到的%
实际上可能是您的shell提示符,而不是程序输出的一部分.您无需在输出后写新行,因此shell提示符出现在最后一条命令的输出末尾.
The %
you see there might actually be your shell prompt, and not part of your program output. You're not writing a new line after your output, so the shell prompt appears at the very end of the output of the last command.
可能的解决方案:
- 使用
print
代替sys.stdout.write
- 使用
+"\ n"
在输出末尾添加换行符 - 在程序末尾添加
print()
这篇关于python脚本输出末尾的百分号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文