Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中将字符串数组列表传递给函数_Python_Python 3.x_List_Function_Web Scraping - Fatal编程技术网

在Python中将字符串数组列表传递给函数

在Python中将字符串数组列表传递给函数,python,python-3.x,list,function,web-scraping,Python,Python 3.x,List,Function,Web Scraping,我是Python的初学者,我正在尝试编写一个函数 对于symbol,我有一个从数据库获取的列表 cur.execute("SELECT sym FROM data") list = cur.fetchall() ####fetches list from database column [['AAPL'] ####This is the output of list ['MSFT'] ['GO

我是Python的初学者,我正在尝试编写一个函数

对于symbol,我有一个从数据库获取的列表

cur.execute("SELECT sym FROM data")

list = cur.fetchall()              ####fetches list from database column

 

[['AAPL']                       ####This is the output of list
 ['MSFT']
 ['GOOGL']
 ['TSLA']
 ['AMZN']
 ['FB']
 ['V']
 ['BABA']
 ['WMT']]
如何在符号中为以下函数传递此列表的值

    symbol = 
    url_cf = 'https://somewebsite.com' + symbol

    def scrape_data():
        def dir_create():
            # base dir
            _dir = "/Users/Desktop/Companies"
    
            # create dynamic name
            _dir2 = os.path.join(_dir, symbol)
    
            # create 'dynamic' dir, if it does not exist
            if not os.path.exists(_dir2):
                os.makedirs(_dir2)
    
    
        dir_create()
    
    
        # Scraping Cash Flow
        def cash_flow():
        .
        .
        page = requests.get(url_cf, headers)
        .
        .
        #### Some function for scraping
        .
        .
        path = '/Users/Desktop/Companies' + '/' + symbol
        sheet_name = str('cash_flow_' + symbol + '.xlsx')
        .
        .
        cash_flow()

scrape_data()

我希望对列表中的每个符号执行scrape_data()。

使用参数和循环:

def scrape_data(symbol):
    url_cf = f'https://somewebsite.com/{symbol}'
    ....


for sym, in list:
    scrape_data(sym)
在Python3中,可以使用f字符串使变量连接更容易
注意
循环声明的
sym
之后的
。这将解压缩列表中的列表元素。如果每行有多个值,则此操作将失败。如果我正确理解您的问题,则只需将列表传递给函数,如下所示:

def scrape_data(symbol):
    #...

scrape_data(symbol)
在这种情况下,重要的一点是,
symbol
是一个列表,您必须在函数中循环使用它们。 但是,如果要为
符号中的每个值运行函数,则应执行以下操作:

def scrape_data(symbol):
    #...

for symb in symbol:
    scrape_data(symb)
在这种情况下,函数将从列表中逐个获取元素

另外,请重命名您的列表。函数外部的列表和函数内部的字符串使用相同的名称