1

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'
        )
2
  • stocks is a tuple. You are looking for a tuple object in a string. Instead, you need a separate for loop to go through each string inside the overall tuple. Commented Apr 3, 2017 at 15:17
  • Have you considered changing stocks to a set - i.e. sstocks = set(stocks) Commented Apr 3, 2017 at 15:27

3 Answers 3

1
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)
Sign up to request clarification or add additional context in comments.

5 Comments

So I think it works, but just a quick follow up question: it doesn't print anything, I think because the stocks tuple is in all caps, is there a way to ignore upper/lower case? I know that exists in RegEx
Yes, you can update the condition to: any(stock.lower() in line.lower().strip() for stock in stocks)
It says: AttributeError: 'builtin_function_or_method' object has no attribute 'strip' does that mean strip() won't work on the file?
That's interesting, line.lower() should return a string, not a function. Are you sure the executing code contains the () after the .lower?
Yup that was the problem. Sorry for the dumb question, I'm a novice. I really appreciate the help :)
1

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):

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.