reading from a file and checking if word in list












0















my wife texts me unorganized and long grocery lists. I will paste her list into a text file. I want to write a program that will will erase her unordered list and write to that file an ordered list. e.x. all fruit together, meat together ect. clearly this program is in its infancy. in my text file, I have written:



banana
apple


in this case, when I iterate through each line of my text file, I expect item to be appended to fruit? but it is not. what have I missed?



fruit = 
vegetables =
meat =
dairy =
fruit_list = ["banana", "apple", "orange"]

def read_list():
with open("/storage/emulated/0/textfiles/grocery.txt", "r") as r:
for item in r:
if item in fruit_list:
fruit.append(item)
print(fruit)









share|improve this question


















  • 3





    strip the item. there is a newline at the end. "banana" != "bananan"

    – Lafexlos
    Nov 21 '18 at 14:53






  • 2





    Try to cut the whitespaces before the comparison: item=item.strip()

    – kantal
    Nov 21 '18 at 14:53











  • Possible duplicate of String comparison doesn't seem to work for lines read from a file Probably there are better ones out there but this is the first one I got.

    – Lafexlos
    Nov 21 '18 at 14:55













  • Iterating like that includes the newline character in each item. Try e.g. item.strip() or use r.splitlines() for a list of all newlineless items.

    – SpghttCd
    Nov 21 '18 at 14:55


















0















my wife texts me unorganized and long grocery lists. I will paste her list into a text file. I want to write a program that will will erase her unordered list and write to that file an ordered list. e.x. all fruit together, meat together ect. clearly this program is in its infancy. in my text file, I have written:



banana
apple


in this case, when I iterate through each line of my text file, I expect item to be appended to fruit? but it is not. what have I missed?



fruit = 
vegetables =
meat =
dairy =
fruit_list = ["banana", "apple", "orange"]

def read_list():
with open("/storage/emulated/0/textfiles/grocery.txt", "r") as r:
for item in r:
if item in fruit_list:
fruit.append(item)
print(fruit)









share|improve this question


















  • 3





    strip the item. there is a newline at the end. "banana" != "bananan"

    – Lafexlos
    Nov 21 '18 at 14:53






  • 2





    Try to cut the whitespaces before the comparison: item=item.strip()

    – kantal
    Nov 21 '18 at 14:53











  • Possible duplicate of String comparison doesn't seem to work for lines read from a file Probably there are better ones out there but this is the first one I got.

    – Lafexlos
    Nov 21 '18 at 14:55













  • Iterating like that includes the newline character in each item. Try e.g. item.strip() or use r.splitlines() for a list of all newlineless items.

    – SpghttCd
    Nov 21 '18 at 14:55
















0












0








0








my wife texts me unorganized and long grocery lists. I will paste her list into a text file. I want to write a program that will will erase her unordered list and write to that file an ordered list. e.x. all fruit together, meat together ect. clearly this program is in its infancy. in my text file, I have written:



banana
apple


in this case, when I iterate through each line of my text file, I expect item to be appended to fruit? but it is not. what have I missed?



fruit = 
vegetables =
meat =
dairy =
fruit_list = ["banana", "apple", "orange"]

def read_list():
with open("/storage/emulated/0/textfiles/grocery.txt", "r") as r:
for item in r:
if item in fruit_list:
fruit.append(item)
print(fruit)









share|improve this question














my wife texts me unorganized and long grocery lists. I will paste her list into a text file. I want to write a program that will will erase her unordered list and write to that file an ordered list. e.x. all fruit together, meat together ect. clearly this program is in its infancy. in my text file, I have written:



banana
apple


in this case, when I iterate through each line of my text file, I expect item to be appended to fruit? but it is not. what have I missed?



fruit = 
vegetables =
meat =
dairy =
fruit_list = ["banana", "apple", "orange"]

def read_list():
with open("/storage/emulated/0/textfiles/grocery.txt", "r") as r:
for item in r:
if item in fruit_list:
fruit.append(item)
print(fruit)






python list for-loop conditional






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 14:49









TerryTerry

86




