Flask JSONify在新行上打印结果 [英] Flask jsonify print results on new lines

查看:54
本文介绍了Flask JSONify在新行上打印结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次使用Flask,我创建了一个非常基本的应用程序,并且试图打印推荐系统的结果.第一组代码来自我的python函数(print_most_like),并正在创建一个格式化的字符串,以期在新行上打印每个REC.代码的第二部分显然是我的烧瓶路由.您会看到烧瓶部分调用了该函数,因此返回了"y".我相信jsonify不会使用\ n字符.我尝试在字符串格式中仅使用'\ n',但它只是显示为字符串.就像"\ t"一样.

First time using Flask, I have created a very basic app and I am trying to print the results of a recommender system. The first set of code is from my python function (print_most_similar) and is creating a formatted string in hopes to print every REC on a new line. The second section of code is obviously my flask routing. You can see that the flask part calls the function, so it is returned 'y'. I believe the jsonify will not take the \n characters. I have tried using just '\n' in the string formatting, but it just appears as a string. As does '\t'.

for k in range(len(sugg)):
    x = str("REC {}: {}\\n".format(k+1, sugg[k]))
    y += x
return y

@app.route("/getrecomm",methods=['GET','POST'])
def getrecomm():
    restname = request.args.get('restname', type=str)
    number = request.args.get('number', type=int)
    i = getBusIndex(restname, names)
return make_response(jsonify(result=(print_most_similar(rating, names, i, number))),200)

当前,结果显示如下:REC 1:Harbor House Cafe&休息室\ nREC 2:星巴克\ nREC 3:麦当劳\ nREC 4:塔可钟\ nREC 5:熊猫快车\ n

Currently, the results print like this: REC 1: Harbor House Cafe & Lounge\nREC 2: Starbucks\nREC 3: McDonald's\nREC 4: Taco Bell\nREC 5: Panda Express\n

我希望他们像这样打印:REC 1:Harbor House Cafe&休息室REC 2:星巴克REC 3:麦当劳REC 4:塔可钟REC 5:Panda Express

I would like them to print like this: REC 1: Harbor House Cafe & Lounge REC 2: Starbucks REC 3: McDonald's REC 4: Taco Bell REC 5: Panda Express

我正在使用python 3,fyi.任何建议将不胜感激!

I'm using python 3, fyi. Any suggestions would be super appreciated!

推荐答案

摘要

  • 答案:< br>
  • 替代:JSONView Chrome扩展程序
  • Answer: <br>
  • Alternative: JSONView Chrome Extension

唯一给我好的结果的是< br> :

The only one that give me good results was <br>:

示例

from flask import Flask, jsonify

app = Flask(__name__)

tasks = [
    {
        '<br>id': 1,
        'title': u'Buy groceries',
        'description': u'Milk, Cheese, Pizza, Fruit, Tylenol', 
        'done': False
    },
    {
        '<br>id': 2,
        'title': u'Learn Python',
        'description': u'Need to find a good Python tutorial on the web', 
        'done': False
    }
]

@app.route('/todo/api/v1.0/tasks', methods=['GET'])
def get_tasks():
    return jsonify({'tasks': tasks})

if __name__ == '__main__':
    app.run(debug=True)

在您的浏览器中,< br> 字符将被呈现为html并重新生成新行.

In your browser the <br> character will be rendered as html and reproduce a new line.

结果:
在json中创建"新行

Jsonify无法帮助您,因为它将值(整数,布尔值,浮点数等)作为字符串,并且避免使用特殊字符,例如 \ n \ t

Jsonify can't help you because it takes the values (integer,boolean,float, etc) as a string and avoid special characters like \n, \t, etc

最后,如果您只想以一种精美的方式在浏览器中可视化json文件,则可以使用JSONView,这是一个Chrome扩展程序,可以像这样更易于理解的方式呈现Json文件.

Finally, if you just want a fancy way to visualize json files in your browser, you could use JSONView, is a Chrome extension that render Json files in a more understandable way, like this.

使用JSONView渲染

这篇关于Flask JSONify在新行上打印结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