Question

I need help with one small part of the following question. I think I know how...

I need help with one small part of the following question. I think I know how to do all the things the question is asking I just can’t figure out how to get it to open the text file. I tried Filename =input “vacation.txt” but that didn’t open the file. Does the program have to direct it to "find" the program? HERE IS THE COMPLETE QUESTION: Create a Python program that: Opens a file and reads the contents of the following file: vacation.txt -The file contains several lines of data, with each data item separated by commas. The data is: a. A vacation date b. The amount of fuel used on the vacation c. The number of miles traveled in the vacation. For each of the lines of data in the file, separate the data items and put them in appropriate variables (the file contents are in strings so you must convert the numbers in the data into the proper type) 1.Calculate the fuel mileage achieved on each vacation and display on a single line: a. Vacation date b. Amount of fuel used c. Number of miles traveled d. Fuel mileage calculated for each vacation 2.Create a new file to save the data read and the fuel mileage calculated for the vacation a. Write the data on separate lines and for each vacation in the data with each item separated by commas (remember that all the data must be saved in the string form) 3. Remember that the program should: a. Use variables with reasonable names b. Display easily understood prompts when collecting user input c. Prompt the user for the name of the file to read the data from and the name of the file to store the revised data d. Have comments explaining what each part of the program is doing Here is the data in the file vacation.txt 12-2-2019, 13, 244 12-23-2019, 17, 303 1-16-2020, 12, 210 2-10-2020, 25, 560 2-20-2020. 17, 412

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

python code for above problem is attached below :-

filename = input('Enter the file name in txt format: ');

filename = filename+'.txt'

data = [] #empty list to collect data from file

with open(filename) as fp: #openning file as fp

    line = fp.readline().strip() #reading file line by line and removing any extra spaces

    data.append(line)   #adding this read line to the data List

    while line: #continue reading until line is found

        line = fp.readline().strip() #reading file line by line and removing any extra spaces

        data.append(line) #adding this read line to the data List

data.remove('') #removing any empty space read from the file and stored in list

f= open("output.txt","w+") #file object to write data to another file named "output.txt"

for i in data: #iterating over data list

    s = i.split(',') #splitting the data on the delimiter ',' and making a list

    fuel_used = int(s[1].strip()) #taking 2nd item removing any extra space from the list items

    miles_travelled = int(s[2].strip()) #Taking 3 item and removing any extra space from the list items

    fuel_mileage = miles_travelled/fuel_used #calculating fuel mileage for each date

    # displaying information on console as specified in question

    print("for Vacation date : {0}, Amount of fuel used: {1}, Number of miles traveled: {2}, fuel mileage: {3}".format(s[0],s[1],s[2],fuel_mileage))

    # writing vacation date,fuel used,miles travelled, and fuel mileage to output.txt file separated by a ','

    f.write("{0}, {1}, {2},  {3} \n".format(s[0],s[1],s[2],fuel_mileage))  

# closing file output.txt

f.close();

code screenshot : -

input file: -

vacation.txt

output : -

output file (output.txt ) : -

if my answer helped then please upvote and comment for any queries

Thankyou !

