开发者

simple string question - the Single quotation marks and Double quotation marks inside the string just make the string into several parts

开发者 https://www.devze.com 2023-04-05 18:48 出处:网络
I need to use a long string for testing regular expression. However, the test string is always altered by the inside quotation marks, which leads to the whole string seperetaed into several parts and

I need to use a long string for testing regular expression. However, the test string is always altered by the inside quotation marks, which leads to the whole string seperetaed into several parts and some of them are not included into the string, and thus error occurred.

    str1=r"row Id="7" PostType开发者_如何学编程Id="2" ParentId="4" \n
         CreationDate="2008-07-31T22:17:57.883"\n
         Score="49" ViewCount="0" Body="<p>An explicit cast to double i"  

Would you pleae tell me how to tackle this? Thanks a lot.


Use a multiline string:

str1 = r"""row Id="7" PostTypeId="2" ParentId="4"
           CreationDate="2008-07-31T22:17:57.883"
           Score="49" ViewCount="0" Body="<p>An explicit cast to double i"""

As long as your string doesn't span multiple lines, you can also use an alternative quote character:

str1 = r'row Id="7" PostTypeId="2" ParentId="4"'

or (if you have both kinds of quotes inside your string) escape the quotes (but, as Ned Batchelder noted, then you can't use a raw string anymore):

str1 = 'row Id="7" PostTypeId=\'2\' ParentId="4"'
0

精彩评论

暂无评论...
验证码 换一张
取 消