[英]How to compare a substring with a list of elements in Python?
我正在嘗試將 substring 與我從讀取 a.txt 文件並將其與列表元素匹配的字符串中進行比較。
.txt 文件中的字符串如下:
1. Blood 75.6 gm% 12-14
2. Rash 15.4 tm/m 54-89
我有一個名為 data 的列表,其中包括作為字符串中第一個單詞出現的單詞。 所以,
data = ['blood', 'Rash']
f = open('my.txt', r)
for x in f:
if x.find(str(data)):
# Do something
我如何匹配這兩者然后對其進行操作?
with open("my.txt") as f:
for line in f:
for i in data:
if i in line:
do_operation()
這是你要求的嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.