TypeError:“功能”对象在python

时间:2019-02-17 16:34:22

标签: python python-2.7 argparse

我创建了一个get_config函数,以便将两个参数传递给我的主函数。运行脚本时,似乎出现以下错误:

File "querysearch.py", line 14, in main
    search_type = get_config[0]
TypeError: 'function' object has no attribute '__getitem__'

我错误地传递了args吗?我的脚本如下:

import argparse

def get_config():
    parser = argparse.ArgumentParser(description='Search your keyword ex: querycheck.py andsearch general,population,Alzheimer')
    parser.add_argument('searchtype', type=str, help='Search type orsearch and andsearch only ')
    parser.add_argument('Value', type=str, help='Parameter to search')
    args = parser.parse_args()
    return [args.searchtype , args.Value]


def main(get_config):
    finallist = []
    counter = 0
    search_type = get_config[0]
    value = get_config[1]


    if search_type == "orsearch":
        _prasplit = value.split(",")
        with open("hscic-news", "r") as newsfile:
            ncontent = newsfile.readlines()
            for x in range(len(ncontent)):
                for y in _prasplit:
                    if y in ncontent[x]:
                        finallist.append(x)


    #   print (list(set(finallist)))
        list_with_duplicates = list(set(finallist))
        final_list = list(set(finallist))
        result = final_list
        print(result)

1 个答案:

答案 0 :(得分:0)

您实际上并不是在调用get_config函数。

将get_config函数的最后一行更改为return args.searchtype, args.Value

然后,将主函数中的行更改为search_type, value = get_config()