【发布时间】:2017-01-15 13:02:16
【问题描述】:
我正在尝试创建一个程序,该程序从网站上抓取股票价格,然后计算一系列时间价格之间的趋势差异。我通过将数据存储在标记为买入和卖出价格的表格中来做到这一点。 如果我单独运行它们,它们工作正常。但是第二次我把它们放在同一个程序中它们就不起作用了。
我需要能够从列表中的每三个值计算一个趋势,因为这是每只股票的记录数,但它们都放在一个列表中
这是我的代码
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'
【问题讨论】:
-
您的表中似乎有
NULLs,这意味着row[0]然后设置为None。也许您需要更新查询以仅返回非 NULL 值? -
@MartijnPieters 啊,这有道理,我稍后会添加趋势,尽管我使用替换它会将它们添加到创建空字段的新行中,我认为它会忽略这些
标签: python python-3.x sqlite