Index error: List index out of range

A forum for general discussion of the Python programming language.

Index error: List index out of range

Postby chuaminghui » Sun Jan 05, 2014 9:19 am

Hi,

Following are my code:
Code: Select all
import OpenOPCEdited
import pyodbc
import time

opc = OpenOPCEdited.client()
opc.connect('Matrikon.OPC.Simulation.1')

cnxn = pyodbc.connect('DSN=Excel Files;Dbq=C:\Users\Administrator\Desktop\Minghui\Testing.xlsm', autocommit=True)
fetcher = cnxn.cursor()

Query_WindSpeed = fetcher.execute("select WindSpeed from [Sheet2$]").fetchall()

Query_value1 = fetcher.execute("select value1 from [Sheet2$]").fetchall()

tagname1 = []   #Tag name list with Bucket Brigade. prefix
for tags in Query_WindSpeed:
    raw = tags[-1]
    compulsory = 'Bucket Brigade.'
    raw_new = compulsory + str(raw)
    tagname1.append(raw_new)

#1st column of data
for tags in Query_value1:
    value1 = []
    raw = tags[-1]
    value1.append(raw)
   

interval = 5               # update interval in seconds

#Writing data to OPC
for iteration in range(2):
    n = 0
    m = n + 1
    for every in tagname1:
        find_cb = every.find('CB')
        if find_cb == -1:
            opc[every] = value1[n]
        if find_cb != -1:
            opc[every] = str(value1[n])
        n += 1

    time.sleep(interval)   #wait (X) seconds



I'm getting the error at the code " opc[every] = value1[n] "
Anyone able to help me solve it?
These code are to read data from excel into matrikonOPC

Many thanks in advance!!
chuaminghui
 
Posts: 3
Joined: Sat Jan 04, 2014 1:02 pm

Re: Index error: List index out of range

Postby hansn » Sun Jan 05, 2014 9:48 am

The index error is raised when a sequence subscript is out of range.
The following snippet should be self explanatory.
Code: Select all
>>> list = ['a', 'b', 'c']
>>> list[2]
'c'
>>> list[3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>>

hansn
 
Posts: 87
Joined: Thu Feb 21, 2013 8:46 pm

Re: Index error: List index out of range

Postby micseydel » Mon Jan 06, 2014 7:15 pm

You always need to post your full traceback verbatim and not just a snippet from it. As hansn included in his post, this is what a traceback looks like
Code: Select all
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

Note that tracebacks have indentation, so you should use code tags when you post them just as you would Python code.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1391
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 3 guests

cron