2014-02-07 10 views
0

我試圖從csv文件的第一列中逐步加載值到URL中,並以5秒的延遲一次請求一個URL。在第一列的每個值應替換爲「theid」嘗試延遲運行已定義的函數

這是到目前爲止我的代碼:

# I have a defined function 
def withid (theid): 
    """""" 


    global cache 

    dupe = False 

    theurl = "{0}{1}{2}".format(OMDBURL, "?i=", theid) 

    response = urllib2.urlopen(theurl) 

    movdata = json.load(response) 

    for mov in cache: 
     if movdata[MKEY[1]] == mov[MKEY[1]]: 
      dupe = True 
    if not dupe: 
     cache.append(movdata) 



outfile2 = open('outputrows2-shortened.csv', 'rb') 
for row in outfile2: 
    theid = outfile2(row[0]) 
    time.sleep(5) 

輸出:類型錯誤:「文件」對象不是可調用

+0

還有......是什麼問題? – jonrsharpe

+0

它不工作,我只是得到上面的錯誤信息 – kegewe

+0

這是因爲'atid'(now)返回一個字典;你想把它附加到jsonlist,而不是相反。試試'jsonlist.append(withid(row [0]))''。 –

回答

0

withid()函數永遠不會返回任何東西。嘗試在其末尾添加return movdata

這裏是一個重寫的版本,可能會有所幫助:

import csv 
import json 
import time 
import urllib2 

PAGE_DELAY = 5. # time between loading pages 
PAGE_LOAD = 0.3 # how long it takes to load a page 

make_url = 'http://www.imdb.com/title/tt{}/'.format 

def get_csv_column(csv_fname, col, **kwargs): 
    with open(csv_fname, 'rb') as inf: 
     incsv = csv.reader(inf, **kwargs) 
     column = [row[col] for row in incsv] 
    return column 

def get_data_by_id(id): 
    url = make_url(id) 
    response = urllib2.urlopen(url) 
    data = json.load(response) 
    return id,data 

def delayed(delay, fn, *args): 
    time.sleep(delay) 
    return fn(*args) 

def human_time(seconds): 
    if seconds >= 86400: 
     return '{:0.1f} days'.format(seconds/86400.) 
    elif seconds >= 3600: 
     return '{:0.1f} hours'.format(seconds/3600.) 
    elif seconds >= 60: 
     return '{:0.1f} minutes'.format(minutes/60.) 
    else: 
     return '{:0.1f} seconds'.format(seconds) 

def main(): 
    ids = get_csv_column('outputrows2.csv', 0) 

    expected = (PAGE_DELAY + PAGE_LOAD) * len(ids) 
    print('This will take about {}.'.format(human_time(expected))) 

    results = (delayed(PAGE_DELAY, get_data_by_id, id) for id in ids) 
    moviedata = dict(results)  # => gives dict of {id:data} 

if __name__=="__main__": 
    main() 
+0

列中的數據,我可以將這些定義中的哪一個輸出到文本文件中? – kegewe

+0

@kegewe:你想對文本文件做什麼? 'data'是任何以json形式返回的字典。你需要把你之後的任何東西拉出來併發送到你的輸出文件。 –

+0

我想在文本文件的每一行中輸入每個ID的整個JSON字符串 – kegewe