example-code-2e/04-text-byte/numerics_demo.py

19 lines
624 B
Python
Raw Normal View History

2020-01-23 02:52:23 +01:00
# tag::NUMERICS_DEMO[]
2014-10-14 19:26:55 +02:00
import unicodedata
import re
re_digit = re.compile(r'\d')
sample = '1\xbc\xb2\u0969\u136b\u216b\u2466\u2480\u3285'
for char in sample:
print(f'U+{ord(char):04x}', # <1>
2014-10-14 19:26:55 +02:00
char.center(6), # <2>
're_dig' if re_digit.match(char) else '-', # <3>
'isdig' if char.isdigit() else '-', # <4>
'isnum' if char.isnumeric() else '-', # <5>
f'{unicodedata.numeric(char):5.2f}', # <6>
2014-10-14 19:26:55 +02:00
unicodedata.name(char), # <7>
sep='\t')
2020-01-23 02:52:23 +01:00
# end::NUMERICS_DEMO[]