Add a comment
Know the answer?
Add Answer to:
I need help with one small part of the following question. I think I know how...
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
  • I need help building code in python for this: Create a program that: Creates a sales...

    I need help building code in python for this: Create a program that: Creates a sales receipt, displays the receipt entries and totals, and saves the receipt entries to a file Prompt the user to enter the Item Name Item Quantity Item Price Display the item name, the quantity, and item price, and the extended price (Item Quantity multiplied by Item Price) after the entry is made Save the item name, quantity, item price, and extended price to a file...

  • ****THIS IS A 2 PART QUESTION! I ONLY NEED THE ANSWER TO PART 2 ON HOW...

    ****THIS IS A 2 PART QUESTION! I ONLY NEED THE ANSWER TO PART 2 ON HOW TO SEND THE DATA SERIALIZED**** Write a c# program that stores student grades in a text file and read grades from a text file.  The program has the following GUI: There are four buttons: Create File, Save Case, Close File and Load File.  Initially, only the Create File and Load File buttons are enabled.  If the user clicks the Create File button, a Save File Dialog window...

  • c++ question, i put the txt attachment picture for this question... please include comments on calculations...

    c++ question, i put the txt attachment picture for this question... please include comments on calculations and varibles Description In this program, you will write a program to emulate a vending machine (somewhat). In this version of the program, you will use a struct type defined below struct snackType string name; string code; double price; int remaining; Where name contains the snack's name, code contains the 2 digit code for the item, price holds the item's price, and remaining holds...

  • Ive done this part but i want to know how i can change the file so...

    Ive done this part but i want to know how i can change the file so it accepts inputs and a file input PLEASE HELP PYTHON Write a program that computes the fuel efficiency of a multi-leg journey. The program will first prompt for the starting odometer reading and then get information about a series of legs. For each leg, the user enters the current odometer reading and the amount of gas used (separated by a space). The user signals...

  • What to submit: your answers to exercises 2. Write a Java program to perform the following...

    What to submit: your answers to exercises 2. Write a Java program to perform the following tasks: The program should ask the user for the name of an input file and the name of an output file. It should then open the input file as a text file (if the input file does not exist it should throw an exception) and read the contents line by line. It should also open the output file as a text file and write...

  • This question is for Python 3. I can get the first couple of steps in this...

    This question is for Python 3. I can get the first couple of steps in this question but I get errors. Create a program that: Creates a sales receipt, displays the receipt entries and totals, and saves the receipt entries to a file Prompt the user to enter the Item Name Item Quantity Item Price Display the item name, the quantity, and item price, and the extended price (Item Quantity multiplied by Item Price) after the entry is made Save...

  • Need help Purpose Calculate mileage reimbursements using arrays and methods. The Mathematical Association of America hosts...

    Need help Purpose Calculate mileage reimbursements using arrays and methods. The Mathematical Association of America hosts an annual summer meeting. Each state sends one official delegate to the section officers’ meeting at this summer session. The national organization reimburses the official state delegates according to the scale below. Write a Java program to calculate the reimbursement values, satisfying the specifications below. Details on array and method usage follow these specs. 1. The main method should declare all the variables at...

  • PYTHON CODING HELP: BELOW ARE THE STEPS I NEED HELP WITH PLEASE. :) First create a...

    PYTHON CODING HELP: BELOW ARE THE STEPS I NEED HELP WITH PLEASE. :) First create a text file named "items.txt" that has the following data in this order: Potatoes Tomatoes Carrots ... and have it be in the same directory as the Python program you are coding for this assignment. IMPORTANT: Your program should work with any size file. It should work with 100 items listed or with no items in the file! Write a Python program that ... 1....

  • I need help with MIPS-32 assembly language. My question is how to open an existing text...

    I need help with MIPS-32 assembly language. My question is how to open an existing text file, read from the file, display contents, and close the file read in MIPS32 using MARS 4.5 MIPS simulator. Existing text file is testfile.txt testfile.txt contains: 001010100101001100

  • Python Programming 4th Edition: Need help writing this program below. I get an error message that...

    Python Programming 4th Edition: Need help writing this program below. I get an error message that says that name is not defined when running the program below. My solution: def main(): filename = "text.txt" contents=f.read() upper_count=0 lower_count=0 digit_count=0 space_count=0 for i in contents: if i.isupper(): upper_count+=1 elif i.islower(): lower_count+=1 elif i.isdigit(): digit_count+=1 elif i.isspace(): space_count+=1 print("Upper case count in the file is",upper_count) print("Lower case count in the file is",lower_count) print("Digit count in the file is",digit_count) print("Space_count in the file is",space_count)...

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