0

这是我的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()

调用此模块的函数 deviation() 将结果作为 R 向量返回。如何使它返回 Json 对象的结果?谢谢!

4

1 回答 1

1

使用json.dumpssimplejson.dumps(取决于您的 python 版本)。你像这样使用它:

>> print( simplejson.dumps( { "dev": dev.s_deviation() } ) )
'{"dev":1.456}'
于 2012-08-29T08:47:14.900 回答