Respuesta :
Answer:
import pandas as pd
filename = input("Enter file name with the extension included: ")
data = {}
with open("filename", "r") as file:
content = file.readlines()
for line in content:
line_list = line.strip().split(" ")
data[line_list[0]] = line_list[1] * line_list[2]
del line_list
report = pd.DataFrame(list(data.items()), columns = ["lastname", "wages paid"])
print(report)
Explanation:
The python module prompts the user for the file name and opens the text file to extract the employee data with the python open function. It iterates over the file content and saves the last name and the wages paid to the employees ( the hours worked multiplied by the hourly rate) in the dictionary "data".
The dictionary is converted to a dataframe with the Pandas python package and displays the data in tabular format on the screen.
Following are the required code to make a report in tabular format by using the appropriate headers:
Python code:
file_name = input('Enter input filename: ')#defining a variable file_name that inputs file value
try:#defining try block that uses thr open method to open file
file = open(file_name, 'r')#defining file variable that opens file
except:#defining exception block when file not found
print('Error opening file ' , file_name)#print message
exit()#calling exit method to close program
print('{:<12s} {:>10s} {:>10s}'.format('Name', 'Hours', 'Total Pay'))#using print method to print headers
for l in file.readlines():#defining loop that holds file value and calculate the value
name, hour, wages = l.split()#defining variable that holds the value of file
hour = int(hour)#holiding hour value
wages = float(wages)#holiding wages value
total = hour * wages#defining variable total variable that calculates the total value
print('{:<12s} {:>10d} {:>10.2f}'.format(name, hour, total))#print calculated value with message
file.close()#close file
Required file (data.txt) with the value:
Database 34 99
base 30 90
case 34 99
Program Explanation:
- Defining the variable "file_name" that uses the input method to input the file name with an extension.
- Using the exception handling to check the file, with using the try block and except block.
- In the try block check file, and in except block print message with the exit method when file not found.
- In the next line, a print method has used that prints the header and uses a loop to read the file value.
- Inside the loop, a header variable is used that splits and holds the file value, calculates the value, prints its value with the message, and closes the file.
Output:
Please find the attached file.
Find out more information about the Program code here:
brainly.com/question/18598123