86








  • 3





    strip the item. there is a newline at the end. "banana" != "bananan"

    – Lafexlos
    Nov 21 '18 at 14:53






  • 2





    Try to cut the whitespaces before the comparison: item=item.strip()

    – kantal
    Nov 21 '18 at 14:53











  • Possible duplicate of String comparison doesn't seem to work for lines read from a file Probably there are better ones out there but this is the first one I got.

    – Lafexlos
    Nov 21 '18 at 14:55













  • Iterating like that includes the newline character in each item. Try e.g. item.strip() or use r.splitlines() for a list of all newlineless items.

    – SpghttCd
    Nov 21 '18 at 14:55
















  • 3





    strip the item. there is a newline at the end. "banana" != "bananan"

    – Lafexlos
    Nov 21 '18 at 14:53






  • 2





    Try to cut the whitespaces before the comparison: item=item.strip()

    – kantal
    Nov 21 '18 at 14:53











  • Possible duplicate of String comparison doesn't seem to work for lines read from a file Probably there are better ones out there but this is the first one I got.

    – Lafexlos
    Nov 21 '18 at 14:55













  • Iterating like that includes the newline character in each item. Try e.g. item.strip() or use r.splitlines() for a list of all newlineless items.

    – SpghttCd
    Nov 21 '18 at 14:55










3




3





strip the item. there is a newline at the end. "banana" != "bananan"

– Lafexlos
Nov 21 '18 at 14:53





strip the item. there is a newline at the end. "banana" != "bananan"

– Lafexlos
Nov 21 '18 at 14:53




2




2





Try to cut the whitespaces before the comparison: item=item.strip()

– kantal
Nov 21 '18 at 14:53





Try to cut the whitespaces before the comparison: item=item.strip()

– kantal
Nov 21 '18 at 14:53













Possible duplicate of String comparison doesn't seem to work for lines read from a file Probably there are better ones out there but this is the first one I got.

– Lafexlos
Nov 21 '18 at 14:55







Possible duplicate of String comparison doesn't seem to work for lines read from a file Probably there are better ones out there but this is the first one I got.

– Lafexlos
Nov 21 '18 at 14:55















Iterating like that includes the newline character in each item. Try e.g. item.strip() or use r.splitlines() for a list of all newlineless items.

– SpghttCd
Nov 21 '18 at 14:55







Iterating like that includes the newline character in each item. Try e.g. item.strip() or use r.splitlines() for a list of all newlineless items.

– SpghttCd
Nov 21 '18 at 14:55














2 Answers
2






active

oldest

votes


















3














You need to strip the line endings from the file, as they are carrying n or rn(Windows carriage return) at the ends. This means you are comparing banana with bananan, which are not equal, leading to nothing being appended.



You can fix this by using str.strip() beforehand like so:



item = item.strip()
if item in fruit_list:
# rest of code


Or you can just strip from the right with str.rstrip():



item = item.rstrip()
if item in fruit_list:
# rest of code


You could also strip everything while iterating with map():



for item in map(str.strip, r):
# rest of code


But the first two solutions are fine.






share|improve this answer





















  • 1





    I used item.rstrip(). that worked, thank you

    – Terry
    Nov 21 '18 at 15:37



















2














Use .rstrip() to cut 'n' simbols from end of lines



def read_list():
with open("grocery.txt", "r") as r:
for item in r:
if item.rstrip() in fruit_list:
fruit.append(item)
print(fruit)

read_list()


also you can use item.rstrip().lower() to check items that starts with Upper letter






share|improve this answer





















  • 1





    Readlines not needed, strip() is the key to their issue.

    – SpghttCd
    Nov 21 '18 at 15:03











  • @SpghttCd readlines will return 'bananan' of first line in example. And it does not equal to 'banana' that in fruit_list

    – Andrey Suglobov
    Nov 21 '18 at 15:08






  • 1





    Right, and that is exactly the problem of the question. hence: readlines is not a solution for the problem.

    – SpghttCd
    Nov 21 '18 at 15:11











  • @SpghttCd your truth, readlines() is unnecessary

    – Andrey Suglobov
    Nov 21 '18 at 15:20













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',
autoActivateHeartbeat: false,
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%2f53414635%2freading-from-a-file-and-checking-if-word-in-list%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














