无法在 python 中遍历循环

unable to iterate through loop in python

我有一个 sql 查询,它基本上检索硬币名称并为每个硬币提交订单。 然而,它只提交了一个硬币的订单,并没有通过其余的循环,不知道为什么会这样。

import sys

**
import pandas as pd


postgreSQL_select_Query = "SELECT base,quote FROM instrument_static where exchange='ftx'"

cursor.execute(postgreSQL_select_Query)
row=([y for y in cursor.fetchall()])

for i in row:
   base=i[0]
   quote=i[1]
   portfolioItems = [
       {
           'exchange': 'ftx',
           'base': base,
           'quote': quote,
           'amount': 0.01,
       },

   ]


   def init():

       username = us
       password = passwordVal
       initialise(clientId, clientSecret, us, password)


if __name__ == "__main__":
       init()
       result = construct_portfolio_with_params(us, portname, portfolioItems)
       print(result)

您需要在循环之前初始化 portfolioItems,然后您可以添加到它。尝试替换这段代码:

...
row=([y for y in cursor.fetchall()])

portfolioItems = []

for i in row:
   base=i[0]
   quote=i[1]
   portfolioItems.append(
       {
           'exchange': 'ftx',
           'base': base,
           'quote': quote,
           'amount': 0.01,
       }

   )
...