gpt4 book ai didi

Python-识别字符串中的值

转载 作者:行者123 更新时间:2023-12-01 03:07:41 24 4
gpt4 key购买 nike

我有 56 种不同鱼类的列表(或者一组或元组,如果我选择的话)。我想通过 CSV 文件“扫描”来识别任何鱼类物种的出现。

我尝试过:

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)

但失败的原因是:

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

所以我正在寻找一种成功的方法。

可能发生的情况列表:

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

最佳答案

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)

关于Python-识别字符串中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43188339/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com