Checking a text file line by line and word by word python











up vote
-1
down vote

favorite












I have a list of words Ex: Juice, Water, Lemonade
and a text file



Lemon Lemonade Mango Curd Doggy
Dafne Cord Water Color Lemon
Powder Doggy TV Juice


I need my python program to read each word and compare to "Juice, Water, Lemonade" and per each line print



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


My current program is printing



NotAccepted(Lemon)
Accepted
NotAccepted(Mango)
NotAccepted(Curd)


with my current code:
Which



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="")









share|improve this question
























  • Great list of words. What is your question?
    – Willem Van Onsem
    Nov 11 at 19:40















up vote
-1
down vote

favorite












I have a list of words Ex: Juice, Water, Lemonade
and a text file



Lemon Lemonade Mango Curd Doggy
Dafne Cord Water Color Lemon
Powder Doggy TV Juice


I need my python program to read each word and compare to "Juice, Water, Lemonade" and per each line print



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


My current program is printing



NotAccepted(Lemon)
Accepted
NotAccepted(Mango)
NotAccepted(Curd)


with my current code:
Which



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="")









share|improve this question
























  • Great list of words. What is your question?
    – Willem Van Onsem
    Nov 11 at 19:40













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I have a list of words Ex: Juice, Water, Lemonade
and a text file



Lemon Lemonade Mango Curd Doggy
Dafne Cord Water Color Lemon
Powder Doggy TV Juice


I need my python program to read each word and compare to "Juice, Water, Lemonade" and per each line print



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


My current program is printing



NotAccepted(Lemon)
Accepted
NotAccepted(Mango)
NotAccepted(Curd)


with my current code:
Which



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="")









share|improve this question















I have a list of words Ex: Juice, Water, Lemonade
and a text file



Lemon Lemonade Mango Curd Doggy
Dafne Cord Water Color Lemon
Powder Doggy TV Juice


I need my python program to read each word and compare to "Juice, Water, Lemonade" and per each line print



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


My current program is printing



NotAccepted(Lemon)
Accepted
NotAccepted(Mango)
NotAccepted(Curd)


with my current code:
Which



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="")






python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 20:07









eyllanesc

71.8k93054




71.8k93054










asked Nov 11 at 19:39









Nick

35




35












  • Great list of words. What is your question?
    – Willem Van Onsem
    Nov 11 at 19:40


















  • Great list of words. What is your question?
    – Willem Van Onsem
    Nov 11 at 19:40
















Great list of words. What is your question?
– Willem Van Onsem
Nov 11 at 19:40




Great list of words. What is your question?
– Willem Van Onsem
Nov 11 at 19:40












1 Answer
1






active

oldest

votes

















up vote
0
down vote













If I understand correctly, you want to find out if each word on a line matches with your list of pre-determined words?



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


should suffice - it'll break onto new lines every print but that's relatively easily to fix :)






share|improve this answer





















  • How would I make it to print in same line ? like per Line
    – Nick
    Nov 11 at 20:02










  • I got it, but if my list of Word is quite big like more than 20? will I have to do "OR" for each word?
    – Nick
    Nov 11 at 20:09










  • An easy way would be to have something like a variable word_list=['a','b'....'] and then check if j in word_list. For same line printing, you could - after for line in text_file, have output='', and then instead of using print, use output+=
    – Henry
    Nov 11 at 20:43










  • If I want to put that if it starts with an quotation mark, it is correct until the end of the quotation mark Ex: Ramen Cord "Cardomologisticly correct" Rambo NotCorrect(Ramen) NotCorrect(Cord) Correct NotCorrect(Rambo)
    – Nick
    Nov 11 at 21:13












  • I dont understand your question
    – Henry
    Nov 11 at 21:14











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252481%2fchecking-a-text-file-line-by-line-and-word-by-word-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













If I understand correctly, you want to find out if each word on a line matches with your list of pre-determined words?



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


should suffice - it'll break onto new lines every print but that's relatively easily to fix :)






share|improve this answer





















  • How would I make it to print in same line ? like per Line
    – Nick
    Nov 11 at 20:02










  • I got it, but if my list of Word is quite big like more than 20? will I have to do "OR" for each word?
    – Nick
    Nov 11 at 20:09










  • An easy way would be to have something like a variable word_list=['a','b'....'] and then check if j in word_list. For same line printing, you could - after for line in text_file, have output='', and then instead of using print, use output+=
    – Henry
    Nov 11 at 20:43










  • If I want to put that if it starts with an quotation mark, it is correct until the end of the quotation mark Ex: Ramen Cord "Cardomologisticly correct" Rambo NotCorrect(Ramen) NotCorrect(Cord) Correct NotCorrect(Rambo)
    – Nick
    Nov 11 at 21:13












  • I dont understand your question
    – Henry
    Nov 11 at 21:14















up vote
0
down vote













If I understand correctly, you want to find out if each word on a line matches with your list of pre-determined words?



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


should suffice - it'll break onto new lines every print but that's relatively easily to fix :)






