Question: Q:Write Python code that reads text from a provided file, searches for a specific word in the text, and returns the position ( line number
Q:Write Python code that reads text from a provided file, searches for a specific word in the text, and returns the position line number of the first occurrence of this word.
Instructions:
read the contents of the file sample.txt written below as example
Iterate through each line of the text to find the first occurrence of the word. Remember to keep track of the line number.
Once the word is found, return the line number to the user.
If the word is not found after iterating through all lines, inform the user that the word does not exist in the text by returning "Does not exist".
Example: Sample.txt
Hello and welcome to the Python exercise.
This text contains multiple lines and words for you to search. Try finding different words to see how effective your function is Remember the position should reflect the line number of the text that you want to search. Good luck with your Python coding journey.
If the word to be searched is "Python", the expected output should be "Line Write a function findword and make sure the output eg Line is returned using the function.
Hint: You may start with the code below to first read the text from the file as separate lines.
with opensampletxt as file:
text file.read
lines text.split
In this exercise, it's recommended to use the 'split function. The 'split' function is used to split a string into a list of substrings based on a specified delimiter. When you call split on a string, you provide the delimiter as an argument, and Python returns a list of substrings obtained by splitting the original string wherever the delimiter occurs.
For example:
text "apple banana orange"
fruits text.split # splits by space
fruits is a list with items apple "banana", "orange"
text "apole.banana.orange"
Answer:
Code below: Didnt work
def findwordfilename word:
This function reads a text file, searches for a specific word, and returns the line number of the first occurrence.
Args:
filename str: The name of the text file to read.
word str: The word to search for.
Returns:
str: The line number of the first occurrence of the word eg "Line or "Does not exist" if not found.
# Open the file in read mode
with openfilenamer as file:
# Initialize line counter
linenumber
for line in file:
# Remove leadingtrailing whitespace from the line optional for better handling
line line.strip
# Search for the word in the current line caseinsensitive
if word.lower in line.lower:
return fLine linenumber # Return the line number with formatting
# Increment line counter for the next iteration
linenumber
# If the loop completes without finding the word, return "Does not exist"
return "Does not exist"
# Example usage with sample.txt replace with your actual filename
wordtofind "Python" # Change this to the word you want to search
result findwordsampletxt wordtofind
printresult
Code below: didn't Work
def findwordword: with opensampletxt as file:
text file.read
lines text.split
linenumber
if word in lines:
return fLine Linenumber
linenumber
return "Does not exist"
# Tests
print
Find Word"
printfindwordPython printfindwordposition
printfindwordcomputer
Code : below: Worked
def findwordword:
with opensampletxt as file:
text file.readlines
linenumber # Start line numbering from
for line in text:
if word in line:
return fLine linenumber
linenumber
return "Does not exist" # Word not found in any line
# Tests
printFind Word:"
printfindwordPython
printfindwordposition
printfindwordcomputer
Explain In detail why Code and code were wrong and didn't work as compared to code that worked and identify the mistake done in code and code and mistakes corrected in code that worked
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
