这是我的python模块:
#!/usr/bin/python
import rpy2.robjects as robjects
import MySQLdb as mdb
import json
import rpy2.robjects.vectors as ro
r= robjects.r
class Deviation:
def __init__(self):
print("Standard Deviation")
def s_deviation(self):
con = mdb.connect('localhost', 'root', 'devil', 'data')
cursor = con.cursor()
cursor.execute("SELECT * FROM traffic")
rows = cursor.fetchall()
rows = list(rows)
a = [x[1] for x in rows]
result = ro.IntVector(a)
a = r.sd(result)
print a
def deviation():
dev = Deviation()
deviation = dev.s_deviation()
调用时该模块的函数偏差()将结果返回为R向量。如何将结果作为Json对象返回? 谢谢!
答案 0 :(得分:1)
使用json.dumps
或simplejson.dumps
(取决于您的python版本)。
你这样使用它:
>> print( simplejson.dumps( { "dev": dev.s_deviation() } ) )
'{"dev":1.456}'