You need to strip the line endings from the file, as they are carrying n or rn(Windows carriage return) at the ends. This means you are comparing banana with bananan, which are not equal, leading to nothing being appended.



You can fix this by using str.strip() beforehand like so:



item = item.strip()
if item in fruit_list:
# rest of code


Or you can just strip from the right with str.rstrip():



item = item.rstrip()
if item in fruit_list:
# rest of code


You could also strip everything while iterating with map():



for item in map(str.strip, r):
# rest of code


But the first two solutions are fine.






share|improve this answer





















  • 1





    I used item.rstrip(). that worked, thank you

    – Terry
    Nov 21 '18 at 15:37
















3














You need to strip the line endings from the file, as they are carrying n or rn(Windows carriage return) at the ends. This means you are comparing banana with bananan, which are not equal, leading to nothing being appended.



You can fix this by using str.strip() beforehand like so:



item = item.strip()
if item in fruit_list:
# rest of code


Or you can just strip from the right with str.rstrip():



item = item.rstrip()
if item in fruit_list:
# rest of code


You could also strip everything while iterating with map():



for item in map(str.strip, r):
# rest of code


But the first two solutions are fine.






share|improve this answer





















  • 1





    I used item.rstrip(). that worked, thank you

    – Terry
    Nov 21 '18 at 15:37














3












3








3







You need to strip the line endings from the file, as they are carrying n or rn(Windows carriage return) at the ends. This means you are comparing banana with bananan, which are not equal, leading to nothing being appended.



You can fix this by using str.strip() beforehand like so:



item = item.strip()
if item in fruit_list:
# rest of code


Or you can just strip from the right with str.rstrip():



item = item.rstrip()
if item in fruit_list:
# rest of code


You could also strip everything while iterating with map():



for item in map(str.strip, r):
# rest of code


But the first two solutions are fine.






share|improve this answer















You need to strip the line endings from the file, as they are carrying n or rn(Windows carriage return) at the ends. This means you are comparing banana with bananan, which are not equal, leading to nothing being appended.



You can fix this by using str.strip() beforehand like so:



item = item.strip()
if item in fruit_list:
# rest of code


Or you can just strip from the right with str.rstrip():



item = item.rstrip()
if item in fruit_list:
# rest of code


You could also strip everything while iterating with map():



for item in map(str.strip, r):
# rest of code


But the first two solutions are fine.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 15:09

























answered Nov 21 '18 at 14:53









RoadRunnerRoadRunner

11.3k31441




11.3k31441








  • 1





    I used item.rstrip(). that worked, thank you

    – Terry
    Nov 21 '18 at 15:37














  • 1





    I used item.rstrip(). that worked, thank you

    – Terry
    Nov 21 '18 at 15:37








1




1





I used item.rstrip(). that worked, thank you

– Terry
Nov 21 '18 at 15:37





I used item.rstrip(). that worked, thank you

– Terry
Nov 21 '18 at 15:37













2














Use .rstrip() to cut 'n' simbols from end of lines



def read_list():
with open("grocery.txt", "r") as r:
for item in r:
if item.rstrip() in fruit_list:
fruit.append(item)
print(fruit)

read_list()


also you can use item.rstrip().lower() to check items that starts with Upper letter






share|improve this answer





















  • 1





    Readlines not needed, strip() is the key to their issue.

    – SpghttCd
    Nov 21 '18 at 15:03











  • @SpghttCd readlines will return 'bananan' of first line in example. And it does not equal to 'banana' that in fruit_list

    – Andrey Suglobov
    Nov 21 '18 at 15:08






  • 1





    Right, and that is exactly the problem of the question. hence: readlines is not a solution for the problem.

    – SpghttCd
    Nov 21 '18 at 15:11











  • @SpghttCd your truth, readlines() is unnecessary

    – Andrey Suglobov
    Nov 21 '18 at 15:20


















2














