I have a .csv file with 105 rows and 9 columns. Some of the rows only have values in four columns. How can I create a list from the .csv file without getting an IndexError: list index out of range?
from elements import Element
import csv
filename = "elements.csv"
fields = []
elements_list = []
with open(filename, 'r') as csvfile:
csvreader = csv.reader(csvfile, delimiter=';')
fields = next(csvreader)
for row in csvreader:
elements_list.append(Element(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8]))
When I run this it gives IndexError: list index out of range
Example of the .csv file:
Element,Number,Symbol,Weight,Boil,Melt,Density,Vapour,Fusion,
Aluminum,13,Al,26.98,2723.16,933.16,2700,284.34,10.68,
Silicon,14,Si,28.09,2953.16,1683.16,2330,170.02,46.48,
Phosphorus,15,P,30.98,
Sulfur,16,S,32.06,717.76,392.16,2070,12.60,1.42,
Chlorine,17,Cl,35.45,238.46,172.16,1560,10.22,3.22,
Argon,18,Ar,39.95,87.36,83.76,1400,6.53,1.18,
For example, how can I create a list that fills the last missing 5 values of "Phosphorus" to "0" As in:
Silicon,14,Si,28.09,2953.16,1683.16,2330,170.02,46.48,
Phosphorus,15,P,30.98,0,0,0,0,0,
Sulfur,16,S,32.06,717.76,392.16,2070,12.60,1.42,