Question

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


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
  • python

    I this question, you will be given a list of integers, and you have to  write in a text file named "question3.txt" a barplot corresponding to the  given numbers, as shown below. For example, if you are given [3,5,2,6,4],  then you will write in the file the following plot: ----- question3.txt -----       |   |   |   |   | | | |   | | | | | | | | | | | | 3 5 2 6 4 ------------------------- The given integers will be between 0 and 9 (both inclusive).  """ def file_plot(l1):     return # Remove this line to answer this question

  • python

    Write a function that takes two lists of integers X and Y as input, and  computes and writes a table into a file named "question2.txt" as follows: - On each line the computations for an X,Y pair is given  - Each such possible pair must exist in the file in the same order of values in X and Y - Next on each line, you provide X+Y, X*Y and X^2 + Y^2, all separated by a  comma (and no spaces or other characters must exist). Note that X^2 represents  "square of X".  For example, given X = [1,3] and Y=[-1,1,0], "question2.txt" should contain the following table: ----- question2.txt ----- 1,-1,0,-1,2 1,1,2,1,2 1,0,1,0,1 3,-1,2,-3,10 3,1,4,3,10 3,0,3,0,9 ------------------------- As you can see, the first line is computed for X=1, Y=-1. The second line is  built for X=1, Y=1; and the third line is for X=1, Y=0. It goes on like this,  until X=3, Y=0.  """ def op2file(X, Y):     return # Remove this line to answer this question

  • Python DESCRIPTION Write a program that will read an array of integers from a file and...

    Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....

  • 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

  • average_value_in_file / Python 3.x: Your assistance is appreciated, thank you! Write a function named average_value_in_file that...

    average_value_in_file / Python 3.x: Your assistance is appreciated, thank you! Write a function named average_value_in_file that accepts a file name as a parameter and reads that file, assumed to be full of numbers, and returns the average (mean) of the numbers in that file. The parameter, filename, gives the name of a file that contains a list of numbers, one per line. You may assume that the file exists and follows the proper format. For example, if a file named...

  • python

    pythonComplete the below function that takes the name of two files, inFilename andoutFilename as arguments and reads the text from the inFilename. In this fileeach line contains the Turkish Republic Identity Number (TCNO), name, surnameand telephone number of a person. Your function should sort all personsaccording to their TCNO, write the sorted data into outFilename. If the fileinFilename does not exist, then the function must create an empty file namedoutFilename.For example, if the function is called such asreadText("in.txt", "out.txt")and in.txt...

  • Python Please, If possible, please continue with/ use code already given File Commands: u/a Write a...

    Python Please, If possible, please continue with/ use code already given File Commands: u/a Write a function named, file_commands, that takes the name of a file as a parameter. The function processes the contents the file as follows: For file lines that begin with the letter 'a', calculate & print the integer average of the numbers on the line. For file lines that begin with the letter 'u', print the upper case format for each word following the u. Sample...

  • I'm a bit confused on how to get this program to run right. Here are the...

    I'm a bit confused on how to get this program to run right. Here are the directions: Part 1: Write a Python function called reduceWhitespace that is given a string line and returns the line with all extra whitespace characters between the words removed. For example, ‘This line has extra space characters ‘  ‘This line has extra space characters’ Function name: reduceWhitespace Number of parameters: one string line Return value: one string line The main file should handle the...

  • In Python Please! 16.42 Lab 13B: Palindromes with Files Overview This is a demonstration of reading...

    In Python Please! 16.42 Lab 13B: Palindromes with Files Overview This is a demonstration of reading and writing files. Objectives Be able to read from an input file, perform string manipulation on each line of the file, and write to an output file. Provided input file: A single input file named myinput.txt is provided that contains a few lines of text. bob sees over the moon never odd or even statistics dr awkward Provided output file: A single output file...

  • Please write the following code as simple as possible in python: You will need to define...

    Please write the following code as simple as possible in python: You will need to define a function with four arguments. Here is what I used: > def find_matches(file1.txt, output1.txt, strings, value): file1.txt will contain a list of various strings. The program must copy from the first argument, and it should be written in the second argument (the second file, "output1.txt"). The third and fourth arguments will determine which specific strings will be copied over to the second file. For...

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