0

Context: Building a simple stock maintainer, using CSV file to store and read data.

Data in stock file:

product 1,"[200, 35.5, 37.0]"    
products = {}

with open("stock.csv", "r") as csv_file:
    csv_reader = csv.reader(csv_file)

    for line in csv_reader:
        products[line[0]] = line[1]
        print(line)
        print(products)

Gives error:

products[line[0]] = line[1]  
IndexError: list index out of range  
['product 1', '[200, 35.5, 37.0]']  
{'product 1': '[200, 35.5, 37.0]'}
Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
  • The two print commands at the end are just to check and find the source of error. – Rijul dhingra Jun 11 '19 at 13:14
  • Depending on the platform you might need to open the file in binary mode - see the [documentation](https://docs.python.org/2/library/csv.html#csv.reader). – meowgoesthedog Jun 11 '19 at 13:26
  • There is nothing wrong in what you have. May be your data was not right when you tried it. Try to create a the data file again. – skimad Jun 11 '19 at 13:26
  • Can you check if there is an empty line in the stock.csv file or add a print statement print(line) before the products[line[0]] = line[1] – Manu mathew Jun 11 '19 at 13:33
  • @meowgoesthedog that doesn't work. – Rijul dhingra Jun 12 '19 at 19:54
  • @Manumathew I did what you suggested, while trying to find the source of error myself. Upon printing the line before using its indices as key:value, the data store does get printed as a list. output >>>['product 1', '[200, 35.5, 37.0]'] followed by IndexError – Rijul dhingra Jun 12 '19 at 19:57

3 Answers3

1

In Python, arrays are zero-indexed (ie: line[0] is the first element, line[1] is the second, etc..)
In here: products[line[0]] = line[1], you're trying to access the second element of line, but from your example, the csv file only contains a single line, hence the error.

yaken
  • 559
  • 4
  • 17
  • 1
    In the question, user is trying to access first & second elements of first line and not trying to access a second line. – Manu mathew Jun 11 '19 at 13:30
  • line[0] is the name of the product while line[1] is the details stored as a list in the CSV file. Both are accessing just the first data row from the CSV file. Please refrase/Check. Thanks :) – Rijul dhingra Jun 12 '19 at 19:23
0

The data in your stock file only has one line:

product 1,"[200, 35.5, 37.0]"

So when you try to get line[1] in line 7, you are trying to get the data in line 2 of your data, which doesn't exist.

stevestar888
  • 113
  • 7
  • 1
    In the question, user is trying to access first & second elements of first line and not trying to access a second line. – Manu mathew Jun 11 '19 at 13:31
  • What I am trying to do using the 'for line' loop is get the each row from the CSV file as a list. Later, I am using that list to access the actual data (with the name as 0th Index and details list as 1st index). And I am unable to find the error in doing so myself. – Rijul dhingra Jun 12 '19 at 20:03
0

From this SO answer

what you are missing is

skipinitialspace=True

in your csv_reader.

Here is the code that worked :

p = {}
with open("stock.csv", "r") as csv_file:
    csv_reader = csv.reader(csv_file, skipinitialspace=True)
    for line in csv_reader:
            p[line[0]] = line[1]
    print(p)

and the result:

{'product 1': '[200, 35.5, 37.0]'}
  • For some reason, whatever combination of list indices I use >>> p[line[0]] = line[1] or >>> p[line[0]] = p[line[0], the error still exists (list index out of range) – Rijul dhingra Jun 12 '19 at 19:45