如何从SQLite选项卡返回实际值

2024-04-19 06:38:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个程序,从一个网站上刮下股票价格,然后计算一系列时间内价格之间的趋势差异。我是通过将数据存储在标记为买入和卖出价格的表中来实现的。 如果我把它们分开运行,它们就可以正常工作。但第二次我把它们放在同一个程序里,它们就不起作用了。你知道吗

我需要能够从列表中的每三个值计算出一个趋势,因为这是每只股票的记录数,但它们都放在一个列表中

这是我的密码

def plot(price,trend_price):    
    for a in stock:
        count1 = 0
        count2 = 1
        index = 0
        value = ["0"]
        cursor.execute("""SELECT """+price+""" FROM """+a+"""""")
        trend = []
        rows = cursor.fetchall()
        for row in rows:
            print(row[0])
            trend.append(float(row[0]))
            index = index + 1
            if index == len(web):
                percentage = []
                for i in range(len(web)-1):
                    change = trend[count2]-trend[count1]
                    print(trend[count2],trend[count1])
                    percent = (change/trend[count1])*100
                    print(percent)
                    percentage.append(percent)
                    count1 = count1 + 1
                    count2 = count2 + 1
                for i in percentage:
                    print(i)
                    if i <= 0:
                        if i == 0:
                            value.append(0)
                        elif i <= -15:
                            value.append(-4)
                        elif i <= -10:
                            value.append(-3)
                        elif i <= -5:
                            value.append(-2)
                        elif i < 0:
                            value.append(-1)
                    else:
                        if i >= 15:
                            value.append(4)
                        elif i >= 10:
                            value.append(3)
                        elif i >= 5:
                            value.append(2)
                        elif i >= 0:
                            value.append(1)

                for i in value:
                    t = str(i)
                    cursor.execute("""
                        REPLACE INTO """+ a +"""
                        ("""+trend_price+""")
                        VALUES
                        ("""+ t +""")
                        """)

我得到的错误如下

Traceback (most recent call last):
  File "C:/Users/Luke_2/Desktop/Computing/Coursework/live/Jan15.py", line 172, in <module>
    run()
  File "C:/Users/Luke_2/Desktop/Computing/Coursework/live/Jan15.py", line 167, in run
    plot("BuyPrice","TrendBuy")
  File "C:/Users/Luke_2/Desktop/Computing/Coursework/live/Jan15.py", line 88, in plot
    trend.append(float(row[0]))
TypeError: float() argument must be a string or a number, not 'NoneType'

Tags: inforindexifplotvaluepricecursor
1条回答
网友
1楼 · 发布于 2024-04-19 06:38:41

表中有NULL个值,这些值在Python中被转换为None个值。不能将None转换为浮点。也许您只想选择不为空的行?你知道吗

cursor.execute(
    "SELECT ? FROM {} WHERE columnname is not NULL".format(a), 
    (price,))

我不知道第一列的列名是什么,您必须调整查询。你知道吗

我已将您的查询转换为对价格使用SQL参数(不能使用参数插入表名,请确保仅使用硬编码表名,而不接受不受信任的数据)。你知道吗

另一种方法是在Python中跳过这些行:

if rows[0] is None:
    continue

还要注意,sqlite3模块可以为您转换值,请参见SQLite and Python Types section;如果该列在数据库中声明为REAL,那么您已经得到了float对象。你知道吗

相关问题 更多 >