1

I got error IndexError: list index out of range when I running my program. This is my source code

import csv
from sqlalchemy import *

db = create_engine('mysql://root:123@localhost/client')

meta = MetaData(db)
table = Table('DataSensor', meta,
   Column('id',Integer, primary_key=True),
   Column('Tanggal', DateTime),
   Column('Tipe_sensor', String(50)),
   Column('Value', Integer),
   Column('Ket', String(50)))

with open('sensortest.txt', 'rb') as csvfile:
     tbl_reader = csv.reader(csvfile, delimiter=',')
     for row in tbl_reader:
         table.insert().values(id=row[0], Tanggal=row[1], Tipe_sensor=row[2], Value=row[3], Ket=row[4])
conn = db.connect()

I want import my database file (txt) . Help.

1 Answer 1

1
for row in tbl_reader:
    table.insert().values(id=row[0], Tanggal=row[1], Tipe_sensor=row[2], Value=row[3], Ket=row[4])

Here you are indexing row which might exceed the maximum element in the row.

Try to know the length of the row for example.

for row in tbl_reader:
    preint(len(row))
    #or print(row)

Then check maximum elements in each row and design your code accordingly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.