首页 > 解决方案 > 熊猫系列到 json 数组

问题描述

我正在尝试在 python 中准备 json 数组以在 PHP 中进行处理。我从“for”循环中得出结果:

for Pr in range(150,370,20):
   for Te in range(250,320,10):
       maxvGuess = np.array([0.04])
       y = fsolve(F_vol, maxvGuess, Pr)*1000
       forphp = pd.Series({'Volume': str(y).lstrip('[').rstrip(']'), 'Pressure': Pr, 'Temperature': Te})
       arr = forphp.to_json()
       print(arr)

我得到这样的结果:

{'Volume': ' 57.23582288', 'Pressure': 150, 'Temperature': 250} 
{'Volume': ' 59.62839406', 'Pressure': 150, 'Temperature': 260} 
{'Volume': ' 61.98900065', 'Pressure': 150, 'Temperature': 270} 
{'Volume': ' 64.32323062', 'Pressure': 150, 'Temperature': 280} 
........

我需要一个具有上述结果的 json 结构,以便在 PHP 中对其进行解码:

[
{'Volume': ' 57.23582288', 'Pressure': 150, 'Temperature': 250}, 
{'Volume': ' 59.62839406', 'Pressure': 150, 'Temperature': 260}, 
{'Volume': ' 61.98900065', 'Pressure': 150, 'Temperature': 270}, 
{'Volume': ' 64.32323062', 'Pressure': 150, 'Temperature': 280}, 
........
]

我尝试了很多组合(数据框、json.dumps 等)并浏览了很多帖子,但就是不知道如何制作正确的 jason 数组。我不是程序员,如果这是一个微不足道的问题,请提前道歉。

标签: pythonjson

解决方案


我不确定您真正想要什么,但请尝试这样做:

l=[]
for Pr in range(150,370,20):
    for Te in range(250,320,10):
        maxvGuess = np.array([0.04])
        y = fsolve(F_vol, maxvGuess, Pr)*1000
        forphp = pd.Series({'Volume': str(y).lstrip('[').rstrip(']'), 'Pressure': Pr, 'Temperature': Te})
        l.append(dict(forphp))

jsonobj = json.dumps(l)
print (jsonobj)

推荐阅读