Question

Could someone help with this function Python? It involves opening and reading from a text file....

Could someone help with this function Python? It involves opening and reading from a text file.

Write a function that returns a list k of words that start at word i of the text.

def extractWord(text, i=0, k=None)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

def extractWord(text, i=0, k=None):

k=[] # defining an empty list

word="" # defining an empty string

with open(text, 'r') as myfile: # reading file

data = myfile.read() # storing all text of file in data

for x in data: # for every character in data

if x == " " or x == "\n": # if character is space or line change (\n)

k.append(word) # word is added in list

word="" # word string is emptied

x="" # character is emptied

word=word+x # word + character, making full word

return k

filename = 'HomeworkLib.txt'

print(extractWord(filename))

SAMPLE FILE INPUT:

SAMPLE OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Could someone help with this function Python? It involves opening and reading from a text file....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Python, if you could help me with the code # Create a modified version of the search linear function defined # above to return all # occurrences of the search word in the text # An occurrence is...

    Using Python, if you could help me with the code # Create a modified version of the search linear function defined # above to return all # occurrences of the search word in the text # An occurrence is the index of a word in the text that matches your given # search string. # e.g. if "hatter" occurs at positions 0, 6, 12 then return [ 0, 6, 12] def search_linear_occurrences(xs, target): """ Find and return a list of...

  • Using Python, if you could help me with the code # Create a modified version of...

    Using Python, if you could help me with the code # Create a modified version of the search linear function defined # above to return all # occurrences of the search word in the text # An occurrence is the index of a word in the text that matches your given # search string. # e.g. if "hatter" occurs at positions 0, 6, 12 then return [ 0, 6, 12] def search_linear_occurrences(xs, target): """ Find and return a list of...

  • IN PYTHON 3: Write a Python program to read the attached text file containing words, count...

    IN PYTHON 3: Write a Python program to read the attached text file containing words, count the occurrence of each unique word and store the words in alphabetical order in a text file listing the word and the total count of that word. You cannot use any advanced features such as dictionaries. The program requires user input to determine the name of the file. (example of the text file that needs to be read = for a brief moment I...

  • Python problem If a variable training_text represents a file open for reading, then training_text.read() returns a...

    Python problem If a variable training_text represents a file open for reading, then training_text.read() returns a string containing the entire contents of the file. You can use the str method split to make your list. from typing import TextIO, List def get_words(training_text: TextIO) -> List[str]: ''' Return a new list containing the words from training_text in the name order as they appear in training_text. '''

  • QUESTION17 The following Python script aims to read lines from a text file and add them...

    QUESTION17 The following Python script aims to read lines from a text file and add them to a list Marlout oft. 00 Example P Flag question If the text file contains the following lines wordt wordz words the list will be ['word, word2, Words' after the execution of the program Question: Rearrange the following statements on the correct (logical) form Note: The-indicates the indentation #--> Program infile=onen/fileName --infile = open(fileName, 'r') . . -try: --for line in infile: --infile =...

  • python Create a program to open a text file for reading, find the maximum number in...

    python Create a program to open a text file for reading, find the maximum number in the file, determine if that maximum number is even, and write to an output text file. You should either write Yes if the number is even, otherwise write the maximum number. You should note the following: • Your input file must be named input.txt • The input file has one integer number per line • Your output file must be named output.txt • Your...

  • In Python 3 only please. A simple function that will be used on a file. commonpair(str)...

    In Python 3 only please. A simple function that will be used on a file. commonpair(str) – Takes a single string argument, representing the first word. This function should return the word that most frequently followed the given argument word (or one of, in case of ties). If the argument word does not appear in the text at all, or is never followed by another word (i.e., is the last word in the file), this function should return None. I...

  • Need some help on this Python Problem called "unique words" the text.txt file can be any...

    Need some help on this Python Problem called "unique words" the text.txt file can be any .txt file with words in it. Write a program that opens a specified text file and then displays the number of unique words in the text file after stripping all the white spaces, comma, and the punctuation marks and list of all the unique words found in the file. The list should be sorted in alphabetical order. The program should handle the ‘file not...

  • Task 1: Reading files Part A - Read a word Implement a function word from.file(file) that...

    Task 1: Reading files Part A - Read a word Implement a function word from.file(file) that takes a file as input and returns the first word in the file. Hint: the string methods split and strip will be useful. Example: calling word from file('files/task1A.txt') returns 'Once'. Part B - Read a table Implement a function nested int.list from file(file) that takes a file containing lines of integers that are separated by commas as input and returns the file contents formatted...

  • (Python 3) Write a program that reads the contents of a text file. The program should...

    (Python 3) Write a program that reads the contents of a text file. The program should then create a dictionary in which the keys are individual words found in the file and the values are the number of times each word appears and a list that contains the line numbers in the file where the word (the key) is found. Then the program will create another text file. The file should contain an alphabetical listing of the words that are...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT