为了账号安全,请及时绑定邮箱和手机立即绑定

逐行逐字检查文本文件python

逐行逐字检查文本文件python

蝴蝶刀刀 2021-08-14 21:30:08
我有一个单词列表,例如:果汁、水、柠檬水和一个文本文件Lemon Lemonade Mango Curd DoggyDafne Cord Water Color LemonPowder 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)AcceptedNotAccepted(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 回答

?
繁星点点滴滴

TA贡献1803条经验 获得超3个赞

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


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

应该足够了 - 它会在每次打印时换行,但这相对容易修复:)


查看完整回答
反对 回复 2021-08-14
  • 1 回答
  • 0 关注
  • 205 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信