Question

(1)Write a program in Python that reads an arbitrary-length file. All lines that start with semicolons...

(1)Write a program in Python that reads an arbitrary-length file.

All lines that start with semicolons (;) or pound signs (#) should be ignored.

All empty lines must be ignored

Lines containing a string between square brackets are sections.

Within each section, lines may contain numbers or strings.

For each section, calculate the lowest, highest and modal (most common) number, and print it. In each section, also count all non-whitespace characters, and print that.

Example input:

; this is a demo file
[ section 1 ]
23
23
43
56
Hello, World!

# next section
[section 2]
19.4
4.19
We love Python!
Well,... mostly.

Your program must print the following output

section 1: lowest=23, highest=56, mode=23, characters=12
section 2: lowest=4.19, highest=19.4, mode=4.19,19.4, characters=28
0 0
Add a comment Improve this question Transcribed image text
Answer #1

def is_int(s):
try:
float(s)
return True
except ValueError:
return False

import re
a = open("input.txt","r")
b = a.readlines()

for i in b:
if(i[0] == ";" or i[0] == "#" or i[0] == " "):
b.remove(i)

trimmedListIndices = []
for i in range(len(b)):
if(b[i][0] == "["):
trimmedListIndices.append(i)
trimmedListIndices.append(len(b))

for i in range(len(trimmedListIndices)-1):
extractedArray = b[trimmedListIndices[i]:trimmedListIndices[i+1]]
  
s = str(extractedArray[0])
val = s.split('[', 1)[1].split(']')[0]
  
for j in range(1,len(extractedArray),1):
high = 0
low = 0
length = 0
int_arr = []
if(is_int(extractedArray[j])):
int_arr.append(extractedArray[j])
else:
length += len(extractedArray[i])
print("%s: lowest=%d, highest=%d,characters=%d",%(val, min(int_arr),max(int_arr),length))   
  

  

Add a comment
Know the answer?
Add Answer to:
(1)Write a program in Python that reads an arbitrary-length file. All lines that start with semicolons...
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 Python program to read lines of text from a file. For each word (i.e,...

    Write a Python program to read lines of text from a file. For each word (i.e, a group of characters separated by one or more whitespace characters), keep track of how many times that word appears in the file. In the end, print out the top twenty counts and the corresponding words for each count. Print each value and the corresponding words, in alphabetical order, on one line. Print this in reverse sorted order by word count. You can assume...

  • Write a program that reads in a text file, infile.txt, and prints out all the lines...

    Write a program that reads in a text file, infile.txt, and prints out all the lines in the file to the screen until it encounters a line with fewer than 4 characters. Once it finds a short line (one with fewer than 4 characters), the program stops. For your testing you should create a file named infile.txt. Only upload your Python program, I will create my own infile.txt. Please use a while-loop BUT do not use break, Exit or Quit...

  • In python programming Write a program that opens the EmployeeSalaries.txt file and reads the salaries into...

    In python programming Write a program that opens the EmployeeSalaries.txt file and reads the salaries into a list. The program should output: The lowest salary in the list The highest salary in the list The total of all salaries in the list The average of all salaries in the list

  • Write a program that reads a file containing an arbitrary number of text integers that are...

    Write a program that reads a file containing an arbitrary number of text integers that are in the range 0 to 99 and counts how many fall into each of eleven ranges. The ranges are 0-9, 10-19, 20-29,..., 90-99 and an eleventh range for "out of range." Each range will correspond to a cell of an array of ints. Find the correct cell for each input integer using integer division. After reading in the file and counting the integers, write...

  • Write a program in python that reads each line in a file, reverses its lines, and...

    Write a program in python that reads each line in a file, reverses its lines, and writes them to another file. For example, if the file input.txt contain the lines: Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go. and you run your Python file then output.txt contains The lamb was sure to go. And everywhere that Mary went Its fleece was white as snow Mary had...

  • (IN PYTHON) You are to develop a Python program that will read the file Grades-1.txt that...

    (IN PYTHON) You are to develop a Python program that will read the file Grades-1.txt that you have been provided on Canvas. That file has a name and 3 grades on each line. You are to ask the user for the name of the file and how many grades there are per line. In this case, it is 3 but your program should work if the files was changed to have more or fewer grades per line. The name and...

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

  • For each problem, you must: Write a Python program Test, debug, and execute the Python program...

    For each problem, you must: Write a Python program Test, debug, and execute the Python program Save your program in a .py file and submit your commented code to your Student Page. Note regarding comments: For every class, method or function you create, you must provide a brief description of the what the code does as well as the specific details of the interface (input and output) of the function or method. (25 pts.) Write a program that reads in...

  • Write a PYTHON program that asks the user for the name of the file. The program...

    Write a PYTHON program that asks the user for the name of the file. The program should write the contents of this input file to an output file. In the output file, each line should be preceded with a line number followed by a colon. The output file will have the same name as the input filename, preceded by “ln” (for linenumbers). Be sure to use Try/except to catch all exceptions. For example, when prompted, if the user specifies “sampleprogram.py”...

  • 1. Write a python program that reads a file and prints the letters in increasing order...

    1. Write a python program that reads a file and prints the letters in increasing order of frequency. Your program should convert entire input to lower case and only counts letters a-z. Special characters or spaces should not be counted. Each letter and it's occurrences should be listed on a separate line. 2. Test and verify your program and it's output. ---Please provide a code and a screenshot of the code and output. Thank you. ----

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