Transform varchar to float

Asked

Viewed 207 times

0

I made a code that takes data from the comic to generate a graph with the matplotlib, but the data in the comic is as varchar and for the creation of the data it must be in the format float. How can one make the conversion?

# -*- coding: utf-8 -*-
import MySQLdb as mdb
import matplotlib.pyplot as plt
print(plt)

con = mdb.connect('localhost', 'root', '123456', 'testdb');
null = None
with con:
    cur = con.cursor()
    d = cur.execute("SELECT Ping FROM Pings WHERE Enderecos = 'www.peplink.com'")
    d = cur.fetchall()

    plt.plot(d)
    plt.title("Peplink")
    plt.show() 
  • No, because he presents himself later as "78.5 ms"

1 answer

1


I don’t know how the API works MySQLdb, but I believe that the cast query should work, assuming Ping be the column that has the value of varchar that you quoted in numerical format:

SELECT CAST(Ping AS DECIMAL(10,6)) as Ping FROM Pings WHERE Enderecos = 'www.peplink.com'

Using the DECIMAL(10,6), but can adjust the accuracy, I’m not sure what the effect will be after "passing" the API.

Now if this doesn’t work you can just take the result of Ping in Python itself and do something like this:

 float("10.5")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.