将函数映射到每个item元素

时间:2015-11-13 13:40:23

标签: python

这是一个相当简单的问题;我只是无法通过正常接近来弄明白。我有一个吐出csv和hdfs'对象'的函数。因为我需要单独运行此函数的许多项目,所以我使用map和其他方法将此函数应用于列表中的每个元素。但是,没有运气。任何人都可以了解可能出现的问题,

# define weekly pull code
def GT_Weekly_Run(keys):

    # connect to Google
    connector = pyGTrends(google_username, google_password)
    # make request
    connector.request_report(keys, geo="US")
    # wait a random amount of time between requests to avoid bot detection
    time.sleep(randint(5, 10))
    # download file
    connector.save_csv(path, '_' + "GT_Weekly" + '_' + keys)

    name = path + '_' + "GT_Weekly" + '_' + keys
    with open(name + '.csv', 'rt') as csvfile:    
        csvReader = csv.reader(csvfile)
        data = []

        global week_df
        data = [row for row in csvReader if row and row[0].startswith("20")]
        week_df = pd.DataFrame(data)

        cols = ["Date", keys]    
        week_df.columns = [cols]   
        week_df.to_csv(name + '.csv' , index=False)
        hdf.put(keys, week_df, format='table')

keywords = ['office_depot', 'office_supplies', 'office_desk', 'copy_paper', 'toner', 
            'ink_cartridge', 'printer', 'copier', 'scanner', 'cleaning_supplies', 
            'business_cards', 'postage_stamps', 'office_max']    


numbers = [GT_Weekly_Run(word) for word in keywords]

map(lambda x: GT_Weekly_Run(x), keywords)  

(GT_Weekly_Run(wd) for wd in keywords)

for wd in keywords:
    GT_Weekly_Run(wd)

map(GT_Weekly_Run, keywords)

def run(ks):
    for i in ks:
        GT_Weekly_Run(i)
run(keywords)

以下是常见错误消息:

  File "<ipython-input-25-c11c5a547b16>", line 1, in <module>
    map(GT_Weekly_Run(), keywords)

TypeError: GT_Weekly_Run() takes exactly 1 argument (0 given)



  File "C:\Anaconda\lib\site-packages\pandas\core\internals.py", line 2219, in set_axis
    'new values have %d elements' % (old_len, new_len))

ValueError: Length mismatch: Expected axis has 0 elements, new values have 2 elements



  File "C:\Anaconda\lib\site-packages\pandas\core\internals.py", line 2219, in set_axis
    'new values have %d elements' % (old_len, new_len))

ValueError: Length mismatch: Expected axis has 0 elements, new values have 2 elements

0 个答案:

没有答案