<html><head></head><body bgcolor="#FFFFFF"><div>Pokud jste do programu napsal</div><div><br></div><blockquote type="cite"><div><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><font class="Apple-style-span" color="#000000">s='Ă˝ á Ă Ă.'<br></font></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><font class="Apple-style-span" color="#000000">u = s.decode("utf8")</font></blockquote></blockquote></blockquote></div></blockquote><div><br></div>Pak musíte ten zdrojový kód uložit v kódování, pro které to bude fungovat a toto kódování deklarovat v hlavičce (-*- coding: cp1250). Možná bude lepší pracovat s hexadecimalnim zápisem těchto znaků, abychom se podobným problémům vyhnuli. Bohuzel teď nejsem u počítače, pak odpovím i s příkladem.<div><br>Petr Messner</div><div><br>2. 5. 2012 v 11:06, "Jaroslav Lukesh" <<a href="mailto:lukesh@seznam.cz">lukesh@seznam.cz</a>>:<br><br></div><div></div><blockquote type="cite"><div><span>Jenže právě toto nějak nejde, řve na tom "ř":</span><br><span></span><br><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><span>s='Ă˝ á Ă Ă.'</span><br></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><span>u = s.decode("utf8")</span><br></blockquote></blockquote></blockquote><span>Traceback (most recent call last):</span><br><span> File "<stdin>", line 1, in ?</span><br><span> File "/var/zope/python/lib/python2.4/encodings/utf_8.py", line 16, in decode</span><br><span> return codecs.utf_8_decode(input, errors, True)</span><br><span>UnicodeDecodeError: 'utf8' codec can't decode bytes in position 9-10: invalid data</span><br><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote></blockquote><span></span><br><span>Tohle je převod "znaků":</span><br><span>ě š č ř ž ý á í é ď ť ň ó ú ů ĺ ľ ŕ ö ë ä ü</span><br><span></span><br><span>Ä› š ÄŤ Ĺ™ Ĺľ Ă˝ á Ă Ă© ÄŹ ĹĄ Ĺ? Ăł Ăş ĹŻ Äş Äľ Ĺ• ö Ă« ä ĂĽ</span><br><span></span><br><span>Ě Š Č Ř Ž Ý Á Í É Ď Ť Ň Ó Ú Ů Ĺ Ľ Ŕ Ö Ë Ä Ü</span><br><span></span><br><span>Äš Ĺ ÄŚ Ĺ? Ĺ˝ Ăť Ă? ĂŤ É ÄŽ Ť Ň Ă“ Ăš Ĺ® Äą Ä˝ Ĺ” Ă– Ă‹ Ă„ Ăś</span><br><span></span><br><span>vypadá to teda asi na mohutný replace, co?</span><br><span></span><br><span></span><br><span>----- Původní zpráva ----- Od: "Petr Messner" <<a href="mailto:petr.messner@gmail.com">petr.messner@gmail.com</a>></span><br><span></span><br><span>pokud v proměnné s máte 'Ă˝ á Ă Ă.', pak zavoláním u = s.decode("utf8") z toho dostanete unicode řetězec. S ním pak můžete dále pracovat nebo ho převést do jiného kódování. Pokud ho chcete zobrazit na Windows v kódování cp1250, zkuste u.encode("cp1250").</span><br><span></span><br><span>Je nutné si uvědomit, ze unicode řetězec nejde přímo zobrazit - vždy to bude jen nejaká jeho podoba v některém kódování. Třeba print v 2.x automaticky použije kódování např. z LANG. Je dobré v programu vždy pracovat s unicode, mit správně nastavené vstupy a výstupy tak, aby unicode správně kódovaly a dekódovaly, a pak se o nějaké kódování už vůbec nestarat. Samozřejmě pokud máte nejaky vstup, u kterého jste si jistý, že vám bude dodávat řetězce v utf-8, první (a jediné), co uděláte, je decode("utf-8") :)</span><br><span></span><br><span>Petr Messner</span><br><span></span><br><span>2. 5. 2012 v 10:16, "Jaroslav Lukesh" <<a href="mailto:lukesh@seznam.cz">lukesh@seznam.cz</a>>:</span><br><span></span><br><blockquote type="cite"><span>Děkuji, ale nechápu, jak proměnné říct, že už obsahuje text v kódování UTF8.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Mám proměnnou, která obsahuje 'Ă˝ á Ă Ă.' tedy přesněji, toto je ve windowsech vidět, když si zobrazíte utf8 v cp1250. Python mám ale na Linuxu v LANG=cs_CZ.iso8859-2. Ty znaky v UTF8 tam jdou binárně zvenčí a nevím jak pythonu říct, že to má považovat za utf8.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Chtěl bych to udělat systémově, než na ten vstup poštvat 50x replace.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Děkuji, JL.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>----- Původní zpráva ----- Od: "Petr Přikryl" <<a href="mailto:prikryl@atlas.cz">prikryl@atlas.cz</a>></span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Nejjednodušší je to převést nejdříve do Unicode a druhým krokem do požadovaného</span><br></blockquote><blockquote type="cite"><span>kódování. Tohle jsem dělal pod Windows na konzoli, která používá cp852:</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><span>u = u"ě š č ř"</span><br></blockquote></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><span>u</span><br></blockquote></blockquote></blockquote></blockquote><blockquote type="cite"><span>u'\u011b \u0161 \u010d \u0159'</span><br></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><span>s = u.encode('utf-8')</span><br></blockquote></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><span>s</span><br></blockquote></blockquote></blockquote></blockquote><blockquote type="cite"><span>'\xc4\x9b \xc5\xa1 \xc4\x8d \xc5\x99'</span><br></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><span>u2 = s.decode('utf-8')</span><br></blockquote></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><span>u2</span><br></blockquote></blockquote></blockquote></blockquote><blockquote type="cite"><span>u'\u011b \u0161 \u010d \u0159'</span><br></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><span>u3 = unicode(s, 'utf-8')</span><br></blockquote></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><span>u3</span><br></blockquote></blockquote></blockquote></blockquote><blockquote type="cite"><span>u'\u011b \u0161 \u010d \u0159'</span><br></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><span>sLatin2 = u.encode('iso8859_2')</span><br></blockquote></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><span>sLatin2</span><br></blockquote></blockquote></blockquote></blockquote><blockquote type="cite"><span>'\xec \xb9 \xe8 \xf8'</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>.decode() nebo unicode() to převede na Unicode řetězec.</span><br></blockquote><blockquote type="cite"><span>.encode() to převede na požadované kódování.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>V názvu kódování má být oficiálně znak podtržení, ale</span><br></blockquote><blockquote type="cite"><span>implementace toleruje i uvedení velkých písmen a pomlček.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Měj se,</span><br></blockquote><blockquote type="cite"><span> Petr</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>______________________________________________________________</span><br></blockquote><blockquote type="cite"><blockquote type="cite"><span>Od: "Jaroslav Lukesh" <<a href="mailto:lukesh@seznam.cz">lukesh@seznam.cz</a>></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>Komu: Konference PyCZ <<a href="mailto:python@py.cz">python@py.cz</a>></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>Datum: 27.04.2012 10:35</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>Předmět: [python] Unicode/ne-unicode problem</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>Dobrý den,</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>potřeboval bych dostat z binárních dat utf8 na vstupu normální neunicodový</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>výstup, ale nějak mi to nejde.</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>Vstup: znaky "ě š č ř" v utf8 již v binární formě, systém je v iso8859-2</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>Čekám že z toho nějak dostanu "ě š č ř" ale pořád nic. Jak mu říct, že 'Ă˝</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>á Ă Ă.' je už v utf8?</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>Děkuji, JL.</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><span>a='Ă˝ á Ă Ă.'</span><br></blockquote></blockquote></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><span>A=unicode(a,'iso8859-2')</span><br></blockquote></blockquote></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><span>print a</span><br></blockquote></blockquote></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>Ă˝ á Ă Ă.</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><span>A=unicode(a,'utf8')</span><br></blockquote></blockquote></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>Traceback (most recent call last):</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>File "<stdin>", line 1, in ?</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>File "/var/zope/python/lib/python2.4/encodings/utf_8.py", line 16, in</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>decode</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span> return codecs.utf_8_decode(input, errors, True)</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>UnicodeDecodeError: 'utf8' codec can't decode bytes in position 9-10:</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>invalid data</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><span>A.encode('iso8859-2')</span><br></blockquote></blockquote></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>'\xc3\xbd \xc3\xb7 \xc3\xad \xc3.'</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><span>a.encode('iso8859-2')</span><br></blockquote></blockquote></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>Traceback (most recent call last):</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>File "<stdin>", line 1, in ?</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>File "/var/zope/python/lib/python2.4/encodings/iso8859_2.py", line 18, in</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>encode</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span> return codecs.charmap_encode(input,errors,encoding_map)</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0:</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>ordinal not in range(128)</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><span>A.decode('iso8859-2')</span><br></blockquote></blockquote></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>Traceback (most recent call last):</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>File "<stdin>", line 1, in ?</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>File "/var/zope/python/lib/python2.4/encodings/iso8859_2.py", line 22, in</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>decode</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span> return codecs.charmap_decode(input,errors,decoding_map)</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1:</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>ordinal not in range(128)</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><span>a.decode('iso8859-2')</span><br></blockquote></blockquote></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>u'\u0102\u02dd \u0102\u02c7 \u0102\xad \u0102.'</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><span>a.decode('utf8')</span><br></blockquote></blockquote></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>Traceback (most recent call last):</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>File "<stdin>", line 1, in ?</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>File "/var/zope/python/lib/python2.4/encodings/utf_8.py", line 16, in</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>decode</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span> return codecs.utf_8_decode(input, errors, True)</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>UnicodeDecodeError: 'utf8' codec can't decode bytes in position 9-10:</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>invalid data</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote></blockquote></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>_______________________________________________</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span>Python mailing list</span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span><a href="mailto:Python@py.cz">Python@py.cz</a></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span><a href="http://www.py.cz/mailman/listinfo/python">http://www.py.cz/mailman/listinfo/python</a></span><br></blockquote></blockquote><blockquote type="cite"><blockquote type="cite"><span></span><br></blockquote></blockquote><blockquote type="cite"><span>_______________________________________________</span><br></blockquote><blockquote type="cite"><span>Python mailing list</span><br></blockquote><blockquote type="cite"><span><a href="mailto:Python@py.cz">Python@py.cz</a></span><br></blockquote><blockquote type="cite"><span><a href="http://www.py.cz/mailman/listinfo/python">http://www.py.cz/mailman/listinfo/python</a></span><br></blockquote><blockquote type="cite"><span>_______________________________________________</span><br></blockquote><blockquote type="cite"><span>Python mailing list</span><br></blockquote><blockquote type="cite"><span><a href="mailto:Python@py.cz">Python@py.cz</a></span><br></blockquote><blockquote type="cite"><span><a href="http://www.py.cz/mailman/listinfo/python">http://www.py.cz/mailman/listinfo/python</a></span><br></blockquote><span>_______________________________________________</span><br><span>Python mailing list</span><br><span><a href="mailto:Python@py.cz">Python@py.cz</a></span><br><span><a href="http://www.py.cz/mailman/listinfo/python">http://www.py.cz/mailman/listinfo/python</a> </span><br><span>_______________________________________________</span><br><span>Python mailing list</span><br><span><a href="mailto:Python@py.cz">Python@py.cz</a></span><br><span><a href="http://www.py.cz/mailman/listinfo/python">http://www.py.cz/mailman/listinfo/python</a></span></div></blockquote></body></html>