我有一個56種不同魚種的列表(或者如果我選擇,可以是集合或元組)。我想通過CSV文件「掃描」來識別任何魚類的發生。Python - 識別字符串中的值
我想:
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'
)
'stocks'是一個元組。您正在查找字符串中的元組對象。相反,你需要一個單獨的'for'循環遍歷整個元組中的每個字符串。 – roganjosh
你是否考慮過把股票變成一套 - 也就是'sstocks = set(股票)' – user1245262