【问题标题】:Python Syntax Error for Iterative Addition e.g. x += 1 [closed]迭代加法的 Python 语法错误,例如x += 1 [关闭]
【发布时间】:2019-01-29 17:28:24
【问题描述】:

为 Finviz 构建股票数据抓取工具。我正在使用通过 Anaconda 为 Python 3.7 安装的 Spyder3。

我的代码如下。当我在终端中逐行执行 x = 0,然后 x = x+1 代码时,它工作得很好。当我运行整个脚本时,如果我使用 x += 1 或 x = x + 1,我会得到同样的错误。

def finviz_query(tickerlist):

'''Get's source code from FinViz and Creates a List of Lists for Export '''
url="https://finviz.com/screener.ashx?v=140&t=" + str(stocks)
response = requests.get(url)
source=response.text
soup = bs4.BeautifulSoup(source)
priceLST = [i.get_text() for i in soup.find_all('a')]
del priceLST[0:37]
del priceLST[len(priceLST)-2:len(priceLST)]
stockLST = re.split(',',stocks)
stock_outputLST = []
while len(priceLST) > 0:
    if priceLST[0] in stockLST:
        stock_outputLST.append([priceLST[0:16]])
        del priceLST[0:16]
    if len(priceLST) < 1:
        break
x = 0
while x < len(stock_outputLST):
    if x < len(stock_outputLST):
        stock_outputLST[x][0].append(time.strftime("%Y-%m-%d;%H:%M")
        x = x + 1
    else:
        break
stock_outputLST[len(stock_outputLST)-1][0].append('0')
stock_outputLST[len(stock_outputLST)-1][0].append(time.strftime("%Y-%m-%d;%H:%M"))

错误输出在这里:

...:stock_outputLST[len(stock_outputLST)-1][0].append(time.strftime("%Y-%m-%d;%H:%M"))
...: return print('finviz_query complete')
  File "<ipython-input-114-81b306b1a6de>", line 22
    x = x + 1
    ^
SyntaxError: invalid syntax

提前致谢!

【问题讨论】:

  • 像往常一样,您在上一行缺少一个近括号。
  • 非常感谢。格雷格也做出了同样的回应。你们可以这么快就看到这些东西,真是太神奇了。

标签: python python-3.x anaconda spyder


【解决方案1】:

每当您遇到这样的错误时,请查看上面的行:

    stock_outputLST[x][0].append(time.strftime("%Y-%m-%d;%H:%M")

您在末尾缺少右括号 )

【讨论】:

  • 噢!非常感谢。我觉得很愚蠢。
  • @bio_inf_dreamer:每个人都会犯这个错误,因为它很容易做到,只是当你做了几十次后获得更多经验时,你就会知道要寻找什么。 :)
猜你喜欢
  • 2014-07-19
  • 2018-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-23
  • 2012-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多