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

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

有没有一种方法可以在python中声明字符串变量,以使其中的所有内容都自动转义,或者具有其原义字符值?

不是问如何用斜杠将引号转义,这是显而易见的。我要的是一种通用的方式,使所有内容都以字符串文字形式显示,这样我就不必手动检查并转义非常大的字符串的所有内容。有人知道解决方案吗?谢谢!

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?

I’m not asking 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!


回答 0

原始字符串文字:

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

Raw string literals:

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

回答 1

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

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

If you’re dealing with very large strings, specifically multiline strings, be aware of the triple-quote syntax:

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

回答 2

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

仅使用原始字符串或多行字符串意味着不必担心太多事情。如果您使用原始字符串,则仍然必须在终端“ \”周围解决,并且使用任何字符串解决方案,都必须担心如果数据中包含了结束符“,’,’,’或”“” 。

也就是说,无法获取字符串

 '   ''' """  " \

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

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.

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.


回答 3

您可以在这里找到Python的字符串文字文档:

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

和这里:

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

最简单的示例将使用’r’前缀:

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

You will find Python’s string literal documentation here:

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

and here:

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

The simplest example would be using the ‘r’ prefix:

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

回答 4

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

为了解决安德鲁·达尔克(Andrew Dalke)指出的问题,只需在文本文件中键入文字字符串,然后使用它即可;

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

print input_string

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

 '   ''' """   \

虽然不是很有趣,也不是最佳选择,但它很有用,尤其是当您有3页需要转义字符的代码时。

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

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

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.


回答 5

如果string是变量,请使用。repr方法就可以了:

>>> s = '\tgherkin\n'

>>> s
'\tgherkin\n'

>>> print(s)
    gherkin

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

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

>>> s = '\tgherkin\n'

>>> s
'\tgherkin\n'

>>> print(s)
    gherkin

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