Use .rstrip() to cut 'n' simbols from end of lines



def read_list():
with open("grocery.txt", "r") as r:
for item in r:
if item.rstrip() in fruit_list:
fruit.append(item)
print(fruit)

read_list()


also you can use item.rstrip().lower() to check items that starts with Upper letter






share|improve this answer





















  • 1





    Readlines not needed, strip() is the key to their issue.

    – SpghttCd
    Nov 21 '18 at 15:03











  • @SpghttCd readlines will return 'bananan' of first line in example. And it does not equal to 'banana' that in fruit_list

    – Andrey Suglobov
    Nov 21 '18 at 15:08






  • 1





    Right, and that is exactly the problem of the question. hence: readlines is not a solution for the problem.

    – SpghttCd
    Nov 21 '18 at 15:11











  • @SpghttCd your truth, readlines() is unnecessary

    – Andrey Suglobov
    Nov 21 '18 at 15:20
















2












2








2







Use .rstrip() to cut 'n' simbols from end of lines



def read_list():
with open("grocery.txt", "r") as r:
for item in r:
if item.rstrip() in fruit_list:
fruit.append(item)
print(fruit)

read_list()


also you can use item.rstrip().lower() to check items that starts with Upper letter






share|improve this answer















Use .rstrip() to cut 'n' simbols from end of lines



def read_list():
with open("grocery.txt", "r") as r:
for item in r:
if item.rstrip() in fruit_list:
fruit.append(item)
print(fruit)

read_list()


also you can use item.rstrip().lower() to check items that starts with Upper letter







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 15:21

























answered Nov 21 '18 at 15:01









Andrey SuglobovAndrey Suglobov

1449




1449








  • 1





    Readlines not needed, strip() is the key to their issue.

    – SpghttCd
    Nov 21 '18 at 15:03











  • @SpghttCd readlines will return 'bananan' of first line in example. And it does not equal to 'banana' that in fruit_list

    – Andrey Suglobov
    Nov 21 '18 at 15:08






  • 1





    Right, and that is exactly the problem of the question. hence: readlines is not a solution for the problem.

    – SpghttCd
    Nov 21 '18 at 15:11











  • @SpghttCd your truth, readlines() is unnecessary

    – Andrey Suglobov
    Nov 21 '18 at 15:20
















  • 1





    Readlines not needed, strip() is the key to their issue.

    – SpghttCd
    Nov 21 '18 at 15:03











  • @SpghttCd readlines will return 'bananan' of first line in example. And it does not equal to 'banana' that in fruit_list

    – Andrey Suglobov
    Nov 21 '18 at 15:08






  • 1





    Right, and that is exactly the problem of the question. hence: readlines is not a solution for the problem.

    – SpghttCd
    Nov 21 '18 at 15:11











  • @SpghttCd your truth, readlines() is unnecessary

    – Andrey Suglobov
    Nov 21 '18 at 15:20










1




1





Readlines not needed, strip() is the key to their issue.

– SpghttCd
Nov 21 '18 at 15:03





Readlines not needed, strip() is the key to their issue.

– SpghttCd
Nov 21 '18 at 15:03













@SpghttCd readlines will return 'bananan' of first line in example. And it does not equal to 'banana' that in fruit_list

– Andrey Suglobov
Nov 21 '18 at 15:08





@SpghttCd readlines will return 'bananan' of first line in example. And it does not equal to 'banana' that in fruit_list

– Andrey Suglobov
Nov 21 '18 at 15:08




1




1





Right, and that is exactly the problem of the question. hence: readlines is not a solution for the problem.

– SpghttCd
Nov 21 '18 at 15:11





Right, and that is exactly the problem of the question. hence: readlines is not a solution for the problem.

– SpghttCd
Nov 21 '18 at 15:11













@SpghttCd your truth, readlines() is unnecessary

– Andrey Suglobov
Nov 21 '18 at 15:20







@SpghttCd your truth, readlines() is unnecessary

– Andrey Suglobov
Nov 21 '18 at 15:20




















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414635%2freading-from-a-file-and-checking-if-word-in-list%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

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?