2016-05-12 98 views
-1

如何用燒瓶打印字典?Python燒瓶打印鍵

@app.route("/")  
def hello():  


    templateData = {  

    'canzone': vasco rossi,  
    'destinazione': /home/vasco.mp3  

    }  
    return render_template('main.html', **templateData)  

我的字典是由:

print dizionario: 
    vascorossi:/home/vasco.mp3 
    eros:/home/canzone.mp3 
    etc... 

而且我想這可能是在HTML:

的index.html:

vascorossi - albachiara 
    eros - canzone 
    all the keys and value contents 
+0

你知道神社模板? –

+0

你想迭代你的模板中的字典的鍵嗎?因爲你可以在jinja模板中做到這一點。您需要將字典作爲模板變量之一傳遞。 – Bhargav

回答

0

在蟒蛇更新此功能低於版本。

@app.route("/")  
def hello():  
    templateData = {  
     'canzone': vasco rossi,  
     'destinazione': /home/vasco.mp3  
    } 
    updated_dict = {} 
    for key in templateData.keys(): 
     updated_dict[key.decode('utf-8')] = templateData[key.decode('utf-8')].key.decode('utf-8') 
    return render_template('main.html', templateData=updated_dict) 

然後在main.html中添加一段腳本以在您的html頁面上顯示鍵值對。

main.html中

... 
{% for key in templateData.keys() %} 
    {{ key }} <br> 
    {{ templateData[key] }} 
{% endfor %} 
... 
+0

是的!這是正確的,但是當我寫: @ app.route( 「/」) DEF你好(): templateData = { '肯娜妮':dict.keys(), 'destinazione':dict.values() } 返回render_template( 'main.html中',templateData = templateData) 如果__name__ == 「__main__」: app.run(主機= '0.0.0.0',端口= 80,調試=真) 它們打印字典中的所有內容以及如何分別打印? templateData [key] templateData [value] templateData [Key] templateData [Value]? –

+0

你可以請更新你的問題,並添加一個你想要的格式化輸出? –

+1

@fabriziobianchi檢查,我已經解決了unicode錯誤問題 –