PYTHON 1.---------> Create 1 AbstractCSVReader class a. The class should have an
ID: 3586005 • Letter: P
Question
PYTHON
1.---------> Create 1 AbstractCSVReader class
a. The class should have an initializer method taking the path to the file to be read
b.The class should have the method: row_to_record(row)
-Where “row” is a row from the CSV as a dictionary
-This method should be implemented by simply raising NotImplementedError.
c. The class should have the method: load() that returns a list of records. Load should:
-Use “with” to open the CSV files
-read each row from the file into a dictionary: keys are the column names and values are the matching values (see https://docs.python.org/3/library/csv.html#csv.DictReader )
-call the row_to_record method and send the row as a parameter
-handle the BadData exception raised by row_to_record by skipping the record – For more on BadData Exception see step 5
-If no exception is raised: then the record should be added to the list of records.
-Once all records are loaded into the list, returns the list.
2. ---------> Create a CSV reader class for each of the files you want to load
i.e. BaseballCSVReader and StocksCSVReader
a. The class should inherit the AbstractCSVReader
b. Each class should implement its own row_to_record method. The input is a dictionary of unvalidated data, it should validate the data, parse it, create new record and return the record created.
c. The validation depends on your concrete record:
-Validation fails for any row that is missing any piece of information
-Validation fails if the name (symbol or player name) is empty
-Validation fails if any of the numbers (int or float) cannot be parsed (watch out of the division by zero!!)
d. If validation fails: this method should raise a BadData exception (question 3)
e. StocksCSVReader should have two calculations using the extracted records:
-market_value_usd = Price * ExchangeRate * SharesOutstanding
-pe_ratio = Price / NetIncome
3. -------> Create a BadData custom exception to handle record creation errors
From your main section
Explanation / Answer
ANSWER A-_ The csv module define the following function-
csv.reader
Return a reader object which will iterate over lines in the csv file.Each row read from the csv file is return as a list of strings. No automatic data type conversion is performed unless the QUOTE_NONNUMERIC format option specified (in which case field are transfered into float).
As per Question here we explaine-
>>>import csv
>>>spam reader=csv.reader(open('eggs,csv,'newline."),delimiter=' ',quotechar='1')
>>>for row is spam reader:
print(','join(row))
spam,spam.beautiful spam,lovely spam
string each time its-next_() method is called file objects and list object. It should be opened with newline=".
Answer B-
>>>import csv row _to_record(row)
>>>with open('names.csv') as csv files:
reader=csv.DictReader(row_to_record(row)
for row in reader:
print(row['first_name'],row['last_name'])
Eric Idle
john cleese
>>>print (row)
orderedDict(['first_name','john'),('last_name','cleese')]
Returned rows are now type orderedDict.
Answer C-
with open('load().csv','w',newline=' ')as f:
writer=csv.writer(f)
writer.writerrows(loaditerable)
import csv, sys
file name='load().csv'
withopen(filename,newline=' ')as f
reader=csv.reader(f)
try:
for column in reader:
print(column)
excep csv.Error as e:
Sys.exit(file{ },line{ }:{ }'.format(filename,reader.line_num,e))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.