简体   繁体   English

单引号字符串提取

[英]String extraction with single quote

I wish to detect a string with the below if statement, but it gives me the wrong output. 我希望使用下面的if语句检测一个字符串,但是它给我错误的输出。 I could not detect ARR . 我无法检测到ARR When I use below if statement, the output will be "wrong". 当我在下面的if语句中使用时,输出将为“错误”。 It should go to pass when my input is ARR . 当我的输入是ARR时,它应该通过。

My data is in this way, I wish to edit my if-statement instead of editting the way I put my data. 我的数据就是这样,我希望编辑我的if语句,而不是编辑我放置数据的方式。

['TPA']
['TPI']
['ABC']


if MM[0] == ('\'ARR\'' or '\'ABC\'' or '\'SAC\''):
    pass
else:
    print('wrong')

That's because the result of following part is "'ARR'" : 这是因为以下部分的结果是"'ARR'"

>>> ('\'ARR\'' or '\'ABC\'' or '\'SAC\'')
"'ARR'"

Basically the or operator will return the left operand when both operands are True and here, since all the strings evaluated as True by python the result of your chained or operations is the first string. 基本上,当两个操作数均为True时, or运算符将返回左操作数,在这里,因为所有字符串都被python评估为True,所以链式or操作的结果是第一个字符串。

For getting ride of this problem and as a more pythonic approach you can simple check the membership with the in operator and a set which has O(1) complexity: 为了解决这个问题并采用更Python的方法,您可以使用in运算符和具有O(1)复杂性的set简单地检查成员资格:

if MM[0] in {"'ARR'", "'ABC'", "'SAC'"}

I think it not the single quote make the output of your code unexpected, it's because you use a wrong if statement. 我认为不是单引号会使您的代码输出意外,这是因为您使用了错误的if语句。

If you want to check MM[0] is either 'ARR' or 'ABC' or 'SAC', you need to use 如果要检查MM[0]是'ARR'还是'ABC'或'SAC',则需要使用

MM[0] == 'ARR' or MM[0] == 'ABC' or MM[0] == 'SAC'

or 要么

MM[0] in ('ARR', 'ABC', 'SAC')

Otherwise, ('ARR' or 'ABC' or 'SAC') is an expression which always return 'ARR', so 否则, ('ARR' or 'ABC' or 'SAC')是一个始终返回“ ARR”的表达式,因此

if MM[0]==('\'ARR\'' or '\'ABC\'' or '\'SAC\''):

returns True only if MM[0] is 'ARR'. 仅当MM[0]为'ARR'时返回True If MM[0] is 'ABC', then if statement returns False and you will see 'wrong' printed. 如果MM[0]为'ABC',则if语句返回False ,您将看到打印'错误'。

You should use in for the search. 您应该使用in进行搜索。

if MM[0] in {"'ARR'", "'ABC'", "'SAC'"}

Then, don't escape everywhere with backslashes, it's ugly. 然后,不要到处都有反斜杠,这很丑陋。 If you know your string has single quotes in it, delimit it with double quotes, it will be much more readable. 如果您知道您的字符串中包含单引号,请用双引号将其定界,那么它将更具可读性。

I think this should work 我认为这应该工作

MM[0] in ('ARR', 'ABC','SAC')

Cheers 干杯

You should put all valid choices in a list, and perform a list membership check like this: 您应该将所有有效选择放入列表中,并执行列表成员资格检查,如下所示:

wanted = r"'ARR'"

if wanted in [r"'ARR'", r"'ABC'", r"'SAC'"]:
    print("Given value was a member of the list.")

The r in front of the strings is to denote them as raw strings, which denote them as strings with different rules for escaping some literal values in them. 字符串前面的r表示它们为raw字符串,表示它们为具有不同规则的字符串,这些规则用于转义一些文字值。

MM=['ARR', 'ABC','SAC','WWW','ZZZ']


if MM[0] in ('ARR', 'ABC', 'SAC'):
    pass
else:
    print('wrong')

Works just fine. 效果很好。 You're overthinking it. 您想得太多了。 You don't have to specify MM is an array, as python can figure that out. 您不必指定MM是一个数组,因为python可以弄清楚这一点。 You don't have to escape the single quotes either. 您也不必转义单引号。 You aren't trying to match a string 'ARR' , you're trying to match ARR 您不是要匹配字符串'ARR' ,而是要匹配ARR

This works across the string you provided... are you trying to loop through them?: 这适用于您提供的字符串...您是否试图遍历它们?:

MM = ['ARR', 'ABC', 'SAC', 'WWW', 'ZZZ']
for i in MM:
    if i in ('ARR', 'ABC', 'SAC'):
        print "Winner %s" % i
    else:
        print('wrong')

gives

Winner ARR
Winner ABC
Winner SAC
wrong
wrong

\\' will make ' to be a part of a string. \\'将使'成为字符串的一部分。 ex: 例如:

>>> str1 = '\'demo_string\''
>>> str1
"'demo_string'"

The whole str1 is 'demo_string' . 整个str1'demo_string'

If you want to match ARR or ABC ... 如果要匹配ARRABC ...

data = 'ABC'
if data in ('ARR', 'ABC', 'SAC'):
    pass
else:
    print('wrong')

You can also use " to quote the string, ex: 您也可以使用"引号,例如:

if data in ("ARR", "ABC", "SAC"):
    pass
else:
    print('wrong')

If you want to match 'ABC' , please try: 如果要匹配'ABC' ,请尝试:

if data in ("'ARR'", "'ABC'", "'SAC'")
    pass
else:
    print('wrong')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM