filename = 'fruit.txt'
try:
f = open(filename, 'r')
for line in f:
words = line.strip().split()
print("Color of {} is {}".format(words[0], words[1]))
f.close()
except FileNotFoundError:
print(filename + " does not exists!")
python Write a program that reads the name of fruits and the colors from an input...
Python problem: 1. The colors red, blue, and yellow are known as the primary colors because they cannot be made by mixing other colors. When you mix two primary colors, you get a secondary color, as shown here: When you mix red and blue, you get purple. When you mix red and yellow, you get orange. When you mix blue and yellow, you get green. Create a text file with the names of two colors in it. Write a program...
IN PYTHON Write a program that reads two text files, take one sentence from each text file and print them one after another. Example: If line1 is from text1.txt and line2 is from text2.txt, then print line1 line2 Repeat this for all the lines of the two files
Write a PYTHON program that reads a file (prompt user for the input file name) containing two columns of floating-point numbers (Use split). Print the average of each column. Use the following data forthe input file: 1 0.5 2 0.5 3 0.5 4 0.5 The output should be: The averages are 2.50 and 0.5. a) Your code with comments b) A screenshot of the execution Version 3.7.2
using Python please
reverse file Writ te a function named reverse file. This function will take one parameter which will be the file name of the file to reverse. This function will open the file with the name passed in as a parameter and will print out all the lines of the file in reverse order. Below are a few examples: If the files lines.brt looks like: orange apple orange apple orange apple grape Then the following should look like...
Write a Python program that reads user input from the command-line. The program should define a function read Position, which reads the values for t, v0, and h0. If there is an IndexError, print 'Please provide the values for t, vO, and hO on the command line.'. If t, v0, or h0 are less than 0. print 't = # is not possible.' for each variable respectively. Note that the # represents the number entered on the command-line by the...
Write a program using python that reads from values from a text file and plots them using matplotlib. The input data for each graph is on three lines of the input textfile. The first line contains the x-coordiates of the points of the graph, and the second line contains the y-coordinates of the points of the graph that correspond, in the same order, to the x-coordinates on the first line. The third line in each group of three lines may...
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 python program that prompts the user for the names of two text files and compare the contents of the two files to see if they are the same. If they are, the scripts should simply output “Yes”. If they are not, the program should output “No”, followed by the first lines of each file that differ from each other. The input loop should read and compare lines from each file. The loop should break as soon as a...
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”...
7.2 Write a program that prompts for a file name, then opens that file and reads through the file, looking for lines of the form: X-DSPAM-Confidence: 0.8475 Count these lines and extract the floating point values from each of the lines and compute the average of those values and produce an output as shown below. Do not use the sum() function or a variable named sum in your solution. You can download the sample data at http://www.py4e.com/code3/mbox-short.txt when you are...