Bonjour à tous,
je rencontre un problème que je ne parviens pas à résoudre sous webpy:
Un problème d'encodage avec les accents français lorsque j'utilise db.query().
Il semble qu'il y ait un problème avec le fichier db.py on line 124 :
/usr/lib/python2.5/site-packages/web/db.py", line 124, in __init__ self.items = [str(items)]
UnicodeEncodeError: 'ascii' codec can't encode characters in position 56-57: ordinal not in range(128)
J'ai essayé différentes solutions, y compris celle de Max que j'ai trouvé ici :
http://www.mail-archive.com/webpy@googlegroups.com/msg04618.html
Je joins à ce post un bout de code qui illustre bien cette erreur.
J'ai vraiment besoin d'une aide car je suis complêtement bloqué !
Tout d'abord, voici fichier qui vous aidera à crééer une simple table MySQL 'charsetproblem.mysql':
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 # Usage: mysql -u mylogin -p mybase < charsetproblem.mysql # # Open : mysql -u mylogin -p mybase # # mysql> SHOW TABLES; # # mysql> select * from articles; # # mysql> DROP TABLE IF EXISTS articles; # # # # --- create table ---# CREATE TABLE articles ( id int(10) NOT NULL auto_increment, titre char(80) DEFAULT "" NOT NULL, article text(4096) DEFAULT "" NOT NULL, PRIMARY KEY (id) )DEFAULT CHARACTER SET utf8; # Note: i tried with and without DEFAULT CHARACTER SET utf8;# # --- insert datas ---# INSERT INTO articles VALUES (NULL, "simple link without french characters", "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); INSERT INTO articles VALUES (NULL, "simple link with frènch chàracters", "Lorém ipsum dôlor sit amet, çonsèctetur àdipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.");
Ensuite voici un bout de code 'code.py' qui vous permettra de lancer le serveur webpy:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 #!/usr/bin/env python #-*- coding: iso-8859-1 -*- # Note: See charsetproblem.mysql to install table 'article'. # Change your settings for user, pw and db (web.database lowest). # Create a directory named 'templates" in the same directory as this # script. # import web from web import form web.config.debug = False db = web.database(dbn='mysql', user='myname', pw='mypassword', db='mydb') render = web.template.render('templates/') urls = ( '/(.*)', 'index' ) app = web.application(urls, globals()) class index: """ """ def __init__(self): """ """ def GET(self, name = "index"): """ shows the home page """ # I try with those charsets without success web.header("Content-Type", "text/html; charset= utf-8") #web.header("Content-Type", "text/html; charset= iso-8859-1") links = [] article =[] # --- LINKS - # All titles become links result_titre_db = db.query('select titre from articles;') for i in result_titre_db: links.append(i.titre) # Curiously, if I try this one, link's charset is correct... #links.append(i.titre.encode('raw_unicode_escape')) # --- ARTICLES - # It is the error which shows the 'self.items' error in db.py, # even if I change charset in web.header above: # File "/usr/lib/python2.5/site-packages/web/db.py", line 124, in __init__ # self.items = [str(items)] # UnicodeEncodeError: 'ascii' codec can't encode characters in position 56-57: ordinal not in range(128) # query = 'select article from articles where articles.titre = "%s";' % name # Other tests: # No error, but bad charset even if I change charset in web.header above. query = 'select article from articles where articles.titre = "%s";' % name.encode("utf-8") # query = 'select article from articles where articles.titre = "%s";' % name.encode('raw_unicode_escape') # query = 'select article from articles where articles.titre = "%s";' % name.encode('iso-8859-15') result_article_db = db.query(query) for i in result_article_db: article.append(i.article) if article == []: return web.notfound() return render.index(name, links, article) if __name__ == "__main__": app.run()
Il suffit de placer ce code dans le repertoire web de webpy et le lancer le server :
Merci à vous pour le coup de main, parce que là, je suis bloqué.
Code : Sélectionner tout - Visualiser dans une fenêtre à part python code.py
boubou.
Partager