有没有一种方法可以阻止input()转义\ n? [英] Is there a way to stop input() from escaping \n?

查看:772
本文介绍了有没有一种方法可以阻止input()转义\ n?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图允许我的用户在字符串中输入\ n来指定新行. 但是由于某种原因,它被转换为"\\ n".所以我可以做:

So I'm trying to allow my user to input \n in a string, to designate a new line. However for some reason that is translated into '\\n'. So I can do:

inp = input().replace("\\n", "\n")

但这似乎有点麻烦;有更好的方法吗?

but that seems a little cumbersome; is there a better way?

推荐答案

您在此处查找的内容实际上与您要查找的内容相反.

What you're looking for here is actually almost the opposite of what you're asking for.

input不会以任何方式转义字符串.它会完全返回用户键入的字符串.

input does not escape the string in any way; it returns exactly the string the user typed.

而且,实际上,您尝试的解决方法虽然不是最佳解决方案,但也可以.

And, in fact, your attempted fix for that is, while probably not the best solution, fine.

我认为您的困惑是您将源代码中的字符串文字与实际的字符串值混合在一起:

I think your confusion is that you're mixing up the concept of string literals in source code with actual string values:

我的印象是,字符串中的\ n导致在打印该字符串时触发新行&例如\ n 考试\ nple"只是一个字符串而已.

I was under the impression that \n in a string leads to a new line being triggered when that string is printed & that the \n in e.g. "exam\nple" is just a string and nothing else.

否,字符串中的\n不会 not 导致在打印字符串时触发新行;只有换行符可以做到这一点.

No, \n in a string does not lead to a new line being triggered when the string is printed; only a newline character does that.

但是,源代码中的字符串文字中的\n不能为您提供字符串中的\n,它可以为您提供字符串中的换行符.

But \n in a string literal in your source code doesn't give you \n in the string, it gives you a newline character in the string.

当您写这篇文章时:

>>> spam = "exam\nple"

…Python将\n视为转义序列,并为您提供包含换行符的字符串值.所以:

… Python treats the \n as an escape sequence, and gives you a string value containing a newline character. So:

>>> print(spam)
exam
ple

该字符串中没有反斜杠和n,其中包含换行符.如果您有一个 did 带有反斜杠和n的字符串,它将打印出来.例如:

The string doesn't have a backslash and an n in it, it has a newline in it. If you had a string that did have a backslash and an n in it, it would print that. For example:

>>> spam = r"exam\nple"
>>> print(spam)
exam\nple
>>> spam = "exam" + chr(92) + "nple"
>>> print(spam)
exam\nple

这就是这里发生的情况:input再次为您提供了用户键入的字符,包括反斜杠和n.

And that's exactly what's happening here: input is, again, giving you exactly the characters the user typed, including a backslash and an n.

通过查看字符串的repr,您可能会更加困惑,因为repr不仅显示字符串,还显示了可以输入到Python中以获取字符串的字符串文字.字符串:

You can confuse yourself even more by looking at the repr of a string, because the repr doesn't just show you the string, it shows you a string literal that you could feed into Python to get back the string:

>>> spam = "exam\nple"
>>> print(spam)
exam
ple
>>> spam
'exam\nple'

所以,这解释了为什么它起作用:

So, that explains why this works:

inp = input().replace("\\n", "\n")

不是用户的字符串中包含\\n,而是源代码中的字符串文字"\\n"是反斜杠和n,它与用户输入中的内容匹配,而字符串文字"\n"在用户输入中您的源代码是换行符,这是您要用反斜杠和n替换的内容.

It's not that the user's string has \\n in it, it's that the string literal "\\n" in your source is a backslash and an n, which matches what's in the user's input, while the string literal "\n" in your source is a newline, which is what you want to replace the backslash and n with.

您要寻找的是允许用户手动对输入进行转义,就像您对源代码中的文字字符串进行操作一样,然后让您的代码以相同的方式应用这些转义序列Python确实将源代码中的字符串文字转换为字符串值.

What you're looking for is to allow the user to manually escape their input, in the same way you would do in a literal string in your source code, and then have your code apply those escape sequences, in the same way Python does to turn string literals in your source code into string values.

您可以通过手动处理转义序列来完成此操作,例如您的代码:

You can do that by manually processing escape sequences, like your code:

inp = input().replace("\\n", "\n")

但是,如果您想处理其他转义符,那将是很丑陋的.最简单的方法是应用unicode-escape编解码器,如下所示:

But that's ugly if you want to handle other escapes. The simplest way to do that is to apply the unicode-escape codec, like this:

inp = codecs.decode(input(), "unicode-escape")

现在,如果用户键入abc\ndef,则将其转义为abc,换行符和def.

Now, if the user types abc\ndef, that will be unescaped into abc, a newline, and def.

无论哪种方式,如果您想多次执行此操作,可以编写包装函数:

Either way, if you want to do this many times, you can write a wrapper function:

def unescaped_input(prompt=""):
    return codecs.decode(input(prompt), "unicode-escape")

…现在:

inp = unescaped_input()

这篇关于有没有一种方法可以阻止input()转义\ n?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