'ascii' codec can't encode character u'\u2013'
Hi Using Python 2.3.4 + Feedparser 3.3 (a library to parse XML documents) I'm trying to parse a UTF-8 document with special characters like acute-accent vowels: ... --- But I get this error message: --- UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 122: ordinal not in range(128) --- when trying to execute a MySQL query: query = "UPDATE blogs_news SET text = '" + text_extrated + "'WHERE id='" + id + "'" cursor.execute (query) #<--- error line I tried with: --- text_extrated = text_extrated.encode('iso-8859-1') #<--- error line query = "UPDATE blogs_news SET text = '" + text_extrated + "'WHERE id='" + id + "'" cursor.execute (query) --- But I get this error: -- UnicodeEncodeError: 'latin-1' codec can't encode character u'\u2013' in position 92: ordinal not in range(256) - I also tried with: text_extrated = re.sub(u'\u2013', '-' , text_extrated) query = "UPDATE blogs_news SET text = '" + text_extrated + "'WHERE id='" + id + "'" cursor.execute (query) - It works, but I don't want to substitute each special character, because there are always forgotten ones which can crack the program. Any suggestion to fix it? Thank you very much. -- http://mail.python.org/mailman/listinfo/python-list
Re: 'ascii' codec can't encode character u'\u2013'
Hi. Thank you both for your answers. Finally I changed my MySQL table to UTF-8 and changed the structure of the query (with '%s'). It works. Thank you very much. 2005/9/30, deelan <[EMAIL PROTECTED]>: > thomas Armstrong wrote: > (...) > > when trying to execute a MySQL query: > > > > query = "UPDATE blogs_news SET text = '" + text_extrated + "'WHERE > > id='" + id + "'" > > cursor.execute (query) #<--- error line > > > > well, to start it's not the best way to do an update, > try this instead: > > query = "UPDATE blogs_news SET text = %s WHERE id=%s" > cursor.execute(query, (text_extrated, id)) > > so mysqldb will take care to quote text_extrated automatically. this > may not not your problem, but it's considered "good style" when dealing > with dbs. > > apart for this, IIRC feedparser returns text as unicode strings, and > you correctly tried to encode those as latin-1 str objects before to > pass it to mysql, but not all glyphs in the orginal utf-8 feed can be > translated to latin-1. the charecter set of latin-1 is very thin > compared to the utf-8. > > you have to decide: > > * switch your mysql db to utf-8 and encode stuff before > insertion to UTF-8 > > * lose those characters that cannot be mapped into latin-1, > using the: > > text_extrated.encode('latin-1', errors='replace') > > so unrecognized chars will be replaced by ? > > also, mysqldb has some support to manage unicode objects directly, but > things changed a bit during recent releases so i cannot be precise in > this regard. > > HTH. > > -- > deelan, #1 fan of adriana lima! > <http://www.deelan.com/> > > > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
/usr/lib/python2.3/site-packages/_mysql.so: undefined symbol: mysql_rollback
Hello. Python 2.3.4 (#1, Feb 2 2005, 12:11:53) [GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)] on linux2 MySQL-python-1.2.0 I'm trying to execute this script: -- #!/usr/bin/python import MySQLdb conn = MySQLdb.connect (host = "localhost", user = "root", passwd = "", db = "test") cursor = conn.cursor () cursor.execute ("SELECT VERSION()") row = cursor.fetchone () print "server version:", row0 cursor.close () conn.close () -- But I get this error message: [ ]# python test.py Traceback (most recent call last): File "test.py", line 2, in ? import MySQLdb File "/usr/lib/python2.3/site-packages/MySQLdb/__init__.py", line 27, in ? import _mysql ImportError: /usr/lib/python2.3/site-packages/_mysql.so: undefined symbol: mysql_rollback What am I doing wrong? -- http://mail.python.org/mailman/listinfo/python-list
Re: /usr/lib/python2.3/site-packages/_mysql.so: undefined symbol: mysql_rollback
Hi Skip. Thank you very much for your answer. If LDD: [EMAIL PROTECTED] /]# ldd /usr/lib/python2.3/site-packages/_mysql.so libmysqlclient.so.10 => /usr/lib/mysql/libmysqlclient.so.10 (0xf6fb3000) libcrypt.so.1 => /lib/libcrypt.so.1 (0xf6f84000) libnsl.so.1 => /lib/libnsl.so.1 (0xf6f6e000) libm.so.6 => /lib/tls/libm.so.6 (0xf6f4b000) libz.so.1 => /usr/lib/libz.so.1 (0xf6f3b000) libpthread.so.0 => /lib/tls/libpthread.so.0 (0xf6f29000) libc.so.6 => /lib/tls/libc.so.6 (0xf6e02000) /lib/ld-linux.so.2 (0x001d8000) There is no libmysql, but I've got no idea where it is: [EMAIL PROTECTED] /]# find . -name "*libmysql.so*" (no results) In addition, on my PC (where MySQL-python works ok), there is no libmysql: -- [EMAIL PROTECTED] /]# ldd /usr/lib/python2.3/site-packages/_mysql.so linux-gate.so.1 => (0x00883000) libmysqlclient.so.10 => /usr/local/mysql/lib/mysql/libmysqlclient.so.10 (0x00cbf000) libz.so.1 => /usr/lib/libz.so.1 (0x00d52000) libcrypt.so.1 => /lib/libcrypt.so.1 (0x0092c000) libnsl.so.1 => /lib/libnsl.so.1 (0x00ed6000) libm.so.6 => /lib/tls/libm.so.6 (0x00558000) libpthread.so.0 => /lib/tls/libpthread.so.0 (0x00db3000) libc.so.6 => /lib/tls/libc.so.6 (0x0037c000) /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x00753000) [EMAIL PROTECTED] /] find . -name "*libmysql.so*" (no results) - Must I install some extra tool? Thank you very much. 2005/9/16, [EMAIL PROTECTED] <[EMAIL PROTECTED]>: > > thomas> ImportError: /usr/lib/python2.3/site-packages/_mysql.so: undefined > thomas> symbol: mysql_rollback > > skip> Is your libmysql.so installed in an odd place? > > Oh, also, try executing > > ldd /usr/lib/python2.3/site-packages/_mysql.so > > If that shows libmysql as undefined. Find out where it is and set > LD_LIBRARY_PATH to refer to it, then try again. If that succeeds, either > retain the LD_LIBRARY_PATH setting or relink _mysql.so with the -R stuff I > referred to in my first message. > > Skip > -- http://mail.python.org/mailman/listinfo/python-list