Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'received:209.85.212.46': 0.03; 'received:mail-vw0-f46.google.com': 0.03; 'encoded': 0.05; 'formatting': 0.07; "'''": 0.09; '__name__': 0.09; 'be:': 0.09; 'utf-8': 0.09; 'def': 0.12; "'__main__':": 0.16; '(besides': 0.16; 'hex': 0.16; 'subject:Unicode': 0.16; '\xa0for': 0.16; 'work,': 0.20; 'header:In-Reply-To:1': 0.21; 'seems': 0.21; 'file,': 0.22; 'way?': 0.23; '\xa0if': 0.23; 'url:mailman': 0.26; 'string': 0.26; 'script': 0.27; "i'm": 0.27; 'message-id:@mail.gmail.com': 0.28; 'thanks': 0.28; 'skip:p 30': 0.28; 'unicode': 0.29; 'bit': 0.30; 'url:listinfo': 0.30; 'filtering': 0.30; 'iterating': 0.30; 'hi,': 0.31; 'print': 0.31; "skip:' 10": 0.32; 'skip:\xa0 30': 0.32; 'to:addr:python-list': 0.33; 'list': 0.33; 'starting': 0.33; 'received:209.85.212': 0.34; 'characters': 0.34; '8bit%:3': 0.35; 'usual': 0.35; 'using': 0.35; '8bit%:5': 0.36; 'received:google.com': 0.37; 'received:209.85': 0.37; 'url:python': 0.38; 'url:org': 0.38; 'but': 0.38; 'subject:: ': 0.38; 'should': 0.39; 'received:209': 0.39; 'add': 0.39; 'to:addr:python.org': 0.39; 'help': 0.40; 'more': 0.60 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type:content-transfer-encoding; bh=DwU6HdayFkSiyikdN0Ct0fxe+v9UkIuiWPuJL0/9mRI=; b=AC5V0ODGF4ZagHDWqJRaXHBfz/tZ33dSN1XfEIvoQbOmKn23sRkMMN2RqG/dRyyvXb XQBZUeutEmPyKPh4sp5i9cVFEeVHxkC0Tm4b9SeMkiXWNvIJy23MXsBNP9kwaVkX6GfP xz6kMWSb2YQ6F48Q0TZP0WvgMvQ8qihzsJA48= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=vZe4tlFtzpxp0nTXzKa1OB/pQeOCThQFESWjeGZDVuGqI5IVIfGN+YViMiQ1F6gUWE MCy2sQp7uACGBZ2C4nZ0aHSPd4AD7pDBSdnskbVMncYWQzcfoYa4XdDw3M78lCJsm+Jd DEQbytFR/xd6Ckun+GVrT94yTTiY+v4+4SUiE= MIME-Version: 1.0 In-Reply-To: References: Date: Wed, 22 Jun 2011 10:42:17 +0200 Subject: Re: Unicode codepoints From: Vlastimil Brom To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 65 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1308732140 news.xs4all.nl 49178 [::ffff:82.94.164.166]:37623 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:8191 2011/6/22 Saul Spatz : > Hi, > > I'm just starting to learn a bit about Unicode. I want to be able to read= a utf-8 encoded file, and print out the codepoints it encodes. =A0After ma= ny false starts, here's a script that seems to work, but it strikes me as a= wfully awkward and unpythonic. =A0Have you a better way? > > def codePoints(s): > =A0 =A0''' return a list of the Unicode codepoints in the string s ''' > =A0 =A0answer =3D [] > =A0 =A0skip =3D False > =A0 =A0for k, c in enumerate(s): > =A0 =A0 =A0 =A0if skip: > =A0 =A0 =A0 =A0 =A0 =A0skip =3D False > =A0 =A0 =A0 =A0 =A0 =A0answer.append(ord(s[k-1:k+1])) > =A0 =A0 =A0 =A0 =A0 =A0continue > =A0 =A0 =A0 =A0if not 0xd800 <=3D ord(c) <=3D 0xdfff: > =A0 =A0 =A0 =A0 =A0 =A0answer.append(ord(c)) > =A0 =A0 =A0 =A0else: > =A0 =A0 =A0 =A0 =A0 =A0skip =3D True > =A0 =A0return answer > > if __name__ =3D=3D '__main__': > =A0 =A0s =3D open('test.txt', encoding =3D 'utf8', errors =3D 'replace').= read() > =A0 =A0code =3D codePoints(s) > =A0 =A0for c in code: > =A0 =A0 =A0 =A0print('U+'+hex(c)[2:]) > > Thanks for any help you can give me. > > Saul > > > -- > http://mail.python.org/mailman/listinfo/python-list > Hi, what functionality should codePoints(...) add over just iterating through the characters in the unicode string directly (besides filtering out the surrogates)? It seems, that you can just use s =3D open(r'C:\install\filter-utf-8.txt', encoding =3D 'utf8', errors =3D 'replace').read() for c in s: print('U+'+hex(ord(c))[2:]) or eventually add the condition before the print: if not 0xd800 <=3D ord(c) <=3D 0xdfff: you can also use string formatting to do the hex conversion and a more usual zero padding; the print(...) calls would be: "older style formatting" print("U+%04x"%(ord(c),)) or the newer, potentially more powerful way using format(...) print("U+{:04x}".format(ord(c))) hth, vbr