开发者

converting object value into float in python

i got timestamp returned as a tuple from rrd database i.e. info variable will contain this

[(38177492.733055562, 38177482.886388876),(39370533.190833323, 40563588.018611118)]

inside my code i am converting tuple (38177492.733055562,38177482.886388876) intto list using list function after that i am accessing element using "index" variable and when i am passing this value to fromtimestamp function it is asking me the "float" value so how can i convert list object into float primitive data type ? following is the whole code

def hello(request):

        info = rrdtool.fetch('hello/test.rrd','AVERAGE','-r','3600','-s','1298264400','-e','1298350800')

    datalist = list()
    for index in range(0,len(info)):    
         val = list(info[index])
         dt_obj = datetime.fromtimestamp(float(val[index]))
         str=dt_obj.strftime("%d-%m-%Y %H:%M:%S")     
         datalist.append(str)

        data = simplejson.dumps(info, indent=4)
        return HttpResponse(data,mimetype='application/javascript')

i am getting follow开发者_高级运维ing error

a float is required

after changing code to for index in range(0,len(info)): i am getting following error

invalid literal for float(): Out


What you're doing doesn't make much sense, I'm afraid - index is an index into the info list, not into val, which is one of the tuples in that list.

Also, you don't need to convert the list into a tuple in order to access an element of the tuple.

Your indentation is broken in the line where the loop starts, and also when you outdent to return a json version of info.

To work out your problem, you could start by printing val, val[index] and type(val[index]) on the line before your error to check that they are what you're expecting them to be - clearly they're not from the error you get.

Also, you don't need to use the index into the info list at all, you could iterate over the values in the list with:

for val in info:
    # I'm not sure if you want both values in the tuple here or just one...
    print >> sys.stderr, "val =", val
    dt_obj = datetime.fromtimestamp(val[0])
    str=dt_obj.strftime("%d-%m-%Y %H:%M:%S")     
    datalist.append(str)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