我正在關注一本書中的教程,並且遇到了有關「\ n」的一些問題。Python shell - 新行
這裏是我被要求輸入在Python殼代碼:
from django.template import Template, Context
template = Template(
'{{ ml.exclaim }}!\n'
'she said {{ ml.adverb }}\n'
'as she jumped into her convertible {{ ml.noun1 }}\n'
'and drove off with her {{ ml.noun2 }}.\n'
)
mad_lib = {
'exclaim':'Ouch',
'adverb':'dutifully',
'noun1':'boat',
'noun2':'pineapple',
}
context = Context({'ml': mad_lib})
template.render(context)
所以每當我進入了Python shell中,談到回報它,因爲這一切在一次:
u'Ouch!\nshe said dutifully\nas she jumped into her convertible boat\nand drove off with her pineapple.\n'
我想擁有它出來像這樣所有在單獨的行:
Ouch!
she said dutifully
as she jumped into her convertible boat
and drove off with her pineapple.
所有幫助表示讚賞。
'打印template.render(上下文)' – ForceBru
模板採取HTML,所以你可以放心地使用''
,而不是'\ N' – karthikr
沒有涉及到HTML,爲什麼每個人都回答有關
– ospider