Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder4.news.weretis.net!feeder.news-service.com!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; 'wed,': 0.03; 'encoded': 0.05; 'parameter': 0.05; 'does.': 0.07; 'function,': 0.07; 'python': 0.08; "'''": 0.09; 'iterate': 0.09; 'utf-8': 0.09; 'pm,': 0.10; 'def': 0.12; 'received:209.85.214.174': 0.14; 'received:mail-iw0-f174.google.com': 0.14; 'wrote:': 0.14; 'angelico': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'subject:Unicode': 0.16; '\xa0for': 0.16; "wouldn't": 0.17; 'bytes': 0.19; 'this?': 0.19; 'work,': 0.20; 'header:In-Reply-To:1': 0.21; 'seems': 0.21; 'file,': 0.22; 'way?': 0.23; '\xa0if': 0.23; "doesn't": 0.25; '(and': 0.25; 'function': 0.25; 'string': 0.26; 'script': 0.27; "i'm": 0.27; 'message-id:@mail.gmail.com': 0.28; 'received:209.85.214': 0.28; 'unicode': 0.29; 'bit': 0.30; 'iterating': 0.30; 'hi,': 0.31; 'print': 0.31; 'seem': 0.32; 'to:addr:python-list': 0.33; 'list': 0.33; 'starting': 0.33; 'chris': 0.34; 'using': 0.35; 'received:google.com': 0.37; 'change': 0.37; 'received:209.85': 0.37; 'instead.': 0.37; 'but': 0.38; 'data': 0.38; 'subject:: ': 0.38; "i'd": 0.39; 'received:209': 0.39; 'list,': 0.39; 'to:addr:python.org': 0.39; 'really': 0.40; 'simply': 0.60; 'your': 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=57Zwa0RZrglgx7r1aHUpFGeReBFqDOhreaOkMRXaoTg=; b=rdRyxkQZt+LtOZh65joLpUScdGcmjfhGBTODaeXkI67By2IATRBDDooSHE0vmcpaF/ waEgKYq+NZ2r5L4YZEN1tQBk4Ov5FFP+NCwxb8KjN7vto9YMJQVhoKsQAeV7gcfhQeER EbJFp7gVlyZgQIPdmawi9jH+i+d84LavCkIhQ= 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=mzpdFqQc/lEVva9wd+pOPTQt9Hp84ZhUjrQVN7iiSfXSmW/SxcV7OQgziuBwoVGH3R RbsUp2aR5WWJkJdDOCRtujj2yJ9le1B2bCUZ7k7jkc3gt7Ld86zDIWcX+NV3XEyxQvll OvP97PxsvaVrOw7P1EoYn4+5Ur5EKS89uA2dY= MIME-Version: 1.0 In-Reply-To: References: Date: Wed, 22 Jun 2011 14:00:22 +1000 Subject: Re: Unicode codepoints From: Chris Angelico 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: 37 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1308715226 news.xs4all.nl 49046 [::ffff:82.94.164.166]:57397 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:8179 On Wed, Jun 22, 2011 at 1:37 PM, Saul Spatz wrote: > 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? Once you have your data as a Unicode string (and you seem to be using Python 3, so 's' will be a Unicode string), wouldn't a list of its codepoints simply be this? for c in s: print('U+'+hex(ord(c))[2:]) But if you do need the codePoints() function, I'd do it as a generator. > def codePoints(s): > =A0 =A0''' return a list of the Unicode codepoints in the string s ''' > =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 =A0yield 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 =A0yield ord(c) > =A0 =A0 =A0 =A0else: > =A0 =A0 =A0 =A0 =A0 =A0skip =3D True Your main function doesn't even have to change - it's iterating over the list, so it may as well iterate over the generator instead. But I don't really understand what codePoints() does. Is it expecting the parameter to be a string of bytes or of Unicode characters? Chris Angelico