简体   繁体   English

防止python打印换行符

[英]Prevent python from printing newline

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? 我怎样才能使输入不包含任何换行符,以便我可以将它与其他字符串/字符进行比较? (eg something == 'a' ) (例如something == 'a'

You are correct - a newline is included in inputted . 你是对的 - inputted包含换行符。 To remove it, you can just call strip("\\r\\n") to remove the newline from the end: 要删除它,您只需调用strip("\\r\\n")从末尾删除换行符:

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

This won't cause any issues if inputted does not have a newline at the end, but will remove any that are there, so you can use this whether inputted is user input or not. 如果inputted结尾没有换行符,这将不会导致任何问题,但会删除那里的任何内容,因此无论inputted是否为用户输入,您都可以使用它。

If you don't want any newlines in the text at all, you can use inputted.replace("\\r\\n", "") to remove all newlines. 如果您根本不想在文本中添加任何换行符,可以使用inputted.replace("\\r\\n", "")删除所有换行符。

Your problem is actually Eclipse. 你的问题实际上是Eclipse。 Assuming that you use PyDev, I was able to reproduce the problem. 假设您使用PyDev,我能够重现问题。 When entering something in the Eclipse console, the problem occurs as described in your question. 在Eclipse控制台中输入内容时,会出现问题中描述的问题。 But when directly executing the very same script with the Python 3.1.1 interpreter, inputted does not include a newline character. 但是当使用Python 3.1.1解释器直接执行相同的脚本时, inputted不包括换行符。

I investigated the Python source code and found out input() uses GNU readline if stdin is interactive (ie a TTY or prompt, however you want to call it), but falls back to the .readline() method of the stdin object if necessary. 我研究了Python源代码,发现如果stdin是交互式的(即TTY或提示符,但是你想调用它), input()使用GNU readline ,但如果需要,则回.readline() stdin对象的.readline()方法。 Then, if the result of readline ends with \\n , that character is removed. 然后,如果readline的结果以\\n结尾,则删除该字符。 Note: No CR-LF or LF-CR handling here (in the fallback case)! 注意:这里没有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 . 它首先使用普通的stdin(控制台或Eclipse控制台)执行代码,然后使用包含文本test\\r\\ntest\\r\\n的准备好的stdin执行。

Try and run the script in Eclipse - you must enter a string twice. 在Eclipse中尝试并运行脚本 - 您必须输入两次字符串。 The conclusion: Pressing Enter in the Eclipse console will produce CR-LF ("\\r\\n"). 结论: 在Eclipse控制台中按Enter键将生成CR-LF(“\\ r \\ n”)。 Printing "\\r" in the Eclipse console will jump to the next line. 在Eclipse控制台中打印“\\ r \\ n”将跳转到下一行。

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. 另一方面,在Windows控制台中运行它将产生预期的输出: input()返回一个没有换行符的字符串,因为(我猜)使用GNU readline。 With the prepared stdin StringIO("test\\r\\n") , the input() result is "test\\r" as in Eclipse (although not printed as newline). 使用准备好的stdin StringIO("test\\r\\n")input()结果与Eclipse中的StringIO("test\\r\\n") (尽管不是作为换行符打印)。

Hope this all makes sense... but what I still don't know is if that is expected behavior of Eclipse. 希望这一切都有意义......但我仍然不知道的是,这是否是Eclipse的预期行为。

如果您只想剥离最后一行结尾,则可以使用rstrip。
inputted.rstrip ("\\r\\n")

inputted = inputted.strip()

Edit: As noted, this will kill all whitespace at the start and end. 编辑:如上所述,这将在开始和结束时终止所有空格。 A way to get rid of only the trailing newline is: 一种摆脱只有尾随换行符的方法是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM