python 防止python打印换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2484420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:45:35  来源:igfitidea点击:

Prevent python from printing newline

pythoninputnewlineuser-input

提问by wrongusername

I have this code in Python

我在 Python 中有这段代码

inputted = input("Enter in something: ")
print("Input is {0}, including the return".format(inputted))

that outputs

输出

Enter in something: something
Input is something
, including the return

I am not sure what is happening; if I use variables that don't depend on user input, I do not get the newline after formatting with the variable. I suspect Python might be taking in the newline as input when I hit return.

我不确定发生了什么;如果我使用不依赖于用户输入的变量,则在使用变量格式化后我不会得到换行符。我怀疑当我点击返回时,Python 可能会将换行符作为输入。

How can I make it so that the input does not include any newlines so that I may compare it to other strings/characters? (e.g. something == 'a')

我怎样才能使输入不包含任何换行符,以便我可以将它与其他字符串/字符进行比较?(例如something == 'a'

回答by Daniel G

You are correct - a newline is included in inputted. To remove it, you can just call strip("\r\n")to remove the newline from the end:

您是对的 - 中包含换行符inputted。要删除它,您只需调用strip("\r\n")从末尾删除换行符:

print("Input is {0}, including the return".format(inputted.strip("\r\n")))

This won't cause any issues if inputteddoes not have a newline at the end, but will remove any that are there, so you can use this whether inputtedis user input or not.

如果最后inputted没有换行符,这不会导致任何问题,但会删除那里的任何换行符,因此无论是否inputted为用户输入,您都可以使用它。

If you don't want anynewlines in the text at all, you can use inputted.replace("\r\n", "")to remove all newlines.

如果您根本不希望文本中出现任何换行符,则可以使用inputted.replace("\r\n", "")删除所有换行符。

回答by AndiDog

Your problem is actually Eclipse. Assuming that you use PyDev, I was able to reproduce the problem. When entering something in the Eclipse console, the problem occurs as described in your question. But when directly executing the very same script with the Python 3.1.1 interpreter, inputteddoes not include a newline character.

您的问题实际上是 Eclipse。假设您使用 PyDev,我能够重现该问题。在 Eclipse 控制台中输入内容时,问题如您的问题中所述。但是当使用 Python 3.1.1 解释器直接执行完全相同的脚本时,inputted不包含换行符。

I investigated the Python source code and found out input()uses GNU readlineif stdin is interactive (i.e. a TTY or prompt, however you want to call it), but falls back to the .readline()method of the stdin object if necessary. Then, if the result of readlineends with \n, that character is removed. Note: No CR-LF or LF-CR handling here (in the fallback case)!

我调查了 Python 源代码,发现如果 stdin 是交互式的(即 TTY 或提示,但您想调用它),则input()使用GNU readline,但.readline()在必要时回退到stdin 对象的方法。然后,如果结果以readline结尾\n,则删除该字符。注意:这里没有 CR-LF 或 LF-CR 处理(在后备情况下)!

So I wrote this little script to see what actually happens:

所以我写了这个小脚本来看看实际发生了什么:

import sys
from io import StringIO

for stdin in [sys.stdin, StringIO("test\r\ntest\r\n")]:
    sys.stdin = stdin 

    print("readline returns this: " + repr(sys.stdin.readline()))

    inputted = input("Enter in something: ")
    print("inputted: " + repr(inputted))

    print("inputted is printed like this: --> {0} <--".format(inputted))

It first executes the code with the normal stdin (console or Eclipse console) and then with a prepared stdin containing the text test\r\ntest\r\n.

它首先使用普通的标准输入(控制台或 Eclipse 控制台)执行代码,然后使用包含文本的准备好的标准输入test\r\ntest\r\n

Try and run the script in Eclipse - you must enter a string twice. The conclusion: Pressing Enter in the Eclipse console will produce CR-LF ("\r\n"). Printing "\r" in the Eclipse console will jump to the next line.

尝试在 Eclipse 中运行脚本 - 您必须输入两次字符串。结论:在 Eclipse 控制台中按 Enter 会产生 CR-LF ("\r\n")。在 Eclipse 控制台中打印“\r”将跳转到下一行。

On the other side, running it in the Windows console will produce the expected output: input()returns a string without a newline at the end because (I guess) GNU readline is used. With the prepared stdin StringIO("test\r\n"), the input()result is "test\r" as in Eclipse (although not printed as newline).

另一方面,在 Windows 控制台中运行它会产生预期的输出:input()在末尾返回一个没有换行符的字符串,因为(我猜)使用了 GNU readline。使用准备好的 stdin StringIO("test\r\n")input()结果是“test\r”,就像在 Eclipse 中一样(虽然没有打印为换行符)。

Hope this all makes sense... but what I still don't know is if that is expected behavior of Eclipse.

希望这一切都有意义……但我仍然不知道这是否是 Eclipse 的预期行为。

回答by evilpie

If you only want to stript the last line endings, you could use rstrip.
inputted.rstrip ("\r\n")

如果只想去掉最后一行的结尾,可以使用 rstrip。
inputted.rstrip ("\r\n")

回答by Jakob Borg

inputted = inputted.strip()

Edit:As noted, this will kill allwhitespace at the start and end. A way to get rid of only the trailing newline is:

编辑:如前所述,这将杀死开头和结尾的所有空白。只去掉尾随换行符的一种方法是:

import re
inputted = re.sub("[\n\r]+$", "", inputted)