【问题标题】:Python: Nested while loop inside a for loop breaking unexpectedly without any errorPython:嵌套while循环在for循环中意外中断而没有任何错误
【发布时间】:2019-09-26 18:46:35
【问题描述】:

我有一个嵌套在 for 循环中的 while 循环,该循环在从 firestore 收集的 json 数组上运行,该数组收集股票代码以传递给另一个 api,以收集每分钟的交易数据以放回 firestore 数据库。

当我运行循环时,它会在通过 389 entry while 循环的第四次或第六次(永远不会更多)时意外停止而没有任何错误。

知道这是为什么吗?我的代码有什么问题吗?我注意到如果我将 while 循环中的限制从 389 更改为 100,它对 json 数组中的所有公司都有效。但如果它是完整的 389 个条目,它不会通过列表中的四家公司。

无论如何,谢谢你的帮助!

import requests
import json
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
import datetime

cred = credentials.Certificate("./serviceAccountKey.json")
firebase_admin.initialize_app(cred)

db = firestore.client()

doc_ref1 = db.collection(u'Quiver').stream()

for doc in doc_ref1:

    symbol = doc.id

    api_url = "https://api.iextrading.com/1.0/stock/{}/chart/1d".format(symbol)

    query_url = api_url

    r = requests.get(query_url)

    if r.status_code != 200: 
        print("Error:", r.status_code)
        continue

    if r.status_code == 404: 
        print("Error:", r.status_code, symbol)
        continue

    json_stock = r.json()

    b = 0

    while b <= 100:
        try:
            date = json_stock[b]['date']

            minute = json_stock[b]['minute']

            label = json_stock[b]['label']

            high = json_stock[b]['high']

            low = json_stock[b]['low']

            average = json_stock[b]['average']

            volume = json_stock[b]['volume']

            notional = json_stock[b]['notional']

            numberOfTrades = json_stock[b]['numberOfTrades']

            marketHigh = json_stock[b]['marketHigh']

            marketLow = json_stock[b]['marketLow']

            marketAverage = json_stock[b]['marketAverage']

            marketVolume = json_stock[b]['marketVolume']

            marketNotional = json_stock[b]['marketNotional']

            marketNumberOfTrades = json_stock[b]['marketNumberOfTrades']

            open = json_stock[b]['open']

            close = json_stock[b]['close']

            marketOpen = json_stock[b]['marketOpen']

            marketClose = json_stock[b]['marketClose']

            changeOverTime = json_stock[b]['changeOverTime']

            marketChangeOverTime = json_stock[b]['marketChangeOverTime']

            doc_ref = db.collection(u'dailies').document(u'{}-{}'.format(minute, symbol))

            doc_ref.set({
                u'date':u'{}'.format(date),
                u'minute':u'{}'.format(minute),
                u'label':u'{}'.format(label),
                u'high':u'{}'.format(high),
                u'average':u'{}'.format(average),
                u'notional':u'{}'.format(notional),
                u'number of trades':u'{}'.format(numberOfTrades),
                u'market high':u'{}'.format(marketHigh),
                u'market low':u'{}'.format(marketLow),
                u'market average':u'{}'.format(marketAverage),
                u'market volume':u'{}'.format(marketVolume),
                u'market notional':u'{}'.format(marketNotional),
                u'market number of trades':u'{}'.format(marketNumberOfTrades),
                u'open':u'{}'.format(open),
                u'close':u'{}'.format(close),
                u'market open':u'{}'.format(marketOpen),
                u'market close':u'{}'.format(marketClose),
                u'change over time':u'{}'.format(changeOverTime),
                u'market change over time':u'{}'.format(marketChangeOverTime)
            })

            print("{} {}:  {}".format(symbol, minute, b))

            b += 1

        except IndexError:
            print("Index Error")
            break

【问题讨论】:

  • 没有任何错误是什么意思?您的 IndexError 异常中断,因此不会抛出,但仍应打印一些内容。或者在另一个方向上,是否有其他类型的异常发生而您没有发现?
  • 您可能在某处因 KeyError 异常而出错。如果您不能 100% 确定您获取的数据是否包含每个字段,您应该将 ["key"] 格式更改为 .get("key")
  • 所以我重新启动了我的机器并再次运行它。现在它一遍又一遍地抛出一个索引错误。有什么我需要改变的,以便它贯穿整个列表吗?
  • 您是否仍然获得至少 100 个结果?
  • 是的,它仍在前几家公司中运行并迭代他们的数据。对于每个符号,有 389 次数据提交,并且在给我索引错误之前它会运行前 5 个符号。

标签: python loops python-requests google-cloud-firestore


【解决方案1】:

你可以使用:

except Exception as errmsg:
        print(errmsg)

并提供更多信息

【讨论】:

    猜你喜欢
    • 2020-10-09
    • 2021-08-21
    • 2023-03-02
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-30
    相关资源
    最近更新 更多