hghyami
hghyami

Reputation: 1

Python Working with unicode encoded Strings

Before i start asking anything i have already seen (Working with unicode encoded Strings from Active Directory via python-ldap).

Basically i have a web form where the user can submit data. as an example

I can submit "(nicht verfügbar)" fine and when i goto to database i can see "(nicht verfügbar)",

mysql> select * from tr_values where idvalues = 8905 \G *************************** 1. row *************************** idvalues: 8905 valuename: (nicht verfügbar) idkeys: 584 idvers: 1 idlangs: 5 1 row in set (0.00 sec)

However when i refresh the page i see

For my update query i am using this. db = mysql.connect(dbhost,dbuser,dbpass,dbname,use_unicode=True, charset="utf8")

and for my read query (rendering data on the page) db = mysql.connect(dbhost,dbuser,dbpass,dbname)

now if i set the charset on the read query my script will break and if not set then i will see the this " (nicht verf�gbar) ". As i mentioned submmiting data is working fine however display data (read from db) does not seem to be working and it looks like it is not doing the encoding correctly

  1. Python 2.7.3
  2. import os,cgi,MySQLdb as mysql
  3. Content-type: text/html;charset=UTF-8\r\n
  4. meta http-equiv="content-type" content="text/html;charset=UTF-8">

mysql> show create table tr_values \G *************************** 1. row *************************** Table: tr_values Create Table: CREATE TABLE `tr_values` ( `idvalues` int(11) NOT NULL AUTO_INCREMENT, `valuename` text, `idkeys` varchar(45) DEFAULT NULL, `idvers` varchar(45) DEFAULT NULL, `idlangs` varchar(45) DEFAULT NULL, PRIMARY KEY (`idvalues`) ) ENGINE=InnoDB AUTO_INCREMENT=9986 DEFAULT CHARSET=utf8 1 row in set (0.00 sec)

BTW : I dont speak German. I hope i have explained enough.

Upvotes: 0

Views: 81

Answers (1)

hghyami
hghyami

Reputation: 1

Ok I was able to fix it by doing the following

on my read connector add "use_unicode=True, charset="utf8"

and then where i am generating my data adding .encode('utf-8')

valuename = row[3].encode('utf-8')

hope this helps some one elses.

Upvotes: 0

Related Questions