Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #8191
| 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 | <vlastimil.brom@gmail.com> |
| 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 | <ae8fd9c1-88af-41ef-abb5-3a1883634d0e@glegroupsg2000goo.googlegroups.com> |
| References | <ae8fd9c1-88af-41ef-abb5-3a1883634d0e@glegroupsg2000goo.googlegroups.com> |
| Date | Wed, 22 Jun 2011 10:42:17 +0200 |
| Subject | Re: Unicode codepoints |
| From | Vlastimil Brom <vlastimil.brom@gmail.com> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.275.1308732140.1164.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
2011/6/22 Saul Spatz <saul.spatz@gmail.com>:
> 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. After many false starts, here's a script that seems to work, but it strikes me as awfully awkward and unpythonic. Have you a better way?
>
> def codePoints(s):
> ''' return a list of the Unicode codepoints in the string s '''
> answer = []
> skip = False
> for k, c in enumerate(s):
> if skip:
> skip = False
> answer.append(ord(s[k-1:k+1]))
> continue
> if not 0xd800 <= ord(c) <= 0xdfff:
> answer.append(ord(c))
> else:
> skip = True
> return answer
>
> if __name__ == '__main__':
> s = open('test.txt', encoding = 'utf8', errors = 'replace').read()
> code = codePoints(s)
> for c in code:
> print('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 = open(r'C:\install\filter-utf-8.txt', encoding = 'utf8', errors
= 'replace').read()
for c in s:
print('U+'+hex(ord(c))[2:])
or eventually add the condition before the print:
if not 0xd800 <= ord(c) <= 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Unicode codepoints Saul Spatz <saul.spatz@gmail.com> - 2011-06-21 20:37 -0700
Re: Unicode codepoints Chris Angelico <rosuav@gmail.com> - 2011-06-22 14:00 +1000
Re: Unicode codepoints Vlastimil Brom <vlastimil.brom@gmail.com> - 2011-06-22 10:42 +0200
Re: Unicode codepoints Peter Otten <__peter__@web.de> - 2011-06-22 11:00 +0200
Re: Unicode codepoints jmfauth <wxjmfauth@gmail.com> - 2011-06-22 03:00 -0700
csiph-web