Question

use python to write and submit a script that asks for a filename (to which mbox.txt...

use python to write and submit a script that asks for a filename (to which mbox.txt and mbox-short.txt will be used) and looks for lines of the form

New Revision: 39772

Extract the number from each of the lines using a regular expression and the findall() method. Compute the average of the numbers, rounded to a single decimal place, and print out the average and the number of lines used for the computation.

Enter filename: mbox.txt

Average = 38549.8

Number of lines = 1790

Enter filename: mbox-short.txt

Average = 39756.9

Number of lines = 27

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


import re
def extractNumber(input,lst):
numbers = re.findall('\d+',input)
for num in numbers :
lst.append(int(num))
def Average(lst):
return float(sum(lst) / len(lst))   
# Driver program
if __name__ == "__main__":
filename = input("Enter filename: ")
lst = []
f = open(filename)
line = f.readline()
count = 0
while line:
extractNumber(line,lst)
line = f.readline()
count = count + 1
f.close()
print("Average = ", Average(lst))
print("Number of lines = ",count)
  

Files main.py ョっsaved Python 3.6.1 (default, Dec 2015, 13:05:11) [GCC 4.8.2] on linux D main py 3 def extractNumber (input,lst): 4 numbers -re.findall(d+,input) Enter filename: Average5.5 Number of lines mbox.txt 6 for num in numbers 10 1st.append (int (num)) 9 def Average(1st): 18 return float(sum(lst) len(1st)) 12 # Driver program 13 if name ==.. main 14 15 16 17 18 19 20 21 filename-input Enter filename: ) 1st = [] f open(filename) line f.readline() count-0 while line: extractNumber (line,lst line -f.readline() count-count 1 23 24 25 26 27 28 29 f.close() print(Average , Average(lst)) print(Number of lines -count)

Add a comment
Know the answer?
Add Answer to:
use python to write and submit a script that asks for a filename (to which mbox.txt...
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
  • Script 1: Sum of Numbers Write a python program that asks the user to enter a...

    Script 1: Sum of Numbers Write a python program that asks the user to enter a series of single-digit numbers with nothing separating them. The program should display the sum of all the single digit numbers in the string. For example, if the user enters 2514, the method should return 12, which is the sum of 2, 5, 1, and 4.

  • In python please.. Problem 4 (Tracking Statistics – Part 1) Write a program that repeatedly asks...

    In python please.. Problem 4 (Tracking Statistics – Part 1) Write a program that repeatedly asks the user to enter an integer until they want to quit (e.g., by entering ‘q’). The program should then print the largest number entered, the smallest number entered, the average of all numbers entered, the number of positive numbers entered, and the number of negative numbers entered (0 should not count as positive or negative). Problem 5 (Tracking Statistics – Part 2) Copy your...

  • Use Python Write a script to perform various basic math and string operations. Use some functions...

    Use Python Write a script to perform various basic math and string operations. Use some functions from Python's math module. Generate formatted output. Basic math and string operations Calculate and print the final value of each variable. a equals 3 to the power of 2.5 b equals 2 b equals b + 3 (use +=) c equals 12 c = c divided by 4 (use /=) d equals the remainder of 5 divided by 3 Built-in functions abs, round, and...

  • Python Write a program which asks the user keep inputting values on a line followed by...

    Python Write a program which asks the user keep inputting values on a line followed by enter until they enter a blank line, at which point the program should print the number of lines entered (excluding the blank).

  • Write a script that asks the user to input a number. If the value the user...

    Write a script that asks the user to input a number. If the value the user enters is a negative value, call the pre-defined function neg_value which consumes no arguments and returns no values. If the user enters a positive value, call the pre-defined function pos_value which consumes no arguments and returns no values. If the user enters a zero, display the message "Invalid Value". Doing this on Python. This is what I have: number = int(input("Enter a number: "))...

  • Use Python Write a program that asks the user to enter the expression of a function...

    Use Python Write a program that asks the user to enter the expression of a function to a single variable x. From this expression, using the sympy library, do the following: Calculate the images for x ranging from 0 to 19

  • Python Script format please! 1. Write a script that takes in three integer numbers from the...

    Python Script format please! 1. Write a script that takes in three integer numbers from the user, calculates, and displays the sum, average, product, smallest, and largest of the numbers input. Important things to note: a. You cannot use the min() or max() functions, you must provide the logic yourself b. The calculated average must be displayed as an integer value (ex. If the sum of the three values is 7, the average displayed should be 2, not 2.3333). Example:...

  • Write a PYTHON program that allows the user to navigate the lines of text in a...

    Write a PYTHON program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the...

  • Python)) Data types of Python. Write the python script to show the different examples. Write the...

    Python)) Data types of Python. Write the python script to show the different examples. Write the Python script to present ALL the mathematical arithmetic operations. Write the Python script to display your - first name, last name, ID, college name and the program of study. Write the Python script to receive the number of seconds as input and display the hours, minutes and seconds as output. Assume the variable x to be with the string of "Day by day, dear...

  • Write a python program that repeatedly asks the user to input a pair of integers. The...

    Write a python program that repeatedly asks the user to input a pair of integers. The program should record the largest number of each pair into a list. The program should keep asking the user to input pairs of numbers until the user enters -1 for the first number. At this point the program should print the list on a single line, with each value separated by a space and then stop. Example of expected behaviour: Please enter first number:...

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