Percent sign at the end of the output of python script [closed]

大兔子大兔子 提交于 2020-07-22 22:14:39

问题


Why is a percent sign at the end of the output of the python script?

$ 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()))

回答1:


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.

Possible solutions:

  1. Use print instead of sys.stdout.write
  2. Append a newline to the end of the output with + "\n"
  3. Add a print() to the end of your program


来源:https://stackoverflow.com/questions/36270945/percent-sign-at-the-end-of-the-output-of-python-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!