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 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!
我不是要问如何用斜线来逃避引号,这是显而易见的。我要求的是一个通用的方法,使字符串文字中的所有内容,以便我不必手动通过并转义非常大的字符串的一切。有人知道解决方案吗?谢谢!
5 个解决方案
#1
82
Raw string literals:
原始字符串文字:
>>> r'abc\dev\t'
'abc\\dev\\t'
#2
48
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."""
#3
6
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中使用类似“here documents”的东西,但是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字符串文字中,而不进行某种内部转义。
#4
2
(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页代码需要转义字符。
#5
2
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
#1
82
Raw string literals:
原始字符串文字:
>>> r'abc\dev\t'
'abc\\dev\\t'
#2
48
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."""
#3
6
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中使用类似“here documents”的东西,但是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字符串文字中,而不进行某种内部转义。
#4
2
(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页代码需要转义字符。
#5
2
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