Why do two of the same strings not return as being the same when compared?
I have the following code:
file = open('AdjectivesList.txt', 'r')
lines = file.readlines()
file.close()
for word in words:
wordLowercase = word.lower()
for x, lol in enumerate(lines):
gg = (lines[x].lower())
if wordLowercase == gg:
print('identified')
Even when wordLowercase
does equal gg
, the string "identified" is not being printed. Why is this the case?
python
|
show 1 more comment
I have the following code:
file = open('AdjectivesList.txt', 'r')
lines = file.readlines()
file.close()
for word in words:
wordLowercase = word.lower()
for x, lol in enumerate(lines):
gg = (lines[x].lower())
if wordLowercase == gg:
print('identified')
Even when wordLowercase
does equal gg
, the string "identified" is not being printed. Why is this the case?
python
There're maybe some unprintable characters?
– Hou Lu
Nov 20 '18 at 2:20
1
lines[x].lower()
is justlol.lower()
.
– Austin
Nov 20 '18 at 2:21
Please trygg = lines[x].lower().strip
instead ofgg = lines[x].lower()
only. There might be some whitespace characters that gets included ingg
. I suggest going for @Austin's remark and uselol.lower()
instead oflines[x].lower()
.
– Sean Francis N. Ballais
Nov 20 '18 at 2:25
I think you can do away withenumerate
and just usefor line in lines:
. That way, you won't need to uselines[x]
and just need to useline
.
– Sean Francis N. Ballais
Nov 20 '18 at 2:26
Maybe instead ofgg = (lines[x].lower())
dogg = lol.lower().strip()
– U9-Forward
Nov 20 '18 at 2:32
|
show 1 more comment
I have the following code:
file = open('AdjectivesList.txt', 'r')
lines = file.readlines()
file.close()
for word in words:
wordLowercase = word.lower()
for x, lol in enumerate(lines):
gg = (lines[x].lower())
if wordLowercase == gg:
print('identified')
Even when wordLowercase
does equal gg
, the string "identified" is not being printed. Why is this the case?
python
I have the following code:
file = open('AdjectivesList.txt', 'r')
lines = file.readlines()
file.close()
for word in words:
wordLowercase = word.lower()
for x, lol in enumerate(lines):
gg = (lines[x].lower())
if wordLowercase == gg:
print('identified')
Even when wordLowercase
does equal gg
, the string "identified" is not being printed. Why is this the case?
python
python
edited Nov 20 '18 at 5:27
Sean Francis N. Ballais
1,27121932
1,27121932
asked Nov 20 '18 at 2:17
M. ChakM. Chak
134
134
There're maybe some unprintable characters?
– Hou Lu
Nov 20 '18 at 2:20
1
lines[x].lower()
is justlol.lower()
.
– Austin
Nov 20 '18 at 2:21
Please trygg = lines[x].lower().strip
instead ofgg = lines[x].lower()
only. There might be some whitespace characters that gets included ingg
. I suggest going for @Austin's remark and uselol.lower()
instead oflines[x].lower()
.
– Sean Francis N. Ballais
Nov 20 '18 at 2:25
I think you can do away withenumerate
and just usefor line in lines:
. That way, you won't need to uselines[x]
and just need to useline
.
– Sean Francis N. Ballais
Nov 20 '18 at 2:26
Maybe instead ofgg = (lines[x].lower())
dogg = lol.lower().strip()
– U9-Forward
Nov 20 '18 at 2:32
|
show 1 more comment
There're maybe some unprintable characters?
– Hou Lu
Nov 20 '18 at 2:20
1
lines[x].lower()
is justlol.lower()
.
– Austin
Nov 20 '18 at 2:21
Please trygg = lines[x].lower().strip
instead ofgg = lines[x].lower()
only. There might be some whitespace characters that gets included ingg
. I suggest going for @Austin's remark and uselol.lower()
instead oflines[x].lower()
.
– Sean Francis N. Ballais
Nov 20 '18 at 2:25
I think you can do away withenumerate
and just usefor line in lines:
. That way, you won't need to uselines[x]
and just need to useline
.
– Sean Francis N. Ballais
Nov 20 '18 at 2:26
Maybe instead ofgg = (lines[x].lower())
dogg = lol.lower().strip()
– U9-Forward
Nov 20 '18 at 2:32
There're maybe some unprintable characters?
– Hou Lu
Nov 20 '18 at 2:20
There're maybe some unprintable characters?
– Hou Lu
Nov 20 '18 at 2:20
1
1
lines[x].lower()
is just lol.lower()
.– Austin
Nov 20 '18 at 2:21
lines[x].lower()
is just lol.lower()
.– Austin
Nov 20 '18 at 2:21
Please try
gg = lines[x].lower().strip
instead of gg = lines[x].lower()
only. There might be some whitespace characters that gets included in gg
. I suggest going for @Austin's remark and use lol.lower()
instead of lines[x].lower()
.– Sean Francis N. Ballais
Nov 20 '18 at 2:25
Please try
gg = lines[x].lower().strip
instead of gg = lines[x].lower()
only. There might be some whitespace characters that gets included in gg
. I suggest going for @Austin's remark and use lol.lower()
instead of lines[x].lower()
.– Sean Francis N. Ballais
Nov 20 '18 at 2:25
I think you can do away with
enumerate
and just use for line in lines:
. That way, you won't need to use lines[x]
and just need to use line
.– Sean Francis N. Ballais
Nov 20 '18 at 2:26
I think you can do away with
enumerate
and just use for line in lines:
. That way, you won't need to use lines[x]
and just need to use line
.– Sean Francis N. Ballais
Nov 20 '18 at 2:26
Maybe instead of
gg = (lines[x].lower())
do gg = lol.lower().strip()
– U9-Forward
Nov 20 '18 at 2:32
Maybe instead of
gg = (lines[x].lower())
do gg = lol.lower().strip()
– U9-Forward
Nov 20 '18 at 2:32
|
show 1 more comment
1 Answer
1
active
oldest
votes
.readlines()
includes the newline character at the end of every line in the text file. This is most likely the cause of your problem. You can remove the newline character (and any whitespace characters from the left and right of the string) by using .strip()
.
gg = lines[x].lower().strip()
Reference
- https://www.tutorialspoint.com/python/file_readlines.htm
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385290%2fwhy-do-two-of-the-same-strings-not-return-as-being-the-same-when-compared%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
.readlines()
includes the newline character at the end of every line in the text file. This is most likely the cause of your problem. You can remove the newline character (and any whitespace characters from the left and right of the string) by using .strip()
.
gg = lines[x].lower().strip()
Reference
- https://www.tutorialspoint.com/python/file_readlines.htm
add a comment |
.readlines()
includes the newline character at the end of every line in the text file. This is most likely the cause of your problem. You can remove the newline character (and any whitespace characters from the left and right of the string) by using .strip()
.
gg = lines[x].lower().strip()
Reference
- https://www.tutorialspoint.com/python/file_readlines.htm
add a comment |
.readlines()
includes the newline character at the end of every line in the text file. This is most likely the cause of your problem. You can remove the newline character (and any whitespace characters from the left and right of the string) by using .strip()
.
gg = lines[x].lower().strip()
Reference
- https://www.tutorialspoint.com/python/file_readlines.htm
.readlines()
includes the newline character at the end of every line in the text file. This is most likely the cause of your problem. You can remove the newline character (and any whitespace characters from the left and right of the string) by using .strip()
.
gg = lines[x].lower().strip()
Reference
- https://www.tutorialspoint.com/python/file_readlines.htm
answered Nov 20 '18 at 2:31
Sean Francis N. BallaisSean Francis N. Ballais
1,27121932
1,27121932
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385290%2fwhy-do-two-of-the-same-strings-not-return-as-being-the-same-when-compared%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
There're maybe some unprintable characters?
– Hou Lu
Nov 20 '18 at 2:20
1
lines[x].lower()
is justlol.lower()
.– Austin
Nov 20 '18 at 2:21
Please try
gg = lines[x].lower().strip
instead ofgg = lines[x].lower()
only. There might be some whitespace characters that gets included ingg
. I suggest going for @Austin's remark and uselol.lower()
instead oflines[x].lower()
.– Sean Francis N. Ballais
Nov 20 '18 at 2:25
I think you can do away with
enumerate
and just usefor line in lines:
. That way, you won't need to uselines[x]
and just need to useline
.– Sean Francis N. Ballais
Nov 20 '18 at 2:26
Maybe instead of
gg = (lines[x].lower())
dogg = lol.lower().strip()
– U9-Forward
Nov 20 '18 at 2:32