Python嵌套for循环只运行一次,然后跳过其余的迭代

时间:2017-03-03 06:02:02

标签: python pandas for-loop

我希望这段代码能够获得15倍的数据量。当它运行时,它第一次进入所有的for循环,然后打印出答案,但是当它在第二次循环时在"对于num in范围(0,15):&# 34;循环,它进入"对于页面中的df:"然后不再深入到其他循环中,只返回" page_counter"按原样递增200。

每次" for num in range(0,15)时,我该怎么做才能让它运行最终的嵌套for循环:"循环?

 import pandas as pd

 symbol = 0
 for num in range(symbol,1):
     page_counter =0
     right = 1
     down = 0
     for num in range(0,15):
         page = pd.read_html('https://www.google.com/finance/historical?q=a&startdate=Jan+1%2C+2005&enddate=Mar+2%2C+2017&num=200&ei=Aom4WPizNcWO2AaSnZ3YBg'+ str(page_counter), attrs = {'class': 'gf-table'}, header=0)

         for df in page:
             for num in range(down,200):
                 open_data = df.at[down, 'Open']
                 high_data = df.at[down, 'High']
                 low_data = df.at[down, 'Low']
                 close_data = df.at[down, 'Close']
                 #volume_data = df.at[down, 'Volume']

                 print(open_data)

                 down += 1
         page_counter += 200
         print(page_counter)
      symbol += 1

P.S。我知道我的代码现在有点笨拙,这只是我的一个简单的想法。

1 个答案:

答案 0 :(得分:2)

for num in range(0,15):

中声明down = 0

在第一次运行后= 200,因此此循环不满足for num in range(down,200):范围条件。

 import pandas as pd

 symbol = 0
 for num in range(symbol,1):
     page_counter =0
     right = 1
     for num in range(0,15):
         down = 0
         page = pd.read_html('https://www.google.com/finance/historical?q=a&startdate=Jan+1%2C+2005&enddate=Mar+2%2C+2017&num=200&ei=Aom4WPizNcWO2AaSnZ3YBg'+ str(page_counter), attrs = {'class': 'gf-table'}, header=0)

         for df in page:
             for num in range(down,200):
                 open_data = df.at[down, 'Open']
                 high_data = df.at[down, 'High']
                 low_data = df.at[down, 'Low']
                 close_data = df.at[down, 'Close']
                 #volume_data = df.at[down, 'Volume']

                 print(open_data)

                 down += 1
         page_counter += 200
         print(page_counter)
      symbol += 1