2

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,
Sein
  • 21
  • 1
  • Have you looked into using pandas? I would strongly recommend checking out https://datatofish.com/import-csv-file-python-using-pandas/#:~:text=Steps%20to%20Import%20a%20CSV%20File%20into%20Python,path.%20...%203%20Step%203%3A%20Run%20the%20Code if you haven't done so. pandas can read and handle csv's to the degree that you've stated here. – Omnishroom Sep 29 '22 at 19:07

4 Answers4

1

Here's a simple way to pad a row to a desired width.

width = 9
row = (row + [0] * width)[:width]

As an aside

Element(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8])

Is better written as

Element(*row)

or if row is longer than you need, do

Element(*row[:9])
Steven Rumbalski
  • 44,786
  • 9
  • 89
  • 119
0

EDIT: I thought this was about dicts; there's no get for lists :(


You can utilize the built in get:

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.get(0, 0), row.get(1, 0), row.get(2, 0), row.get(3, 0), row.get(4, 0), row.get(5, 0), row.get(6, 0), row.get(7, 0), row.get(8, 0)))

Though for reading csv files I would recommend you to look into pandas, as it is more powerful and was built for this.

Lior Pollak
  • 3,362
  • 5
  • 27
  • 48
0

If you are willing to use pandas, one option is to read by line and then split each row thereafter.

import pandas as pd
elements = pd.read_csv("elements.csv", sep = "\n")
elements = elements[0].str.split(",", expand = True)
-1

You can use pandas library to read csv Pandas is very much easy than this Filling data is very easy in pandas