Question

Create a program (Lab9_Act1_Write.py) that will read in data from the keyboard and store it in...

Create a program (Lab9_Act1_Write.py) that will read in data from the keyboard and store it in a file. Your program should prompt the user for the name of the file to use and then request values be entered until the user is done. This data should be written to two files: a text file, user_file_name.txt and a comma separated value file, user_file_name.csv.

To help you with decision making, we suggest that you ask the user to input data representing students’ grades for different assignments (that is, create a grade book with at least four grade columns as shown below). The first row will contain the column headers, the second row will contain the assignment point values. Starting with row three (3), each row will contain a student name (first and last) and the grades earned by the student. Note the required formatting of the text file: choose a column width (may vary from column to column); the first column is left-justified; all other columns are right-justified.

user_file_name.txt

Name hw1 hw2 mid-term final

Point value 10 10 100 100

Petr Little 9 8 85 78

Sam Tarley 10 10 99 100

Joff King 4 2 55 61

user_file_name.csv

Name,hw1,hw2,mid-term,final

Point value,10,10,100,100

Petr Little,9,8,85,78

Sam Tarley,10,10,99,100

Joff King,4,2,55,61

a) As an example, you might first ask for a file name and open the two files for writing, Lab Assignment # 9 2

b) You may want to ask about the number of columns in the data table or not (your choice)

c) Prompt the user to enter the data for row 1 (header row), then row 2 (another header row), then row 3, etc.

i. You need to tell the user how to enter data, for example: “enter data separating columns by colon, or semicolon, or vertical bar, etc.” (please, do not use comma)

ii. You’ll need some way to stop the process (e.g. the user types nothing and presses the Enter key or the user enters -1, etc.)

iii. Suggestion: please build a pyramid, not an arch (you know what this means, right?)

d) Your program should write each row of the table to the files. i. It’s your choice as to whether you do this as the data is read, or wait until all the data is entered before saving. ii. Note that the data should be formatted before writing to the output files.

e) To test this code, enter at least five rows of data (see the example given above). Then open the files you created with a text editor and Excel and check that the code worked properly

PYTHON PLEASE!!!!!!

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

Х Lè driver.py - C:\Users\krishna Downloads\driver.py (3.8.5) File Edit Format Run Options Window Help 1 # importing csv modu22 # left justifing the first element 23 string += row[index].ljust(15) 24 else: 25 # right justifying the remaining elements43 42 while(True): # taking data from user and storing in row 44 input(Enter the data: ) row = 45 46 47 # checking if user62 63 # opening the given file in write text mode 64 with open((file_name + .txt), wt) as textfile: 65 # iterating over e81 82 83 Ist[0] = lst[0] + + lst[1] # deleting the last name from the data(lst) del lst[1] else: Ist = data[index].split() 84========== RESTART: C:\Users\krishna\Downloads\driver.py ========== ========== Enter the name of the file: user_file_name EntA1 fic Name hw1 1 Name 2 Point value 3 Petr Little 4 Sam Tarley 5 Joff King 6 hw2 10 9 10 mid-term final 10 8 8 10 2 4- х user_file_name - Notepad File Edit Format View Help Name Point value Petr Little Sam Tarley Joff King hw1 10 9 10 hw2 mid

Raw_code:

# importing csv module
import csv

# reading file name from user
file_name = input("Enter the name of the file: ")

# temporary variables
data = []
row = []

# suggesting the user to enter data in required format
print("Enter the data seperating columns by semicolon(;).")

# taking first header from user
header1 = input("Enter the header1 : ")
# splitting the data with semicolon as delimiter
row = header1.split(";")
string = ""
# iterating over the row
for index in range(len(row)):
if index == 0:
# left justifing the first element
string += row[index].ljust(15)
else:
# right justifying the remaining elements
string += row[index].rjust(10)
# adding header to the data(list)
data.append(string)

# header2 same as header1
header2 = input("Enter the header2: ")
row = header2.split(";")
string = ""
for index in range(len(row)):
if index == 0:
string += row[index].ljust(15)
else:
string += row[index].rjust(10)
data.append(string)

# while loops until user want to exit
while(True):
# taking data from user and storing in row
row = input("Enter the data: ")

# checking if user wants to break or not
if row == "" or string == "-1" or string == "\n":
break

# if not splitting the data with semicolon as delimiter
row = row.split(";")
string = ""
# justifying the data same as header
for index in range(len(row)):
if index == 0:
string += row[index].ljust(15)
else:
string += row[index].rjust(10)
else:
data.append(string)


