2017-08-30 33 views
0

format_map和** kwargs,函數做什麼?

from urllib.request import urlopen 

def urltemplate(template): 
    def opener(**kwargs): 
    return urlopen(template.format_map(kwargs)) 
    return opener 


yahoo = urltemplate('http://finance.yahoo.com/d/quotes.csv?s={names}&f={fields}') 
for line in yahoo(names='IBM,AAPL,FB,CGG', fields='sl1c1v'): 
    print(line.decode('utf-8')) 

正如我們

"IBM",143.14,+0.63,3009520 

"AAPL",162.91,+1.44,29516910 

"FB",168.05,+0.81,11120986 

"CGG",5.46,-0.57,42543 

開瓶器採用命名的參數,然後在接下來的步驟我不明白,結果以何種方式format_map地圖的論點?爲什麼format_map而不是format

+1

https://docs.python.org/3/library/stdtypes.html#str.format_map –

回答

1
help(str.format_map) 

Help on method_descriptor: 

format_map(...) 
    S.format_map(mapping) -> str 

    Return a formatted version of S, using substitutions from mapping. 
    The substitutions are identified by braces ('{' and '}'). 

它是象下面這樣:

​​