我正在尝试读取CSV文件并将其插入PostgreSQL数据库。但是我希望第1列和第5列项目为整数。因此,我将第一和第五列转换为整数。但是显示错误 IndexError:列表索引超出范围> 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])
答案 0 :(得分:0)
仅在其中包含某些内容时,才尝试处理该行:
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])
答案 1 :(得分:0)
由于您收到 IndexError ,似乎您的文件包含空行 试试这个。
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]))
答案 2 :(得分:0)
您看到的错误是由于csv中的数据不正确。
在处理csv文件时,我始终使用pandas
。这样,您不必考虑自己面临的问题,它将自动为您解决。
与executemany
结合使用,将使您的代码运行更快。
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())