[英]Checking a text file line by line and word by word python
我有一個單詞列表,例如:果汁、水、檸檬水和一個文本文件
Lemon Lemonade Mango Curd Doggy
Dafne Cord Water Color Lemon
Powder Doggy TV Juice
我需要我的 python 程序來讀取每個單詞並與“果汁、水、檸檬水”和每行打印進行比較
Line 1: NotAccepted(Lemon) Accepted NotAccepted(Mango) NotAccepted(Curd) NotAccepted(Doggy)
Line 2:NotAccepted(Dafne) NotAccepted(Cord) Accepted NotAccepted(Color) NotAccepted(Lemon)
Line 3:NotAccepted(Powder) NotAccepted(Doggy) NotAccepted(TV) Accepted
我當前的程序正在打印
NotAccepted(Lemon)
Accepted
NotAccepted(Mango)
NotAccepted(Curd)
使用我當前的代碼:哪個
lineas = archivo.readlines()
for linea in lineas:
linea = linea.strip()
lista = linea.split()
for a in lista:
if (a == "Mango"):
print ("Aceptado", end="")
else:
print ("Denegado ("+ a + ")",end="")
如果我理解正確,您想知道一行中的每個單詞是否與您預先確定的單詞列表相匹配?
for line in text_file:
for j in line.split(' '):
if j=='Juice' or j=='Water' or j=='Lemonade':
print('Accepted')
else:
print('Not accepted('+j+')')
應該足夠了 - 它會在每次打印時換行,但這相對容易修復:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.