gpt4 book ai didi

python: jsonify 仅显示一个输出

转载 作者:太空宇宙 更新时间:2023-11-03 14:05:29 24 4
gpt4 key购买 nike

json 中有 10 个输出,但仅显示第一个输出。如何显示所有 10 个输出?

from collections import OrderedDict
import requests
from lxml import html


@app.route('/saptop', methods=['GET'])
def saptop():

page_indo = requests.get('http://www.waterfrontsekuritas.com/marketview')
indo = html.fromstring(page_indo.content)
indo = indo.xpath('//table[@id="top-gainer"]//td/text()')

col = ['Stockcode','Lastprice','Prevprice','pc','Change','Tfreq','Vol','Value']

c1 = [OrderedDict(zip(col,indo))]
return jsonify({'Stock': c1})

当前输出仅显示第一个

{
"Stock": [
{
"Stockcode": "BOSS",
"Lastprice": "1,400",
"Prevprice": "1,120",
"pc": "25.00",
"Change": "280",
"Tfreq": "3,640",
"Vol": "23,087,100",
"Value": "29,132,765,000"
}
]
}

更改为 c1 = [OrderedDict(zip(col,t)) for t in indo] 时,输出如下,这也是错误的,所有字符串均由字母和数字分隔:

{
"Stock": [
{
"Stockcode": "B",
"Lastprice": "O",
"Prevprice": "S",
"pc": "S"
},
{
"Stockcode": "1",
"Lastprice": ",",
"Prevprice": "4",
"pc": "0",
"Change": "0"
},
{
"Stockcode": "1",
"Lastprice": ",",
"Prevprice": "1",
"pc": "2",
"Change": "0"
},
{
"Stockcode": "2",
"Lastprice": "5",
"Prevprice": ".",
"pc": "0",
"Change": "0"
},

如何在 Flask 中正确显示所有 10 个结果?是json格式

最佳答案

您需要将列表格式转换为

indo = ['abc','123','cde','efg','112','123'][['abc','123' ],['cde','efg'],['112','123']]

对于您的示例,只需添加即可更改

indo = indo.xpath('//table[@id="top-gainer"]//td/text()')
indo = [indo[n:n+8] for n in range(0, len(indo), 8)]

关于python: jsonify 仅显示一个输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48927600/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com