0
$\begingroup$

I am working through this very helpful tutorial "Data Visualization in Blender and Python", but have hit a snag.

I'm using this dummy CSV file:

width,5
width,10
width,2
width,6
width,10
width,9
width,1
width,10
width,7
width,8

and this version of the YouTuber's code

import csv
import bpy

with open ('/Users/me/Desktop/widthCSV5 - Sheet1.csv', newline='') as f:
    readout = list(csv.reader(f))

for a in readout:
    placement = readout.index(a)
    print(placement)

I get this output:

0
1
2
3
1
5
6
1
8
9

The entries that had 10 as a value all get 1 as the index. Has anyone run into this issue before? Thank you in advance!

$\endgroup$
1
  • $\begingroup$ "The index() method only returns the first occurrence of the matching element." $\endgroup$
    – thibsert
    Dec 9, 2022 at 19:17

1 Answer 1

1
$\begingroup$

The output is correct. The numbers are index values of the found values in the list, not the values themselves.

You can execute the code in the Python console. See also Easy way to run python script in blender python console?

Here is the data that is read from the file:

>>> readout
[['width', '5'], ['width', '10'], ['width', '2'], ['width', '6'], ['width', '10'], ['width', '9'], ['width', '1'], ['width', '10'], ['width', '7'], ['width', '8']]

In the loop a is an element of the list:

>>> for a in readout:
...     print(a)
...     
['width', '5']       # index 0
['width', '10']      # index 1
['width', '2']       # index 2
['width', '6']       # index 3
['width', '10']      # index 4
['width', '9']       #  ...
['width', '1']
['width', '10']
['width', '7']
['width', '8']

If you enter readout.index( and hit the Tab key you will get a short description of the index() function. It tries to find the given value in the list and returns its index. The index starts at 0 for the first element. If the element is not found you will get an exception (error).

>>> readout.index(
index(value, start=0, stop=9223372036854775807)
Return first index of value.
Raises ValueError if the value is not present.

If you call the index() function with the element ['width', '10'] (which is a list with the two column values from the file) then you will get its index: 1

>>> readout.index(['width', '10'])
1

If you increase the start index to '2', you will get the next search result: 4. This is correct.

>>> readout.index(['width', '10'], 2)
4

If you want to see just the numbers, you need to use a[1] to access the nested list element:

>>> for a in readout:
...     print(a[1])
...     
5
10
2
6
10
9
1
10
7
8
$\endgroup$
3
  • $\begingroup$ Hi, thank you for your help. I guess I was misunderstanding the youtuber's intention with the index as it was being used more like an incrementor to position each new object. It now seems like index would be hazardous to use like that. Thanks for clearing that up, I will probably just use a separate incrementing variable. $\endgroup$ Dec 9, 2022 at 19:51
  • $\begingroup$ Now you mentioned this I had to watch the part of the video. Indeed he does this around timestamp 13:45. But that's not a good way to get the element's index and it only works because the unique names of the months are used. In your case, the elements are not unique in the list and it doesn't work. Better use enumeration as shown here Accessing the index in 'for' loops at stackoverflow.com $\endgroup$
    – Blunder
    Dec 9, 2022 at 20:37
  • $\begingroup$ that looks like the one I need, thanks again! $\endgroup$ Dec 9, 2022 at 21:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .