In: Computer Science
Write a code in python to read in a file of spice like component and/or control data. break the data up into fields in a class object.
Python code:
import csv
#Create country class with two columns
class Country:
"""docstring forCountry."""
def __init__(self, data):
super(Country,
self).__init__()
#assigning data to
member varibles
self.name =
data[0]
self.code = data[1]
#To call main method
def main(path):
#To read file
with open(path, 'r') as f:
#to read csv
reader =
csv.reader(f)
#To create List of
objects
return [Country(i) for i
in list(reader)[1:]]
#replace with your data path
path = '~/Downloads/country.csv'
print(main(path))
Sample data:
name,code
Afghanistan,AF
Åland Islands,AX
Albania,AL
Algeria,DZ
American Samoa,AS
Andorra,AD
Angola,AO
Anguilla,AI
Antarctica,AQ
Antigua and Barbuda,AG
Argentina,AR
Armenia,AM
Aruba,AW
Sample result: