read the CSV file into my main code

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter7: File Handling And Applications
Section: Chapter Questions
Problem 10RQ
icon
Related questions
Question
Please help - I cannot get my fileIO.py to read the CSV file into my main code. Please let me know if I’m doing soemthing incorrect, I have all files in the same file as well.
File Edit Format Run Options Window Help
from datetime import datetime
from FileIO import read csv, write csv
# to print and process dates
# to read and write files
# tuple of valid positi
VALID POSITIONS =
('C', '1B', '2B', 'SS', 'LF', 'CF', 'RF', 'P')
main
':
# main driver function
if
name
data = read csv()
now = datetime.now()
# fetch the csv file data using read csv function
# store the current date, month and year
# printing to the display
print ("=" *60)
string = "Basketball Team Manager"|
print (string.rjust (len (string) + 20, " "))
# print the current date
print ("CURRENT DATE: ", end="")
print (now.strftime("%Y-%m-%d"))
# get the game data
print ("GAME DATE: "
input_ = input ()
if input :
date = list (map(int, input .split("-")))
date of match = datetime(date[0], date[1]], date[2]).
print ("DAYS UNTIL GAME: ", end="")
print ((date of match - now).days)
end="")
# if the game data value was inputted
# convert input to list of ints. eg: 2020
# print the difference in data
# print the menu
print ("\NMENU OPTIONS\n" +
Display lineup" + "\n"
"2
"1
Add player" + "\n" +
"3
Remove player" + "\n" +
Move player" + "\n" +
Edit player position" + "\n" +
"6
"4
"5
Edit player stats" + "\n" +
"7 - Exit program\n")
print (", ".join (VALID_POSITIONS))
print ("=" * 60)
while True:
# infinite loop until user enters the value 7
option = input ("\nMenu option: ")
if option == "1":
# print the elements with padding
print (" " * 3 + f"{'Player'.ljust(31)}{'POS'.ljust(6)}{'AB'.ljust (6)}{'H'.ljust(6;
print ("=" *60)
for idx, data in enumerate (data, 1):
# to print the player lineup
# get the data
number = str(idx)
name = data["name"]
position = data["position"]
at bats = data["at bats"]
hits = data["hits"]
avg = int (hits) / int (at_bats)
# calculate the average
# using rjust to add the necessary space paddings
number
number.ljust (3)
Transcribed Image Text:File Edit Format Run Options Window Help from datetime import datetime from FileIO import read csv, write csv # to print and process dates # to read and write files # tuple of valid positi VALID POSITIONS = ('C', '1B', '2B', 'SS', 'LF', 'CF', 'RF', 'P') main ': # main driver function if name data = read csv() now = datetime.now() # fetch the csv file data using read csv function # store the current date, month and year # printing to the display print ("=" *60) string = "Basketball Team Manager"| print (string.rjust (len (string) + 20, " ")) # print the current date print ("CURRENT DATE: ", end="") print (now.strftime("%Y-%m-%d")) # get the game data print ("GAME DATE: " input_ = input () if input : date = list (map(int, input .split("-"))) date of match = datetime(date[0], date[1]], date[2]). print ("DAYS UNTIL GAME: ", end="") print ((date of match - now).days) end="") # if the game data value was inputted # convert input to list of ints. eg: 2020 # print the difference in data # print the menu print ("\NMENU OPTIONS\n" + Display lineup" + "\n" "2 "1 Add player" + "\n" + "3 Remove player" + "\n" + Move player" + "\n" + Edit player position" + "\n" + "6 "4 "5 Edit player stats" + "\n" + "7 - Exit program\n") print (", ".join (VALID_POSITIONS)) print ("=" * 60) while True: # infinite loop until user enters the value 7 option = input ("\nMenu option: ") if option == "1": # print the elements with padding print (" " * 3 + f"{'Player'.ljust(31)}{'POS'.ljust(6)}{'AB'.ljust (6)}{'H'.ljust(6; print ("=" *60) for idx, data in enumerate (data, 1): # to print the player lineup # get the data number = str(idx) name = data["name"] position = data["position"] at bats = data["at bats"] hits = data["hits"] avg = int (hits) / int (at_bats) # calculate the average # using rjust to add the necessary space paddings number number.ljust (3)
FilelO.py- C:/Users/lyart/OneDrive/Desktop/project2_AppDevelopment/FilelO.py (3.9.1)
File Edit Format Run Options Window Help
import csv
def read csv():
Function to read data from the players.csv file
data = []
# to store the data from the csv file
with open ('players.csv') as csv 'file:
# using context manager to open and close t
CVs reader = csv.reader(csy file)
for row in cvs reader:
{"name": row[0],
"position": row[1],
"at bats": row[2],
"hits": row[3] }
dictionary
# storing the data into a dictionary
data.append (dictionary)
# creating a list of dictionary objects
return data
# return data to the main program
def write csv(data):
Function to write data back to the players.csv file
with open('players.csv', 'w') as csv file:
writer = csv.writer(csv fi'e)
# using context manager to open and cl
for row in data:
writer.writerow(row.values(() )
# writing data to the file
Transcribed Image Text:FilelO.py- C:/Users/lyart/OneDrive/Desktop/project2_AppDevelopment/FilelO.py (3.9.1) File Edit Format Run Options Window Help import csv def read csv(): Function to read data from the players.csv file data = [] # to store the data from the csv file with open ('players.csv') as csv 'file: # using context manager to open and close t CVs reader = csv.reader(csy file) for row in cvs reader: {"name": row[0], "position": row[1], "at bats": row[2], "hits": row[3] } dictionary # storing the data into a dictionary data.append (dictionary) # creating a list of dictionary objects return data # return data to the main program def write csv(data): Function to write data back to the players.csv file with open('players.csv', 'w') as csv file: writer = csv.writer(csv fi'e) # using context manager to open and cl for row in data: writer.writerow(row.values(() ) # writing data to the file
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Database connectivity
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
Computer Science
ISBN:
9780357392676
Author:
FREUND, Steven
Publisher:
CENGAGE L