1

I am trying to read a CSV file and insert into a PostgreSQL database. But I want 1st and 5th column item as integer. So, I am converting 1st and 5th columns into an integer. But it is showing error < date_2 = int(row[0])

IndexError: list index out of range>

    with open('parts_time.txt') as csvfile:
        readCSV = csv.reader(csvfile, delimiter=',')
        for row in readCSV:

            date_2 = int(row[0])
            date_4 = int(row[4])

            cursor.execute("INSERT INTO parts_time 
            (time_id,time_day,time_month,time_year,vendor_idk)"\
            "VALUES (%s,%s,%s,%s,%s)",
           [date_2,row[1],row[2],row[3],date_4])
2
  • 2
    You probably have an empty line at the end of the file Commented Dec 17, 2018 at 11:23
  • is the header in csv same to the headers in table ? Commented Dec 17, 2018 at 12:04

3 Answers 3

1

Try processing the row only if there is something inside:

with open('parts_time.txt') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        if row:
            date_2 = int(row[0])
            date_4 = int(row[4])

            cursor.execute("INSERT INTO parts_time 
            (time_id,time_day,time_month,time_year,vendor_idk)"\
            "VALUES (%s,%s,%s,%s,%s)",
            [date_2,row[1],row[2],row[3],date_4])
Sign up to request clarification or add additional context in comments.

Comments

0

Seems your file contains empty lines due to that your getting IndexError Try this.

with open('t.txt') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        if len(row) != 5:
            continue
        date_2 = int(row[0])
        date_4 = int(row[4])
        cursor.execute("INSERT INTO parts_time (time_id,time_day,time_month,time_year,vendor_idk) VALUES (%s,%s,%s,%s,%s)" % tuple([date_2,row[1],row[2],row[3],date_4]))

Comments

0

The error you see is because of your improper data in csv. While dealing with csv files i always use pandas. By this you don't have to think about the issues like you are facing, it will automatically resolve it for you. Clubbing that with the executemany, will make your code run faster.

import pandas
df = pd.read_csv('parts_time.txt')
df.columns = ['time_id', 'time_day', 'time_month', 'time_year', 'vendor_idk'] # You can skip this line if the column names in csv file matches that in Database
df['time_id'] = df['time_id'].astype(int)
df['vendor_idk'] = df['vendor_idk'].astype(int)
cursor.prepare("insert into parts_time(time_id,time_day,time_month,time_year,vendor_idk) values(:1, :2, :3 ,:4, :5)")
cursor.executemany(None, df.values.tolist())

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.