0

It works fine with regular characters but it doesn't work with accented characters like é,à etc... Here is the program:

def search():
 connection = sqlite3.connect('vocab.sqlite')
 cursor = connection.cursor()
 sql = "SELECT French, English value FROM Ami "
 cursor.execute(sql)
 data = cursor.fetchall()
 data=sorted(data)
 file_open=open('vraiamis.html','w')
 for i in data:
 a='<a href="'+'http://www.google.fr/#hl=fr&gs_nf=1&cp=4&gs_id=o&xhr=t&q='
 a=a+str(i[0]).encode('latin-1')+'">'+str(i[0]).encode('latin-1')+'</a>'+'<br>'
 file_open.write(a)
 file_open.close()
 webbrowser.open('vraiamis.html')

when the value in the database contains special characters like é,à,ç ( it doesn't work I get the following error message: UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 1: ordinal not in range(128)

Thanks in advance for your help

asked Feb 23, 2012 at 13:35

2 Answers 2

2

Try

a=a+i[0].encode('latin-1')+'">' + i[0].encode('latin-1')+'</a>'+'<br>'

etc - your str() calls are trying to convert the unicode to a bytestring before you've decoded it.

answered Feb 23, 2012 at 13:45
Sign up to request clarification or add additional context in comments.

Comments

0

You may write your vraiamis.html in utf-8 encoding, so that your special characters may be encoded.

def search():
 import codecs
 connection = sqlite3.connect('vocab.sqlite')
 cursor = connection.cursor()
 sql = "SELECT French, English value FROM Ami "
 cursor.execute(sql)
 data = cursor.fetchall()
 data=sorted(data) 
 file_open= codecs.open('vraiamis.html', 'w', encoding='utf-8')
 for i in data:
 a=u'<a href="' + u'http://www.google.fr/#hl=fr&gs_nf=1&cp=4&gs_id=o&xhr=t&q='
 a=a + i[0] + u'">' + i[0] + u'</a>' + u'<br>'
 file_open.write(a)
 file_open.close()
 webbrowser.open('vraiamis.html')
answered Feb 23, 2012 at 13:52

1 Comment

Sorry but the encoding doesn't work it returns an error message

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.