theprowler
theprowler

Reputation: 3600

Python - recognize a value in a string

I have a list (or a set or tuple if I choose) of 56 different fish species. I would like to "scan" thru a CSV file to recognize any occurrence of any of the fish species.

I tried:

with open(file_path) as f:
    for line in f:
        if (stocks in line):
            fish, remainder = line.split('\t')
            print("fish:", fish)
            print("remainder:", remainder)

but that fails because of:

TypeError: 'in <string>' requires string as left operand, not tuple

So I am looking for a way for this to succeed.

The list of possible occurrences:

stocks = (
        'GB COD EAST',
        'GBE COD',
        'GB COD WEST',
        'GBW COD',
        'GOM COD',
        'GB HADDOCK EAST',
        'GBE HADD',
        'GB HADDOCK WEST',
        'GBW HADD',
        'GOM HADDOCK',
        'GOM HADD',
        'GOM HAD',
        'GOM HADOCK',
        'PLAICE',
        'DABS',
        'POLLOCK',
        'POLL',
        'REDFISH',
        'REDS',
        'RED',
        'WHITE HAKE' ,
        'WHITEHAKE',
        'WHAKE',
        'WHAK',
        'GB WINTER FLOUNDER',
        'GB BB',
        'GB WINTER',
        'GB BLACK BACKS',
        'GB BLACKBACKS',
        'GOM WINTER FLOUNDER',
        'GOM BLACKBACKS',
        'GOM BB',
        'GOM WINTER',
        'SNE WINTER FLOUNDER',
        'SNE WINTER',
        'SNE/MA WINTER FLOUNDER',
        'SNE BLACKBACK',
        'SNE BLACKBACKS',
        'SNE BB',
        'WITCH FLOUNDER',
        'WITCH',
        'WHICH',
        'WHITCH',
        'GREYSOLE',
        'GREY SOLE',
        'CC/GOM YELLOWTAIL FLOUNDER',
        'GOM YELLOWTAIL',
        'GOM YELLOW TAIL',
        'GOM YT',
        'GB YELLOWTAIL FLOUNDER',
        'GB YELLOWTAIL',
        'GB YT',
        'SNE/MA YELLOWTAIL FLOUNDER',
        'SNE YT',
        'SNE YELLOWTAIL',
        'SNE YELLOW TAIL',
        'SCALLOP IFQ'
        )

Upvotes: 1

Views: 99

Answers (3)

shadan
shadan

Reputation: 38

Python only allows you to use the in operator with a right operand of type string if the left operand is also of type string.

if (stocks in line):

stocks is a tuple and line is string. You could use the following method, assuming you want all the matches, including duplicates:

with open(file_path) as f: for line in f: matches = [x for x in stocks if x in line] print(matches)

Upvotes: 0

Elisha
Elisha

Reputation: 23770

with open(file_path) as f:
    for line in f:
        if any(stock.lower() in line.lower().strip() for stock in stocks):
            fish, remainder = line.split('\t')
            print("fish:", fish)
            print("remainder:", remainder)

Upvotes: 1

alecxe
alecxe

Reputation: 473763

stocks is a tuple, you cannot check if a tuple is in a string:

>>> ("a", "b") in "ab"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'in <string>' requires string as left operand, not tuple

You may use the built-in any() function instead:

if any(stock in line for stock in stocks):

Upvotes: 1

Related Questions