逐行和逐字检查python文本文件

时间:2018-11-11 19:39:22

标签: 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="")

1 个答案:

答案 0 :(得分:0)

如果我的理解正确,您是否想知道一行中的每个单词是否与您的预定单词列表匹配?

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

就足够了-每次打印都会在新行上打断,但这相对容易修复:)