XiaoJian39
2022-03-18 17:09
采纳率: 100%
浏览 576

python运行问题IndexError: tuple index out of range

IndexError: tuple index out of range
import pymysql
import operator
'''创建数据库'''
import pymysql
import numpy as np
#  打开数据库连接,不需要指定数据库,因为需要创建数据库
#  conn = pymysql.connect('localhost',user = "root",passwd = "639801",db='maintenance')
conn = pymysql.connect(host="127.0.0.1", user="root", password="639801", database="user1", port=3306, autocommit=True)
#  获取游标
cursor=conn.cursor()
#  cursor.execute()
#  查询数据
cursor.execute("select * from maintenance1")
data = cursor.fetchall()
row = cursor.rowcount  # 取得记录个数,用于设置表格的行数
vol = len(data[0])  # 取得字段数,用于设置表格的列数
print(row)
start_num=[0.0]*row
end_num=[0.0]*row
data1=[]
for i in range(row):
    if data[i][1] != None and "+" in data[i][1]:
        start_str=str(data[i][1])
        if "k" in start_str:
            start_int1 = start_str[start_str.index("k") + 1:start_str.index("+")]
        if "K" in start_str:
            start_int1 = start_str[start_str.index("K") + 1:start_str.index("+")]
        start_int2 = start_str[start_str.index("+") + 1:]
        start_num1 = float(start_int1)*1000+float(start_int2)
    else:
        start_num1 = data[i][1]

    if data[i][2]!= None and "+" in data[i][2]:
        end_str = str(data[i][2])
        if "k" in end_str:
            end_int1 = end_str[end_str.index("k") + 1:end_str.index("+")]
        if "K" in end_str:
            end_int1 = end_str[end_str.index("K") + 1:end_str.index("+")]
        end_int2 = end_str[end_str.index("+") + 1:]
        end_num1 = float(end_int1)*1000+float(end_int2)
    else:
        end_num1 = data[i][2]
    start_num[i] = start_num1
    end_num[i] = end_num1
    data1.append((start_num[i], end_num[i], data[i][75]))

print(data1)
sql="UPDATE maintenance1 SET start_number=(%s),end_number =(%s) WHERE id=(%s)"

cursor.executemany(sql, data1)
cursor.close()
conn.commit()
conn.close()
print('sql执行成功')

执行结果

928
Traceback (most recent call last):
File "E:/python1xuexi/pythonProject5/zhuanghao2.py", line 45, in
data1.append((start_num[i], end_num[i], data[i][75]))
IndexError: tuple index out of range

Process finished with exit code 1

换一个数据库连接就行了,很奇怪,找不到解决方案,求帮助啊
我想要达到的结果

3条回答 默认 最新

相关推荐 更多相似问题