python - 熊猫系列到 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 数组。我不是程序员,如果这是一个微不足道的问题,请提前道歉。
解决方案
我不确定您真正想要什么,但请尝试这样做:
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)
推荐阅读
- c# - 无法使用 LINQ 连接从右侧列表中选择值
- python-3.x - 如何以所有可能的方式将列表划分为大小为 k 的子字符串
- robotframework - 使用ride.py 命令打开 RIDE(机器人框架 IDE)时出错
- react-native - 有什么方法可以在 React Native for iOS 中实现真正的暗模式?
- visual-studio - 特定于配置的 resw 资源字符串变体
- asp.net - 更新表单下拉列表的角度设置值
- python - Html 无法识别块、结束块参数。Django 查询
- javascript - React CRUD 应用程序中的错误:TypeError:无法解构“this.props.event”的属性“id”,因为它未定义
- c++ - 如何为 RGB 键盘编写微控制器?
- python - 将烧瓶部署到单个服务器