2013-06-13 32 views
0

我有循環腳本返回不同的過濾結果,我可以使這個數據返回爲每個不同的過濾器類的數組。不過,我不確定將所有這些陣列連接在一起的最佳方法。如何將循環腳本的結果加入單個變量?

import mechanize 
import urllib 
import json 
import re 
import random 
import datetime 
from sched import scheduler 
from time import time, sleep 
from sets import Set 

##### Code to loop the script and set up scheduling time 
s = scheduler(time, sleep) 
random.seed() 

##### Code to stop duplicates part 1 
userset = set() 

def run_periodically(start, end, interval, func): 
    event_time = start 
    while event_time < end: 
     s.enterabs(event_time, 0, func,()) 
     event_time += interval + random.randrange(-5, 10) 
    s.run() 

##### Code to get the data required from the URL desired 
def getData(): 
    post_url = "URL OF INTEREST" 
    browser = mechanize.Browser() 
    browser.set_handle_robots(False) 
    browser.addheaders = [('User-agent', 'Firefox')] 

##### These are the parameters you've got from checking with the aforementioned tools 
    parameters = {'page' : '1', 
        'rp' : '250', 
        'sortname' : 'race_time', 
        'sortorder' : 'asc' 
       } 
##### Encode the parameters 
    data = urllib.urlencode(parameters) 
    trans_array = browser.open(post_url,data).read().decode('UTF-8') 

    xmlload1 = json.loads(trans_array) 
    pattern2 = re.compile('/control/profile/view/(.*)\' title=') 
    pattern4 = re.compile('title=\'posted: (.*) strikes:') 
    pattern5 = re.compile('strikes: (.*)\'><img src=') 

    for row in xmlload1['rows']: 
     cell = row["cell"] 

##### defining the Keys (key is the area from which data is pulled in the XML) for use in the pattern finding/regex 

     user_delimiter = cell['username'] 
     selection_delimiter = cell['race_horse'] 

     user_numberofselections = float(re.findall(pattern4, user_delimiter)[0]) 
     user_numberofstrikes = float(re.findall(pattern5, user_delimiter)[0]) 
     strikeratecalc1 = user_numberofstrikes/user_numberofselections 
     strikeratecalc2 = strikeratecalc1*100 
     userid_delimiter_results = (re.findall(pattern2, user_delimiter)[0]) 


##### Code to stop duplicates throughout the day part 2 (skips if the id is already in the userset) 

     if userid_delimiter_results in userset: continue; 
     userset.add(userid_delimiter_results) 

     arraym = "" 
     arrayna = "" 

     if strikeratecalc2 > 50 and strikeratecalc2 < 100): 

      arraym0 = "System M" 
      arraym1 = "user id = ",userid_delimiter_results 
      arraym2 = "percantage = ",strikeratecalc2,"%" 
      arraym3 = "" 
      arraym = [arraym0, arraym1, arraym2, arraym3] 

     if strikeratecalc2 > 0 and strikeratecalc2 < 50): 

      arrayna0 = "System NA" 
      arrayna1 = "user id = ",userid_delimiter_results 
      arrayna2 = "percantage = ",strikeratecalc2,"%" 
      arrayna3 = "" 
      arrayna = [arrayna0, arrayna1, arrayna2, arrayna3] 


getData() 

run_periodically(time()+5, time()+1000000, 10, getData) 

我希望能夠做的,就是在腳本中的舊的每個循環返回兩個「arraym」和「arrayna」作爲最後一個陣列,但是由於腳本的循環性質'arraym'/'arrayna'被覆蓋,當前我試圖產生一個包含所有數據的數組,導致'systemm'的最後一個userid和'sustemna'的最後一個userid。這顯然是因爲,在循環的每次運行中,它會覆蓋舊的'arraym'和'arrayna',但我不知道如何解決這個問題,這樣我的所有數據都可以在一個數組中累積。請注意,我現在已經累積了兩週的編碼,所以可能有一些簡單的功能來解決這個問題。

親切的問候AEA

+0

如果你能總結你的曲線通常很有幫助estion /代碼成爲您的問題的一個更小的例子。有了這些代碼,它使回答你的問題變得困難。 – jedwards

+0

我的確嘗試儘可能多地總結代碼,本質上它只是底部的那兩個數組,我需要將它們合併到一個數組中,但我認爲我應該很明顯地看到這段代碼是循環的(沒有希望在考慮答案時忽略一些重要的代碼)親切的問候AEA – AEA

回答

2

不看那個巨大的代碼段,通常你可以這樣做:

my_array = [] # Create an empty list 
for <some loop>: 
    my_array.append(some_value) 


# At this point, my_array is a list containing some_value for each loop iteration 
print(my_array) 

查找到Python的list.append()

所以,你的代碼可能看起來像:

#... 
arraym = [] 
arrayna = [] 

for row in xmlload1['rows']: 
    #... 
    if strikeratecalc2 > 50 and strikeratecalc2 < 100): 
     arraym.append("System M") 
     arraym.append("user id = %s" % userid_delimiter_results) 
     arraym.append("percantage = %s%%" % strikeratecalc2) 
     arraym.append("") 
    if strikeratecalc2 > 0 and strikeratecalc2 < 50): 
     arrayna.append("System NA") 
     arrayna.append("user id = %s" % userid_delimiter_results) 
     arrayna.append("percantage = %s%%" % strikeratecalc2) 
     arrayna.append("") 
#... 
+0

謝謝傑德華茲的回答,我會給這個答案:) – AEA

相關問題