Function to find a string within text file (Python)
I'm trying to write a function where the input string is searched for within a text file (the text file contains a list of english words, all in lowercase).
The input string may be inputted as all lowercase, uppercase or with the first letter uppercase and the rest lowercase.
So far I've got this, but it's not exactly working and I'm not sure what to do.
def is_english_word( string ):
with open("english_words.txt", "r") as fileObject:
if string in fileObject.read():
return(True)
newstring = string
if newstring[0].isupper() == True:
newstring == string.lower()
if newstring in fileObject.read():
return(True)
python text-files
|
show 1 more comment
I'm trying to write a function where the input string is searched for within a text file (the text file contains a list of english words, all in lowercase).
The input string may be inputted as all lowercase, uppercase or with the first letter uppercase and the rest lowercase.
So far I've got this, but it's not exactly working and I'm not sure what to do.
def is_english_word( string ):
with open("english_words.txt", "r") as fileObject:
if string in fileObject.read():
return(True)
newstring = string
if newstring[0].isupper() == True:
newstring == string.lower()
if newstring in fileObject.read():
return(True)
python text-files
The first fileObject.read() reads all the file content, so the second call reads nothing.
– kantal
Nov 12 '18 at 15:30
@kantal how do you make it read one line at a time
– anon2000
Nov 12 '18 at 15:32
for s in fileObject:
– kantal
Nov 12 '18 at 15:37
Maybe I've misread your question, but why not simply doingdef is_english_word(string): with open("english_words.txt", "r") as fileObject: if string.lower() in fileObject.read(): return True
? So justs lowering input string to be consistent to the text.
– colidyre
Nov 12 '18 at 15:46
@colidyre because if the input string has an uppercase letter in the middle of string whilst the rest of the letters are lowercase, the return value should be false. Doing what you described above would simply return it true and not take into account the cases of the letters
– anon2000
Nov 12 '18 at 15:48
|
show 1 more comment
I'm trying to write a function where the input string is searched for within a text file (the text file contains a list of english words, all in lowercase).
The input string may be inputted as all lowercase, uppercase or with the first letter uppercase and the rest lowercase.
So far I've got this, but it's not exactly working and I'm not sure what to do.
def is_english_word( string ):
with open("english_words.txt", "r") as fileObject:
if string in fileObject.read():
return(True)
newstring = string
if newstring[0].isupper() == True:
newstring == string.lower()
if newstring in fileObject.read():
return(True)
python text-files
I'm trying to write a function where the input string is searched for within a text file (the text file contains a list of english words, all in lowercase).
The input string may be inputted as all lowercase, uppercase or with the first letter uppercase and the rest lowercase.
So far I've got this, but it's not exactly working and I'm not sure what to do.
def is_english_word( string ):
with open("english_words.txt", "r") as fileObject:
if string in fileObject.read():
return(True)
newstring = string
if newstring[0].isupper() == True:
newstring == string.lower()
if newstring in fileObject.read():
return(True)
python text-files
python text-files
edited Nov 12 '18 at 15:33
anon2000
asked Nov 12 '18 at 15:23
anon2000anon2000
135
135
The first fileObject.read() reads all the file content, so the second call reads nothing.
– kantal
Nov 12 '18 at 15:30
@kantal how do you make it read one line at a time
– anon2000
Nov 12 '18 at 15:32
for s in fileObject:
– kantal
Nov 12 '18 at 15:37
Maybe I've misread your question, but why not simply doingdef is_english_word(string): with open("english_words.txt", "r") as fileObject: if string.lower() in fileObject.read(): return True
? So justs lowering input string to be consistent to the text.
– colidyre
Nov 12 '18 at 15:46
@colidyre because if the input string has an uppercase letter in the middle of string whilst the rest of the letters are lowercase, the return value should be false. Doing what you described above would simply return it true and not take into account the cases of the letters
– anon2000
Nov 12 '18 at 15:48
|
show 1 more comment
The first fileObject.read() reads all the file content, so the second call reads nothing.
– kantal
Nov 12 '18 at 15:30
@kantal how do you make it read one line at a time
– anon2000
Nov 12 '18 at 15:32
for s in fileObject:
– kantal
Nov 12 '18 at 15:37
Maybe I've misread your question, but why not simply doingdef is_english_word(string): with open("english_words.txt", "r") as fileObject: if string.lower() in fileObject.read(): return True
? So justs lowering input string to be consistent to the text.
– colidyre
Nov 12 '18 at 15:46
@colidyre because if the input string has an uppercase letter in the middle of string whilst the rest of the letters are lowercase, the return value should be false. Doing what you described above would simply return it true and not take into account the cases of the letters
– anon2000
Nov 12 '18 at 15:48
The first fileObject.read() reads all the file content, so the second call reads nothing.
– kantal
Nov 12 '18 at 15:30
The first fileObject.read() reads all the file content, so the second call reads nothing.
– kantal
Nov 12 '18 at 15:30
@kantal how do you make it read one line at a time
– anon2000
Nov 12 '18 at 15:32
@kantal how do you make it read one line at a time
– anon2000
Nov 12 '18 at 15:32
for s in fileObject:
– kantal
Nov 12 '18 at 15:37
for s in fileObject:
– kantal
Nov 12 '18 at 15:37
Maybe I've misread your question, but why not simply doing
def is_english_word(string): with open("english_words.txt", "r") as fileObject: if string.lower() in fileObject.read(): return True
? So justs lowering input string to be consistent to the text.– colidyre
Nov 12 '18 at 15:46
Maybe I've misread your question, but why not simply doing
def is_english_word(string): with open("english_words.txt", "r") as fileObject: if string.lower() in fileObject.read(): return True
? So justs lowering input string to be consistent to the text.– colidyre
Nov 12 '18 at 15:46
@colidyre because if the input string has an uppercase letter in the middle of string whilst the rest of the letters are lowercase, the return value should be false. Doing what you described above would simply return it true and not take into account the cases of the letters
– anon2000
Nov 12 '18 at 15:48
@colidyre because if the input string has an uppercase letter in the middle of string whilst the rest of the letters are lowercase, the return value should be false. Doing what you described above would simply return it true and not take into account the cases of the letters
– anon2000
Nov 12 '18 at 15:48
|
show 1 more comment
2 Answers
2
active
oldest
votes
you have an indent issue: the with
statement needs to be one indent inside def
:
def is_english_word( string ):
with open("english_words.txt", "r") as fileObject:
if string.istitle() or string.isupper():
return string.lower() in fileObject.read()
else:
return string in fileObject.read()
there is no need to check for all of the possible cases of the input string, just make it all lowercase and check for that.
1
you forgot braces in the secondlower
– Vassiliy Vorobyov
Nov 12 '18 at 15:30
that i did, thanks
– vencaslac
Nov 12 '18 at 15:31
There's a condition where all the possible cases do need to be checked. As a word where one of the middle letters are uppercase (e.g 'pytHon') is meant to return false; but making it all lowercase will always return True
– anon2000
Nov 12 '18 at 15:31
try it out now, i changed it
– vencaslac
Nov 12 '18 at 15:35
@vencaslac the error that was occurring is now fixed but when the whole string is uppercase, it returns false
– anon2000
Nov 12 '18 at 15:43
|
show 2 more comments
Try this:
def is_english_word(string):
with open("english_words.txt", "r") as fileObject:
text = fileObject.read()
return string in text or (string[0].lower() + string[1:]) in text
It works! Thank you. However, it also returns True for when one of the middle letters in the word is uppercase, where it shouldn't. Eg 'pyThon' should return false but it returns True
– anon2000
Nov 12 '18 at 15:30
I've updated an answer. Try it out.
– Vassiliy Vorobyov
Nov 12 '18 at 15:38
I understand what you tried doing by checking if the first letter is uppercase, but when it is, the function returns False for some reason
– anon2000
Nov 12 '18 at 15:39
one more update
– Vassiliy Vorobyov
Nov 12 '18 at 15:40
1
What about strings of length 1?
– colidyre
Nov 12 '18 at 16:04
|
show 2 more comments
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%2f53265205%2ffunction-to-find-a-string-within-text-file-python%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
you have an indent issue: the with
statement needs to be one indent inside def
:
def is_english_word( string ):
with open("english_words.txt", "r") as fileObject:
if string.istitle() or string.isupper():
return string.lower() in fileObject.read()
else:
return string in fileObject.read()
there is no need to check for all of the possible cases of the input string, just make it all lowercase and check for that.
1
you forgot braces in the secondlower
– Vassiliy Vorobyov
Nov 12 '18 at 15:30
that i did, thanks
– vencaslac
Nov 12 '18 at 15:31
There's a condition where all the possible cases do need to be checked. As a word where one of the middle letters are uppercase (e.g 'pytHon') is meant to return false; but making it all lowercase will always return True
– anon2000
Nov 12 '18 at 15:31
try it out now, i changed it
– vencaslac
Nov 12 '18 at 15:35
@vencaslac the error that was occurring is now fixed but when the whole string is uppercase, it returns false
– anon2000
Nov 12 '18 at 15:43
|
show 2 more comments
you have an indent issue: the with
statement needs to be one indent inside def
:
def is_english_word( string ):
with open("english_words.txt", "r") as fileObject:
if string.istitle() or string.isupper():
return string.lower() in fileObject.read()
else:
return string in fileObject.read()
there is no need to check for all of the possible cases of the input string, just make it all lowercase and check for that.
1
you forgot braces in the secondlower
– Vassiliy Vorobyov
Nov 12 '18 at 15:30
that i did, thanks
– vencaslac
Nov 12 '18 at 15:31
There's a condition where all the possible cases do need to be checked. As a word where one of the middle letters are uppercase (e.g 'pytHon') is meant to return false; but making it all lowercase will always return True
– anon2000
Nov 12 '18 at 15:31
try it out now, i changed it
– vencaslac
Nov 12 '18 at 15:35
@vencaslac the error that was occurring is now fixed but when the whole string is uppercase, it returns false
– anon2000
Nov 12 '18 at 15:43
|
show 2 more comments
you have an indent issue: the with
statement needs to be one indent inside def
:
def is_english_word( string ):
with open("english_words.txt", "r") as fileObject:
if string.istitle() or string.isupper():
return string.lower() in fileObject.read()
else:
return string in fileObject.read()
there is no need to check for all of the possible cases of the input string, just make it all lowercase and check for that.
you have an indent issue: the with
statement needs to be one indent inside def
:
def is_english_word( string ):
with open("english_words.txt", "r") as fileObject:
if string.istitle() or string.isupper():
return string.lower() in fileObject.read()
else:
return string in fileObject.read()
there is no need to check for all of the possible cases of the input string, just make it all lowercase and check for that.
edited Nov 12 '18 at 15:45
answered Nov 12 '18 at 15:29
vencaslacvencaslac
1,002217
1,002217
1
you forgot braces in the secondlower
– Vassiliy Vorobyov
Nov 12 '18 at 15:30
that i did, thanks
– vencaslac
Nov 12 '18 at 15:31
There's a condition where all the possible cases do need to be checked. As a word where one of the middle letters are uppercase (e.g 'pytHon') is meant to return false; but making it all lowercase will always return True
– anon2000
Nov 12 '18 at 15:31
try it out now, i changed it
– vencaslac
Nov 12 '18 at 15:35
@vencaslac the error that was occurring is now fixed but when the whole string is uppercase, it returns false
– anon2000
Nov 12 '18 at 15:43
|
show 2 more comments
1
you forgot braces in the secondlower
– Vassiliy Vorobyov
Nov 12 '18 at 15:30
that i did, thanks
– vencaslac
Nov 12 '18 at 15:31
There's a condition where all the possible cases do need to be checked. As a word where one of the middle letters are uppercase (e.g 'pytHon') is meant to return false; but making it all lowercase will always return True
– anon2000
Nov 12 '18 at 15:31
try it out now, i changed it
– vencaslac
Nov 12 '18 at 15:35
@vencaslac the error that was occurring is now fixed but when the whole string is uppercase, it returns false
– anon2000
Nov 12 '18 at 15:43
1
1
you forgot braces in the second
lower
– Vassiliy Vorobyov
Nov 12 '18 at 15:30
you forgot braces in the second
lower
– Vassiliy Vorobyov
Nov 12 '18 at 15:30
that i did, thanks
– vencaslac
Nov 12 '18 at 15:31
that i did, thanks
– vencaslac
Nov 12 '18 at 15:31
There's a condition where all the possible cases do need to be checked. As a word where one of the middle letters are uppercase (e.g 'pytHon') is meant to return false; but making it all lowercase will always return True
– anon2000
Nov 12 '18 at 15:31
There's a condition where all the possible cases do need to be checked. As a word where one of the middle letters are uppercase (e.g 'pytHon') is meant to return false; but making it all lowercase will always return True
– anon2000
Nov 12 '18 at 15:31
try it out now, i changed it
– vencaslac
Nov 12 '18 at 15:35
try it out now, i changed it
– vencaslac
Nov 12 '18 at 15:35
@vencaslac the error that was occurring is now fixed but when the whole string is uppercase, it returns false
– anon2000
Nov 12 '18 at 15:43
@vencaslac the error that was occurring is now fixed but when the whole string is uppercase, it returns false
– anon2000
Nov 12 '18 at 15:43
|
show 2 more comments
Try this:
def is_english_word(string):
with open("english_words.txt", "r") as fileObject:
text = fileObject.read()
return string in text or (string[0].lower() + string[1:]) in text
It works! Thank you. However, it also returns True for when one of the middle letters in the word is uppercase, where it shouldn't. Eg 'pyThon' should return false but it returns True
– anon2000
Nov 12 '18 at 15:30
I've updated an answer. Try it out.
– Vassiliy Vorobyov
Nov 12 '18 at 15:38
I understand what you tried doing by checking if the first letter is uppercase, but when it is, the function returns False for some reason
– anon2000
Nov 12 '18 at 15:39
one more update
– Vassiliy Vorobyov
Nov 12 '18 at 15:40
1
What about strings of length 1?
– colidyre
Nov 12 '18 at 16:04
|
show 2 more comments
Try this:
def is_english_word(string):
with open("english_words.txt", "r") as fileObject:
text = fileObject.read()
return string in text or (string[0].lower() + string[1:]) in text
It works! Thank you. However, it also returns True for when one of the middle letters in the word is uppercase, where it shouldn't. Eg 'pyThon' should return false but it returns True
– anon2000
Nov 12 '18 at 15:30
I've updated an answer. Try it out.
– Vassiliy Vorobyov
Nov 12 '18 at 15:38
I understand what you tried doing by checking if the first letter is uppercase, but when it is, the function returns False for some reason
– anon2000
Nov 12 '18 at 15:39
one more update
– Vassiliy Vorobyov
Nov 12 '18 at 15:40
1
What about strings of length 1?
– colidyre
Nov 12 '18 at 16:04
|
show 2 more comments
Try this:
def is_english_word(string):
with open("english_words.txt", "r") as fileObject:
text = fileObject.read()
return string in text or (string[0].lower() + string[1:]) in text
Try this:
def is_english_word(string):
with open("english_words.txt", "r") as fileObject:
text = fileObject.read()
return string in text or (string[0].lower() + string[1:]) in text
edited Nov 12 '18 at 15:40
answered Nov 12 '18 at 15:27
Vassiliy VorobyovVassiliy Vorobyov
1,11721434
1,11721434
It works! Thank you. However, it also returns True for when one of the middle letters in the word is uppercase, where it shouldn't. Eg 'pyThon' should return false but it returns True
– anon2000
Nov 12 '18 at 15:30
I've updated an answer. Try it out.
– Vassiliy Vorobyov
Nov 12 '18 at 15:38
I understand what you tried doing by checking if the first letter is uppercase, but when it is, the function returns False for some reason
– anon2000
Nov 12 '18 at 15:39
one more update
– Vassiliy Vorobyov
Nov 12 '18 at 15:40
1
What about strings of length 1?
– colidyre
Nov 12 '18 at 16:04
|
show 2 more comments
It works! Thank you. However, it also returns True for when one of the middle letters in the word is uppercase, where it shouldn't. Eg 'pyThon' should return false but it returns True
– anon2000
Nov 12 '18 at 15:30
I've updated an answer. Try it out.
– Vassiliy Vorobyov
Nov 12 '18 at 15:38
I understand what you tried doing by checking if the first letter is uppercase, but when it is, the function returns False for some reason
– anon2000
Nov 12 '18 at 15:39
one more update
– Vassiliy Vorobyov
Nov 12 '18 at 15:40
1
What about strings of length 1?
– colidyre
Nov 12 '18 at 16:04
It works! Thank you. However, it also returns True for when one of the middle letters in the word is uppercase, where it shouldn't. Eg 'pyThon' should return false but it returns True
– anon2000
Nov 12 '18 at 15:30
It works! Thank you. However, it also returns True for when one of the middle letters in the word is uppercase, where it shouldn't. Eg 'pyThon' should return false but it returns True
– anon2000
Nov 12 '18 at 15:30
I've updated an answer. Try it out.
– Vassiliy Vorobyov
Nov 12 '18 at 15:38
I've updated an answer. Try it out.
– Vassiliy Vorobyov
Nov 12 '18 at 15:38
I understand what you tried doing by checking if the first letter is uppercase, but when it is, the function returns False for some reason
– anon2000
Nov 12 '18 at 15:39
I understand what you tried doing by checking if the first letter is uppercase, but when it is, the function returns False for some reason
– anon2000
Nov 12 '18 at 15:39
one more update
– Vassiliy Vorobyov
Nov 12 '18 at 15:40
one more update
– Vassiliy Vorobyov
Nov 12 '18 at 15:40
1
1
What about strings of length 1?
– colidyre
Nov 12 '18 at 16:04
What about strings of length 1?
– colidyre
Nov 12 '18 at 16:04
|
show 2 more comments
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%2f53265205%2ffunction-to-find-a-string-within-text-file-python%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
The first fileObject.read() reads all the file content, so the second call reads nothing.
– kantal
Nov 12 '18 at 15:30
@kantal how do you make it read one line at a time
– anon2000
Nov 12 '18 at 15:32
for s in fileObject:
– kantal
Nov 12 '18 at 15:37
Maybe I've misread your question, but why not simply doing
def is_english_word(string): with open("english_words.txt", "r") as fileObject: if string.lower() in fileObject.read(): return True
? So justs lowering input string to be consistent to the text.– colidyre
Nov 12 '18 at 15:46
@colidyre because if the input string has an uppercase letter in the middle of string whilst the rest of the letters are lowercase, the return value should be false. Doing what you described above would simply return it true and not take into account the cases of the letters
– anon2000
Nov 12 '18 at 15:48