python mariadb & html tables
MRAB
python at mrabarnett.plus.com
Tue Sep 15 15:41:57 EDT 2020
On 2020-09-15 19:41, SS wrote:
> I'm trying to create an table in html from a Maria DB table, from a python script. I'm getting some unexpected results.
>
> The environment is Centos 7, I'm using Python3 with apache.
>
> Here is copy of the script I'm using:
>
> ******* SCRIPT START *********
>
> import mysql.connector
> import cgitb
> cgitb.enable()
>
> mydb = mysql.connector.connect(
> host="localhost",
> user="root",
> password="somepassword",
> database="somedb"
> )
>
> mycursor = mydb.cursor()
>
> mycursor.execute("select name, provisioned_space, used_space, memory_size, cpus, ip_address, host_cpu, host_mem from projecttable where name like '%newproject%'")
>
> myresult = mycursor.fetchall()
>
> print "Content-type: text/plain:charset=utf-8"
> print
> for x in myresult:
> print(x)
>
>
> ******* SCRIPT STOPS *********
>
> It works. But I get alot of extraneous data I don't need or want. Here is an example of the output:
>
> ********* OUTPUT STARTS ***********
>
> Content-type: text/plain:charset=utf-8
>
> (u'host1.mydomain.com', u'106.11 GB', u'32.72 GB', u'1 GB', u'1', u'172.18.33.62', u'Running', u'16 MHz')
> (u'hopst2.mydomain.com', u'106.08 GB', u'56.87 GB', u'1 GB', u'1', u'172.17.4.82', u'Running', u'0 Hz')
>
> ********* OUTPUT STOPS ***********
>
> Is this typical of Python? Do I need create another script to clean up the output? Or is there a better way to extract data from a MariaDB instance, and put it in an HTML table? Any advise would be appreciated.
>
That's not Python 3, that's Python 2.
The u'host1.mydomain.com' is a Unicode string. If you're going to print
it in Python 2, you'll need to encode it to bytes, UTF-8 in this case.
More information about the Python-list
mailing list