亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

tornado 模板加載一段json數據格式顯示問題

tornado 模板加載一段json數據格式顯示問題

米脂 2018-10-12 19:30:24
我在一個tornado應用的handler中從mongodb中讀到了一個集合的所有文檔,將轉換成了json格式,然后我用這些json數據為傳入了tornado模板中,顯示在了瀏覽器中。handler的代碼是樣的:class MainHandler(BaseHandler):     def get(self):         codes = self.db.code.find()                 result = json.dumps(list(codes),              default=json_util.default,              sort_keys=True,              ensure_ascii=False,              indent=4)         print result         self.render('index.html', content = result)瀏覽器中顯示是這樣的:就是一個沒換行的狀態。在終端打印時,卻又是換了行的:記得在django中,可以使用content.linebreaks來換行換行之類的,在tornado中,卻沒找到這種方法。煩請幫助一下。
查看完整描述

1 回答

?
一只斗牛犬

TA貢獻1784條經驗 獲得超2個贊

dumps的時候,是不會加縮進之類的。你的dumps指定了indent=4。

indent參數針對打印輸出時候的format

具體可以看文檔:

If indent is a non-negative integer (it is None by default), then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. None is the most compact representation.

還有一個比較好的工具,標準庫pprint用來print對象的時候顯示更美觀:


from?pprint?import?pprint
pprint(your_obj)


查看完整回答
反對 回復 2018-10-28
  • 1 回答
  • 0 關注
  • 1117 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號