python版权属于谁_Python正则表达式和版权符号

您可以将文本转换为unicode并使用unicode正则表达式模式.

这些是你提到的三个符号:

In [213]: print(u'\N{COPYRIGHT SIGN} \N{TRADE MARK SIGN} \N{REGISTERED SIGN}')

? ? ?

这是一些utf-8编码字符串:

In [223]: content = u'\N{TRADE MARK SIGN}'.encode('utf-8')

在这里我们将其转换为unicode:

In [224]: text = content.decode('utf-8')

这是正则表达式搜索三个符号中的任何一个:

In [225]: re.search(u'(\N{COPYRIGHT SIGN}|\N{TRADE MARK SIGN}|\N{REGISTERED SIGN})', text)

Out[225]: <_sre.sre_match at>

每个unicode字符都有webpages which catalog.但是有数十万个指定的unicode代码点,因此通过手动浏览搜索字符是不可行的.

所以,我编写了下面的程序,按名称搜索unicodes.

import sys

import unicodedata as ud

import re

import argparse

import functools

__usage__ = '''\

unicode_lookup.py -u '\d' # Shows all unicode symbols that regex match '\d'

unicode_lookup.py number # Shows all unicode symbols whose name regex matches 'number'

'''

def lookup(name_pat=None, from_num=0, to_num=0x10ffff, unicode_pattern=None,

category_pattern=None, ignore_unnamed=True,

combining=False):

fmt = u"{symbol} {num} {cat} {bi} {w} {comb} {mir} '{name}'"

print(fmt.format(

symbol='Symbol', num='Num', name='NAME',

cat='Category', bi='Bidirectional', w='Width',

comb='Combining', mir='Mirrored'))

for num in range(from_num, to_num + 1):

s = unichr(num)

if unicode_pattern and not unicode_pattern.match(s):

continue

category = ud.category(s)

if category_pattern and not category_pattern.match(category):

continue

try:

name = ud.name(s)

if name_pat and not name_pat.search(name):

continue

except ValueError:

if ignore_unnamed:

continue

else:

name = '?'

bidirectional = ud.bidirectional(s)

combining_class = ud.combining(s)

if combining and not combining_class:

continue

mirrored = ud.mirrored(s)

width = ud.east_asian_width(s)

data = dict(num=num, symbol=s, name=name,

cat=category, bi=bidirectional, w=width,

comb=combining_class, mir=mirrored)

print(fmt.format(**data).encode('utf-8'))

def parse_options():

parser = argparse.ArgumentParser(

epilog=__usage__,

formatter_class=argparse.RawDescriptionHelpFormatter)

parser.add_argument('name_pat',

type=functools.partial(re.compile, flags=re.IGNORECASE))

parser.add_argument('-f', '--from_num', default=0, type=int)

parser.add_argument('-t', '--to_num', default=0x10ffff, type=int)

parser.add_argument('-u', '--unicode_pattern',

type=functools.partial(re.compile, flags=re.UNICODE))

parser.add_argument('--category_pattern', type=re.compile)

parser.add_argument('--show_unnamed', action='store_true')

parser.add_argument('--combining', action='store_true')

return parser.parse_args()

if __name__ == '__main__':

opt = parse_options()

lookup(name_pat=opt.name_pat, from_num=opt.from_num, to_num=opt.to_num,

unicode_pattern=opt.unicode_pattern,

category_pattern=opt.category_pattern,

ignore_unnamed=not opt.show_unnamed,

combining=opt.combining)

运行

% unicode_lookup.py "copyright|trade|registered"

产量

Symbol Num Category Bidirectional Width Combining Mirrored 'NAME'

? 169 So ON N 0 0 'COPYRIGHT SIGN'

? 174 So ON A 0 0 'REGISTERED SIGN'

? 8471 So ON N 0 0 'SOUND RECORDING COPYRIGHT'

? 8482 So ON A 0 0 'TRADE MARK SIGN'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值