做笔记.记录生活中的点滴!
Python汉字拼音首字母字典+SqlServer汉字拼音首字母
上一篇 /
下一篇 2008-02-21 10:54:31
/ 个人分类:编程技术
最近,用wxpython写一小程序,其中要生成字符串的汉字拼音首字母,在网上找了很多方法,未能找到适合Python的,索性就自己动手利用SQLServer生成一个Python字典.
打包下载地址为:Unicode汉字拼音首字母字典+Sql代码 Unicode汉字编码是从19968到19968+20901共20902个.
SQLServer生成方法如下:
第一步: 新建一个函数,功能实现字符串的汉字拼音首字母输出,代码如下
CREATE function Func_GetPY
(
@str nvarchar(4000)
)
returns nvarchar(4000)
as
begin
declare @word nchar(1),@PY nvarchar(4000)
set @PY=''
while len(@str)>0
begin
set @word=left(@str,1)
--如果非汉字字符,返回原字符
set @PY=@PY+(case when unicode(@word) between 19968 and 19968+20901
then (
select top 1 PY
from
(
select 'A' as PY,N'驁' as word
union all select 'B',N'簿'
union all select 'C',N'錯'
union all select 'D',N'鵽'
union all select 'E',N'樲'
union all select 'F',N'鰒'
union all select 'G',N'腂'
union all select 'H',N'夻'
union all select 'J',N'攈'
union all select 'K',N'穒'
union all select 'L',N'鱳'
union all select 'M',N'旀'
union all select 'N',N'桛'
union all select 'O',N'漚'
union all select 'P',N'曝'
union all select 'Q',N'囕'
union all select 'R',N'鶸'
union all select 'S',N'蜶'
union all select 'T',N'籜'
union all select 'W',N'鶩'
union all select 'X',N'鑂'
union all select 'Y',N'韻'
union all select 'Z',N'咗'
) T
where word>=@word collate Chinese_PRC_CS_AS_KS_WS
order by PY ASC
)
else @word
end)
set @str=right(@str,len(@str)-1)
end
return left(@PY,30)
end
第二步: 用上一步的函数,由如下代码块来生成字典,代码如下:
--select dbo.Func_GetPy('你好朋友')
declare @begin int,@end int
set @begin=19968
set @end= @begin+20901
print '{'
while @begin <= @end
begin
print convert(varchar(20),@begin)+':["' + dbo.Func_GetPy(nchar(@begin))+'",u"' + nchar(@begin)+'"],'
set @begin=@begin+1
end
print '}'
导入论坛
收藏
分享给好友
管理
举报
TAG: