如何在python中编写字符串文字而不必转义它们?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4703516/
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-08-18 17:03:38  来源:igfitidea点击:

How to write string literals in python without having to escape them?

pythonstringescaping

提问by kjakeb

Is there a way to declare a string variable in python such that everything inside of it is automatically escaped, or has its literal character value?

有没有办法在 python 中声明一个字符串变量,这样它里面的所有东西都会自动转义,或者有它的文字字符值?

I'm notasking how to escape the quotes with slashes, that's obvious. What I'm asking for is a general purpose way for making everything in a string literal so that I don't have to manually go through and escape everything for very large strings. Anyone know of a solution? Thanks!

不是在问如何用斜杠转义引号,这很明显。我要的是一种将所有内容都放入字符串文字中的通用方法,这样我就不必手动通过并转义非常大的字符串的所有内容。有人知道解决方案吗?谢谢!

采纳答案by SilentGhost

Raw string literals:

原始字符串文字:

>>> r'abc\dev\t'
'abc\dev\t'

回答by Greg Hewgill

If you're dealing with very large strings, specifically multiline strings, be aware of the triple-quotesyntax:

如果您正在处理非常大的字符串,特别是多行字符串,请注意三引号语法:

a = r"""This is a multiline string
with more than one line
in the source code."""

回答by ?s??o?

You will find Python's string literal documentation here:

您将在此处找到 Python 的字符串文字文档:

http://docs.python.org/tutorial/introduction.html#strings

http://docs.python.org/tutorial/introduction.html#strings

and here:

和这里:

http://docs.python.org/reference/lexical_analysis.html#literals

http://docs.python.org/reference/lexical_analysis.html#literals

The simplest example would be using the 'r' prefix:

最简单的例子是使用 'r' 前缀:

ss = r'Hello\nWorld'
print(ss)
Hello\nWorld

回答by Andrew Dalke

There is no such thing. It looks like you want something like "here documents" in Perl and the shells, but Python doesn't have that.

哪有这回事。看起来您想要 Perl 和 shell 中的“此处文档”之类的东西,但 Python 没有。

Using raw strings or multiline strings only means that there are fewer things to worry about. If you use a raw string then you still have to work around a terminal "\" and with any string solution you'll have to worry about the closing ", ', ''' or """ if it is included in your data.

使用原始字符串或多行字符串仅意味着需要担心的事情更少。如果您使用原始字符串,那么您仍然必须解决终端“\”,并且使用任何字符串解决方案,您都必须担心关闭“,”,“”或“””如果它包含在您的数据中.

That is, there's no way to have the string

也就是说,没有办法让字符串

 '   ''' """  " \

properly stored in any Python string literal without internal escaping of some sort.

正确存储在任何 Python 字符串文字中,而无需进行某种内部转义。

回答by Jeff Hykin

(Assuming you are not required to input the string from directly within Python code)

(假设您不需要直接从 Python 代码中输入字符串)

to get around the Issue Andrew Dalke pointed out, simply type the literal string into a text file and then use this;

要解决 Andrew Dalke 指出的问题,只需将文字字符串键入文本文件,然后使用它;

input_ = '/directory_of_text_file/your_text_file.txt' 
input_open   = open(input_,'r+')
input_string = input_open.read()

print input_string

This will print the literal text of whatever is in the text file, even if it is;

这将打印文本文件中任何内容的文字文本,即使它是;

 '   ''' """  “ \

Not fun or optimal, but can be useful, especially if you have 3 pages of code that would've needed character escaping.

不好玩或不理想,但可能很有用,尤其是当您有 3 页需要字符转义的代码时。

回答by markling

if string is a variable, use the .reprmethod on it:

如果字符串是变量,则使用 . 关于它的repr方法:

>>> s = '\tgherkin\n'

>>> s
'\tgherkin\n'

>>> print(s)
    gherkin

>>> print(s.__repr__())
'\tgherkin\n'