2016-07-21 6 views
1

書き込まれるテキストファイル内で実行されたループが5行だけであればループを実行します。その理由は、テキストファイルの最後の5行から平均を計算し、プログラムに5つの数値がなければ、rumtimeエラーがスローされます。テキストファイルのn行後のトリガーループ

#Imports 
    from bs4 import BeautifulSoup 
    from urllib import urlopen 
    import time 

    #Required Fields 
    pageCount = 1290429 


    #Loop 
    logFile = open("PastWinners.txt", "r+") 
    logFile.truncate() 
    while(pageCount>0): 
     time.sleep(1) 
     html = urlopen('https://www.csgocrash.com/game/1/%s' % (pageCount)).read() 
     soup = BeautifulSoup(html, "html.parser") 

     try: 
      section = soup.find('div', {"class":"row panel radius"}) 
      crashPoint = section.find("b", text="Crashed At: ").next_sibling.strip() 
      logFile.write(crashPoint[0:-1]+"\n") 
     except: 
      continue 

     for i, line in enumerate(logFile):    #After 5 lines, execute this 
      if i > 4: 
       data = [float(line.rstrip()) for line in logFile] 
       print("Average: " + "{0:0.2f}".format(sum(data[-5:])/len(data[-5:]))) 
      else: 
       continue 

     print(crashPoint[0:-1]) 
     pageCount+=1 
    logFile.close() 

If anyone knows the solution, or knows a better way to go about doing this, it would be helpful, thanks :). 

編集:

更新されたコード:

#Imports 
from bs4 import BeautifulSoup 
from urllib import urlopen 
import time 

#Required Fields 
pageCount = 1290429 
lineCount = 0 

def FindAverage(): 
    with open('PastWinners.txt') as logFile: 
     data = [float(line.rstrip()) for line in logFile] 
     print("Average: " + "{0:0.2f}".format(sum(data[-5:])/len(data[-5:]))) 

#Loop 
logFile = open("PastWinners.txt", "r+") 
logFile.truncate() 
while(pageCount>0): 
    time.sleep(1) 
    html = urlopen('https://www.csgocrash.com/game/1/%s' % (pageCount)).read() 
    soup = BeautifulSoup(html, "html.parser") 

    if lineCount > 4: 
     logFile.close() 
     FindAverage() 
    else: 
     continue 

    try: 
     section = soup.find('div', {"class":"row panel radius"}) 
     crashPoint = section.find("b", text="Crashed At: ").next_sibling.strip() 
     logFile.write(crashPoint[0:-1]+"\n") 
    except: 
     continue 

    print(crashPoint[0:-1]) 
    pageCount+=1 
    lineCount+=1 


logFile.close() 

新しい問題: 期待通りにプログラムが実行される平均が計算され、表示された後、しかし、プログラムがループしません再び、それは止まる。私はそれが5行の後に平均を計算し、次の数を表示してから、新しい平均を表示するようにしたいので、それを動作させたいと思います。

+1

あなたのコードは動作していますか?そうでない場合は、期待した結果と実際の結果の例を表示できますか? – spectras

+0

また、少なくとも4行の終わりがあるかどうかを確認することも考えましたか?または空白行を渡したいですか? – HolyDanna

+0

書かれた行を数えることはできませんし、数が4より大きいまで「続ける」ことはできませんか? – martineau

答えて

0

最後の問題は、ループが再起動せず、最初の平均計算で終了したことでした。これはlogFileがクローズされていて再オープンされていなかったためで、プログラムがそれを考えてファイルに追加するので、期待どおりに動作します。助けてくれてありがとう。

#Imports 
from bs4 import BeautifulSoup 
from urllib import urlopen 
import time 

#Required Fields 
pageCount = 1290429 
lineCount = 0 

def FindAverage(): 
    with open('PastWinners.txt') as logFile: 
     data = [float(line.rstrip()) for line in logFile] 
     print("Average: " + "{0:0.2f}".format(sum(data[-5:])/len(data[-5:]))) 

#Loop 
logFile = open("PastWinners.txt", "r+") 
logFile.truncate() 
while(pageCount>0): 
    time.sleep(1) 
    html = urlopen('https://www.csgocrash.com/game/1/%s' % (pageCount)).read() 
    soup = BeautifulSoup(html, "html.parser") 

    try: 
     section = soup.find('div', {"class":"row panel radius"}) 
     crashPoint = section.find("b", text="Crashed At: ").next_sibling.strip() 
     logFile.write(crashPoint[0:-1]+"\n") 
    except: 
     continue 

    print(crashPoint[0:-1]) 
    pageCount+=1 
    lineCount+=1 

    if lineCount > 4: 
     logFile.close() 
     FindAverage() 
     logFile = open("PastWinners.txt", "a+") 
    else: 
     continue 
logFile.close() 
0

whileループは決して終了しません。私はあなたが減少することを意味すると思う:pageCount-=1

+0

良い点はありますが、更新されたコードの 'ZeroDivisionError'については説明しません。 – martineau

+0

私はウェブサイトからデータを絶えず引き出しているので、このループは永遠に続くものです。私は実際にこれを今のところ稼働させることに成功しましたが、スレッドに今追加する別の問題があります。 –

関連する問題