doc:appunti:prog:python_unicode
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| doc:appunti:prog:python_unicode [2012/11/26 16:40] – niccolo | doc:appunti:prog:python_unicode [2015/07/17 09:50] (current) – [Nomi di file e directory] niccolo | ||
|---|---|---|---|
| Line 4: | Line 4: | ||
| Oppure queste slide: **[[http:// | Oppure queste slide: **[[http:// | ||
| + | |||
| + | ===== 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' | ||
| + | |||
| + | Per assicurarsi che Python decodifichi correttamente le stringhe lette da un database: | ||
| + | |||
| + | <code python> | ||
| + | import MySQLdb | ||
| + | conn = MySQLdb.connect(host=' | ||
| + | curs = conn.cursor() | ||
| + | # Not strictly needed: | ||
| + | # | ||
| + | curs.execute(" | ||
| + | rows = curs.fetchall() | ||
| + | for row in rows: | ||
| + | field0 = row[0] | ||
| + | print type(field0), | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | La funzione **'' | ||
| + | |||
| + | Se si omette la dichiarazione del **'' | ||
| + | |||
| + | <code python> | ||
| + | curs.execute(" | ||
| + | curs.execute(" | ||
| + | rows = curs.fetchall() | ||
| + | for row in rows: | ||
| + | field0 = row[0].decode(' | ||
| + | print type(field0), | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== Input/ | ||
| + | |||
| + | Con queste istruzioni si apre una connessione al database e ci si assicura che tutte le stringhe che vengono lette siano di **''< | ||
| + | |||
| + | <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 = " | ||
| + | conn.set_client_encoding(" | ||
| + | curs = conn.cursor() | ||
| + | </ | ||
| + | ===== Input/ | ||
| + | |||
| + | Se si deve comunicare con un programma esterno utilizzando UTF-8 conviene come al solito memorizzare le stringhe in unicode e quindi esplicitare l' | ||
| + | |||
| + | <code python> | ||
| + | text = u" | ||
| + | subproc = subprocess.Popen([" | ||
| + | output, stderr = subproc.communicate(input=text.encode(' | ||
| + | output = output.decode(' | ||
| + | </ | ||
| + | |||
| + | La stringa **'' | ||
| + | ===== Output: codifica implicita della print ===== | ||
| + | |||
| + | Attenzione ad alcuni comportamenti impliciti del Python, ad esempio una semplice | ||
| + | |||
| + | <code python> | ||
| + | print string | ||
| + | </ | ||
| + | |||
| + | codifica il contenuto di '' | ||
| + | |||
| + | < | ||
| + | UnicodeEncodeError: | ||
| + | </ | ||
| + | |||
| + | Per avere un comportamento univoco conviene codificare esplicitamente l' | ||
| + | |||
| + | <code python> | ||
| + | print string.encode(' | ||
| + | </ | ||
| + | |||
| + | ===== Nomi di file e directory ===== | ||
| + | |||
| + | Alcune funzioni relative al filesystem potrebbero causare problemi, ad esempio: | ||
| + | |||
| + | <code python> | ||
| + | os.path.isfile(filename) | ||
| + | os.stat(filename) | ||
| + | </ | ||
| + | |||
| + | potrebbe fallire con: | ||
| + | |||
| + | < | ||
| + | UnicodeEncodeError: | ||
| + | </ | ||
| + | |||
| + | La soluzione è codificare esplicitamente la stringa prima di passarla alla funzione: | ||
| + | |||
| + | <code python> | ||
| + | os.path.isfile(filename.encode(' | ||
| + | os.stat(filename.encode(' | ||
| + | </ | ||
| + | |||
| + | ===== Lettura file di testo ===== | ||
| + | |||
| + | Invece della semplice **'' | ||
| + | |||
| + | <code python> | ||
| + | import codecs | ||
| + | for line in codecs.open(" | ||
| + | print line.strip() | ||
| + | </ | ||
doc/appunti/prog/python_unicode.1353944453.txt.gz · Last modified: by niccolo
