User Tools

Site Tools


doc:appunti:prog:python_unicode

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
doc:appunti:prog:python_unicode [2013/01/28 10:05] – [Nomi di file e directory] niccolodoc:appunti:prog:python_unicode [2013/10/12 08:05] – [Input: lettura da database] niccolo
Line 5: Line 5:
 Oppure queste slide: **[[http://farmdev.com/talks/unicode/|Unicode In Python, Completely Demystified]]**. Oppure queste slide: **[[http://farmdev.com/talks/unicode/|Unicode In Python, Completely Demystified]]**.
  
-===== Input: lettura da database =====+===== Input: lettura da database MySQL =====
  
 Si assume che nel database i campi testo siano codificati UTF-8. Sarebbe opportuno che il charset del database sia dichiarato UTF-8 in fase di creazione dello stesso; con PostgreSQL si tratta dell'impostazione predefinita, con MySQL invece no. Con MySQL è comunque possibile memorizzare testo UTF-8 anche se il charset dichiarato è quello predefinito **''latin1''**, per fortuna perché MySQL ha ancora molti problemi nella gestione di UTF-8 (spazio occupato dalle stringhe, limiti nella dimensione degli indici, ecc.). Si assume che nel database i campi testo siano codificati UTF-8. Sarebbe opportuno che il charset del database sia dichiarato UTF-8 in fase di creazione dello stesso; con PostgreSQL si tratta dell'impostazione predefinita, con MySQL invece no. Con MySQL è comunque possibile memorizzare testo UTF-8 anche se il charset dichiarato è quello predefinito **''latin1''**, per fortuna perché MySQL ha ancora molti problemi nella gestione di UTF-8 (spazio occupato dalle stringhe, limiti nella dimensione degli indici, ecc.).
Line 39: Line 39:
 </code> </code>
  
 +===== Input/Output: da database PostgreSQL =====
 +
 +Con queste istruzioni si apre una connessione al database e ci si assicura che tutte le stringhe che vengono lette siano di **''<type 'unicode'>''**:
 +
 +<code python>
 +import psycopg2
 +import psycopg2.extensions
 +psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
 +psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)
 +
 +conn = psycopg2.connect(host = "127.0.0.1", database = "dbname", user="dbuser", password="dbpass")
 +conn.set_client_encoding("UTF8")
 +curs = conn.cursor()
 +</code>
 ===== Input/Output: lettura/scrittura da pipe ===== ===== Input/Output: lettura/scrittura da pipe =====
  
Line 77: Line 91:
 <code python> <code python>
 os.path.isfile(filename) os.path.isfile(filename)
 +os.stat(filename)
 </code> </code>
  
Line 84: Line 99:
 UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 79: ordinal not in range(128) UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 79: ordinal not in range(128)
 </code> </code>
 +
 +La soluzione è codificare esplicitamente la stringa prima di passarla alla funzione:
 +
 +<code python>
 +os.path.isfile(filename.encode('utf-8'))
 +os.stat(filename.encode('utf-8'))
 +</code>
 +
doc/appunti/prog/python_unicode.txt · Last modified: 2015/07/17 09:50 by niccolo