简体   繁体   中英

String extraction with single quote

I wish to detect a string with the below if statement, but it gives me the wrong output. I could not detect ARR . When I use below if statement, the output will be "wrong". It should go to pass when my input is ARR .

My data is in this way, I wish to edit my if-statement instead of editting the way I put my data.

['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\'' 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.

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:

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 you want to check MM[0] is either 'ARR' or 'ABC' or 'SAC', you need to use

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

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

returns True only if MM[0] is 'ARR'. If MM[0] is 'ABC', then if statement returns False and you will see 'wrong' printed.

You should use in for the search.

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.

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. 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

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' .

If you want to match ARR or ABC ...

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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