# opening the given file in write text mode
with open((file_name + ".txt"), "wt") as textfile:
# iterating over each row in data(list)
for row in data:
# writing each row to textfile
textfile.write(row)
# appending the new line character to each row
textfile.write("\n")

# opening the given file in csv write mode
with open((file_name + ".csv"), "w", newline = "") as csvfile:
# iterating over each row in data(list)
for index in range(len(data)):
lst = []
if index != 0:
# making each row(string) into list(lst) by splitting with space("") as delimiter
lst = data[index].split()
# concatinating the first and last name of student
lst[0] = lst[0] + " " + lst[1]
# deleting the last name from the data(lst)
del lst[1]
else:
lst = data[index].split()
  
# creating writer object on csvfile
writer = csv.writer(csvfile)
# writing each lst(list) to the csvfile
writer.writerow(lst)

Add a comment
Know the answer?
Add Answer to:
Create a program (Lab9_Act1_Write.py) that will read in data from the keyboard and store it in...
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
  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • Must be done in python and using linux mint Write a program to create a text...

    Must be done in python and using linux mint Write a program to create a text file which contains a sequence of test scores. Each score will be between 0 and 100 inclusive. There will be one value per line, with no additional text in the file. Stop adding information to the file when the user enters -1 The program will first ask for a file name and then the scores. Use a sentinel of -1 to indicate the user...

  • Programming Exercise 4.9 Write a script named numberlines.py. This script creates a program listing from a...

    Programming Exercise 4.9 Write a script named numberlines.py. This script creates a program listing from a source program. This script should: Prompt the user for the names of two files. The input filename could be the name of the script itself, but be careful to use a different output filename! The script copies the lines of text from the input file to the output file, numbering each line as it goes. The line numbers should be right-justified in 4 columns,...

  • Program: Data visualization

    5.10 LAB*: Program: Data visualization(1) Prompt the user for a title for data. Output the title. (1 pt)Ex:Enter a title for the data: Number of Novels Authored You entered: Number of Novels Authored(2) Prompt the user for the headers of two columns of a table. Output the column headers. (1 pt)Ex:Enter the column 1 header: Author name You entered: Author name Enter the column 2 header: Number of novels You entered: Number of novels(3) Prompt the user for data points. Data points must be in this format: string, int. Store the information before the comma into a string variable and the information after the comma into an integer. The user will enter -1 when they...

  • Java program Program: Grade Stats In this program you will create a utility to calculate and...

    Java program Program: Grade Stats In this program you will create a utility to calculate and display various statistics about the grades of a class. In particular, you will read a CSV file (comma separated value) that stores the grades for a class, and then print out various statistics, either for the whole class, individual assignments, or individual students Things you will learn Robustly parsing simple text files Defining your own objects and using them in a program Handling multiple...

  • 7.2 Write a Java program called to create an Excel spreadsheet Create a class called Food...

    7.2 Write a Java program called to create an Excel spreadsheet Create a class called Food to represent a Lunch food item. Fields include name, calories, carbs In a main method (of a different class), ask the user "How many things are you going to eat for lunch?". Loop through each food item and prompt the user to enter the name, calories, and grams of carbs for that food item. Create a Food object with this data, and store each...

  • How can I create Loops and Files on Java? • Create a program that reads a...

    How can I create Loops and Files on Java? • Create a program that reads a list of names from a source file and writes those names to a CSV file. The source file name and target CSV file name should be requested from the user • The source file can have a variable number of names so your program should be dynamic enough to read as many names as needed • When writing your CSV file, the first row...

  • using java Program: Please read the complete prompt before going into coding. Write a program that...

    using java Program: Please read the complete prompt before going into coding. Write a program that handles the gradebook for the instructor. You must use a 2D array when storing the gradebook. The student ID as well as all grades should be of type int. To make the test process easy, generate the grade with random numbers. For this purpose, create a method that asks the user to enter how many students there are in the classroom. Then, in each...

  • In C++, Write a program that retrieves a phone number from a txt files of names...

    In C++, Write a program that retrieves a phone number from a txt files of names and phone numbers. The program should ask the user to enter a name and then print the phone number of that person (or vice versa, number into name) The program should allow the user to repeat the operation as often as they would like to. The data is contained in two text files named "phoneNames.txt" and "phoneNums.txt". The files have one name or one...

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