share|improve this answer





















  • How would I make it to print in same line ? like per Line
    – Nick
    Nov 11 at 20:02










  • I got it, but if my list of Word is quite big like more than 20? will I have to do "OR" for each word?
    – Nick
    Nov 11 at 20:09










  • An easy way would be to have something like a variable word_list=['a','b'....'] and then check if j in word_list. For same line printing, you could - after for line in text_file, have output='', and then instead of using print, use output+=
    – Henry
    Nov 11 at 20:43










  • If I want to put that if it starts with an quotation mark, it is correct until the end of the quotation mark Ex: Ramen Cord "Cardomologisticly correct" Rambo NotCorrect(Ramen) NotCorrect(Cord) Correct NotCorrect(Rambo)
    – Nick
    Nov 11 at 21:13












  • I dont understand your question
    – Henry
    Nov 11 at 21:14













up vote
0
down vote










up vote
0
down vote









If I understand correctly, you want to find out if each word on a line matches with your list of pre-determined words?



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


should suffice - it'll break onto new lines every print but that's relatively easily to fix :)






share|improve this answer












If I understand correctly, you want to find out if each word on a line matches with your list of pre-determined words?



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


should suffice - it'll break onto new lines every print but that's relatively easily to fix :)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 19:59









Henry

1,206517




1,206517












  • How would I make it to print in same line ? like per Line
    – Nick
    Nov 11 at 20:02










  • I got it, but if my list of Word is quite big like more than 20? will I have to do "OR" for each word?
    – Nick
    Nov 11 at 20:09










  • An easy way would be to have something like a variable word_list=['a','b'....'] and then check if j in word_list. For same line printing, you could - after for line in text_file, have output='', and then instead of using print, use output+=
    – Henry
    Nov 11 at 20:43










  • If I want to put that if it starts with an quotation mark, it is correct until the end of the quotation mark Ex: Ramen Cord "Cardomologisticly correct" Rambo NotCorrect(Ramen) NotCorrect(Cord) Correct NotCorrect(Rambo)
    – Nick
    Nov 11 at 21:13












  • I dont understand your question
    – Henry
    Nov 11 at 21:14


















  • How would I make it to print in same line ? like per Line
    – Nick
    Nov 11 at 20:02










  • I got it, but if my list of Word is quite big like more than 20? will I have to do "OR" for each word?
    – Nick
    Nov 11 at 20:09










  • An easy way would be to have something like a variable word_list=['a','b'....'] and then check if j in word_list. For same line printing, you could - after for line in text_file, have output='', and then instead of using print, use output+=
    – Henry
    Nov 11 at 20:43










  • If I want to put that if it starts with an quotation mark, it is correct until the end of the quotation mark Ex: Ramen Cord "Cardomologisticly correct" Rambo NotCorrect(Ramen) NotCorrect(Cord) Correct NotCorrect(Rambo)
    – Nick
    Nov 11 at 21:13












  • I dont understand your question
    – Henry
    Nov 11 at 21:14
















How would I make it to print in same line ? like per Line
– Nick
Nov 11 at 20:02




How would I make it to print in same line ? like per Line
– Nick
Nov 11 at 20:02












I got it, but if my list of Word is quite big like more than 20? will I have to do "OR" for each word?
– Nick
Nov 11 at 20:09




I got it, but if my list of Word is quite big like more than 20? will I have to do "OR" for each word?
– Nick
Nov 11 at 20:09












An easy way would be to have something like a variable word_list=['a','b'....'] and then check if j in word_list. For same line printing, you could - after for line in text_file, have output='', and then instead of using print, use output+=
– Henry
Nov 11 at 20:43




An easy way would be to have something like a variable word_list=['a','b'....'] and then check if j in word_list. For same line printing, you could - after for line in text_file, have output='', and then instead of using print, use output+=
– Henry
Nov 11 at 20:43












If I want to put that if it starts with an quotation mark, it is correct until the end of the quotation mark Ex: Ramen Cord "Cardomologisticly correct" Rambo NotCorrect(Ramen) NotCorrect(Cord) Correct NotCorrect(Rambo)
– Nick
Nov 11 at 21:13






If I want to put that if it starts with an quotation mark, it is correct until the end of the quotation mark Ex: Ramen Cord "Cardomologisticly correct" Rambo NotCorrect(Ramen) NotCorrect(Cord) Correct NotCorrect(Rambo)
– Nick
Nov 11 at 21:13














I dont understand your question
– Henry
Nov 11 at 21:14




I dont understand your question
– Henry
Nov 11 at 21:14


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252481%2fchecking-a-text-file-line-by-line-and-word-by-word-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

The Sandy Post

Danny Elfman

Pages that link to "Head v. Amoskeag Manufacturing Co."