Question

python

Complete the below function that takes a file name and a list as arguments and
reads the text within the corresponding file, then creates a dictionary whose
keys are the characters of the provided list, and the values are the counts of
these characters within the text. If a character in the list is not included in 
the text, "0" should be written in the dictionary as the value of this 
character. The function returns the created dictionary as a result. If the file 
does not exist, then the function returns an empty dictionary.

Here is an example case, assuming a file named "sample.txt" exists with the 
following content:

----- sample.txt -----
This is an example sentence.
This is yet another sentence.
-------------------------

>>> text2dict("sample.txt", ['a', 'b', 'c', 't'])
{'a':3, 'b':0, 'c':2, 't':4}

"""

def text2dict(filename, characters):
    return # Remove this line to answer this question


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

program

import os
from os import path

def text2dict(filename):
data_dict={}
if path.exists(filename):
f = open(filename, "r")
data=f.read()
print(data)
for i in data:
if i!='\n':
if i in data_dict:
data_dict[i]=data_dict[i]+1
else:
data_dict[i]=1
  
  
return data_dict

  
filename="C:/Users/user/IR_WORK/text.txt"
dict=text2dict(filename)
print(dict)

output

runfile('C:/Users/user/IR_WORK/chegg.py', wdir='C:/Users/user/IR_WORK')
hello
name
{'h': 1, 'e': 2, 'l': 2, 'o': 1, 'n': 1, 'a': 1, 'm': 1}

C:\Users\user\IR_WORK Е, 2 ас chegg.py X Name Type Size Value dict dict 7 {h:1, e:2, l:2, o:1, n:1, a:1, m:1} f


answered by: ANURANJAN SARSAM
Add a comment
Know the answer?
Add Answer to:
python
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
  • Write a function called char_counter that counts the number of a certain character in a text file. The function takes tw...

    Write a function called char_counter that counts the number of a certain character in a text file. The function takes two input arguments, fname, a char vector of the filename and character, the char it counts in the file. The function returns charnum, the number of characters found. If the file is not found or character is not a valid char, the function return -1. As an example, consider the following run. The file "simple.txt" contains a single line: "This...

  • Write a function called char_counter that counts the number of a certain character in a text file. The function takes tw...

    Write a function called char_counter that counts the number of a certain character in a text file. The function takes two input arguments, fname, a char vector of the filename and character, the char it counts in the file. The function returns charnum, the number of characters found. If the file is not found or character is not a valid char, the function return -1. As an example, consider the following run. The file "simple.txt" contains a single line: "This...

  • Write a function called char_counter that counts the number of a certain character in a text file. The function takes tw...

    Write a function called char_counter that counts the number of a certain character in a text file. The function takes two input arguments, fname, a char vector of the filename and character, the char it counts in the file. The function returns charnum, the number of characters found. If the file is not found or character is not a valid char, the function return -1. As an example, consider the following run. The file "simple.txt" contains a single line: "This...

  • Hey guys I need help with this assignment. However it contains 7 sub-problems to solve but I figured only 4 of them can...

    Hey guys I need help with this assignment. However it contains 7 sub-problems to solve but I figured only 4 of them can be solved in one post so I posted the other on another question so please check them out as well :) Here is the questions in this assignment: Note: Two helper functions Some of the testing codes for the functions in this assignment makes use of the print_dict in_key_order (a dict) function which prints dictionary keyvalue pairs...

  • python

    Write a function that reads a single line of numbers from a file named  "question1.txt", and returns the average of the numbers read. For example, if  the file is as follows: ----- question1.txt ----- 1 3 5 -2 4 0 6 -2 -3 ------------------------- then the function must return 12/9 = 1.33333 """ def file_average():     return # Remove this line to answer this question

  • Python 12.10 LAB: Sorting TV Shows (dictionaries and lists) Write a program that first reads in...

    Python 12.10 LAB: Sorting TV Shows (dictionaries and lists) Write a program that first reads in the name of an input file and then reads the input file using the file.readlines() method. The input file contains an unsorted list of number of seasons followed by the corresponding TV show. Your program should put the contents of the input file into a dictionary where the number of seasons are the keys, and a list of TV shows are the values (since...

  • Help me with this Python Question a. build_word_dictionary (filename) – This builds a word dictionary indexed...

    Help me with this Python Question a. build_word_dictionary (filename) – This builds a word dictionary indexed by words from the file who’s filename is provided as an argument. It uses the words as keys and the count of occurrences as values. It returns the dictionary it constructed. It can use the ‘tokenize()’ function that is provided in the lecture slides. b. inverse_dict(dict) – This method takes a dictionary (generated by build_word_dictionary() and inverts it (as was done with students and...

  • Python 3 Define a function below, get_subset, which takes two arguments: a dictionary of strings (keys)...

    Python 3 Define a function below, get_subset, which takes two arguments: a dictionary of strings (keys) to integers (values) and a list of strings. All of the strings in the list are keys to the dictionary. Complete the function such that it returns the subset of the dictionary defined by the keys in the list of strings. For example, with the dictionary ("puffin": 5, "corgi": 2, "three": 3) and the list ("three", "corgi"), your function should return {"corgi": 2, "three"...

  • Hey guys I need help with this question with 3 sub-problems. f test remove short synonyms () Define the remove shorti s...

    Hey guys I need help with this question with 3 sub-problems. f test remove short synonyms () Define the remove shorti synonyms function which is passed a dictionary as a parameter- The keys of the parameter dictionary are words and the corresponding values are 1ists of synonyms (synonyms are words which have the same or nearly the same meaning). The function romoves all the eynonyme which have ous than 8 charactors from each corresponding list of synonyms-As well, the funet...

  • [Java] Create a class called FileExercises that contains the following method Method encrypt that does not...

    [Java] Create a class called FileExercises that contains the following method Method encrypt that does not return anything and takes a shift, input filename and output filename as arguments. It should read the input file as a text file and encrypt the content using a Caesar cypher with the shift provided. The resulting text should be placed in the output file specified. (If the output file already exists, replace the existing file with a new file that contains the required...

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
ADVERTISEMENT