python获取中文拼音首字母以进行检索
最近在项目中有这个一个需求,就是要进行首字母查询,就是搜索"王菲",可以用"wf"来搜索。
搜索了一下,没有现在的python代码,倒有现成的php代码。修改一下,成了以下的代码:
def multi_get_letter(str_input): if isinstance(str_input, unicode): unicode_str = str_input else: try: unicode_str = str_input.decode('utf8') except: try: unicode_str = str_input.decode('gbk') except: print 'unknown coding' return return_list = [] for one_unicode in unicode_str: print single_get_first(one_unicode) return_list.append(single_get_first(one_unicode)) return "".join(return_list) def single_get_first(unicode1): str1 = unicode1.encode('gbk') try: ord(str1) return str1 except: asc = ord(str1[0]) * 256 + ord(str1[1]) - 65536 if asc >= -20319 and asc <= -20284: return 'a' if asc >= -20283 and asc <= -19776: return 'b' if asc >= -19775 and asc <= -19219: return 'c' if asc >= -19218 and asc <= -18711: return 'd' if asc >= -18710 and asc <= -18527: return 'e' if asc >= -18526 and asc <= -18240: return 'f' if asc >= -18239 and asc <= -17923: return 'g' if asc >= -17922 and asc <= -17418: return 'h' if asc >= -17417 and asc <= -16475: return 'j' if asc >= -16474 and asc <= -16213: return 'k' if asc >= -16212 and asc <= -15641: return 'l' if asc >= -15640 and asc <= -15166: return 'm' if asc >= -15165 and asc <= -14923: return 'n' if asc >= -14922 and asc <= -14915: return 'o' if asc >= -14914 and asc <= -14631: return 'p' if asc >= -14630 and asc <= -14150: return 'q' if asc >= -14149 and asc <= -14091: return 'r' if asc >= -14090 and asc <= -13119: return 's' if asc >= -13118 and asc <= -12839: return 't' if asc >= -12838 and asc <= -12557: return 'w' if asc >= -12556 and asc <= -11848: return 'x' if asc >= -11847 and asc <= -11056: return 'y' if asc >= -11055 and asc <= -10247: return 'z' return ''
主要的原理是GBK汉字是按拼音顺序编码的。
python获取中文拼音首字母以进行检索相关推荐
- python获取字符串首字母_[Python] Python 获取中文的首字母 和 全部拼音首字母
原博文 2019-07-08 04:18 − Python 获取中文的首字母 和 全部拼音首字母 代码如下: import pinyin def getStrAllAplha(str): return ...
- [Python] Python 获取中文的首字母 和 全部拼音首字母
Python 获取中文的首字母 和 全部拼音首字母 代码如下: import pinyindef getStrAllAplha(str):return pinyin.get_initial(str, ...
- java怎么获取中文首字母_Java如何获取中文拼音首字母的方法介绍
下面小编就为大家带来一篇java获取中文拼音首字母的实例.小编觉得挺不错的,现在就分享给大家,也给大家做个参考.一起跟随小编过来看看吧 如下所示: import net.sourceforge.pin ...
- java 拼音首字母_java获取中文拼音首字母的实例
导读热词 正文 如下所示: import net.sourceforge.pinyin4j.PinyinHelper; public class PinyinHelperUtil { /** * 得到 ...
- 自动获取中文拼音首字母方法-Asp.net
调用方法:GetFirstAlphabet("中文拼音首字母方法!"); public string GetFirstAlphabet(string str) // ...
- java 获取中文拼音首字母(缩写)
第一种: 直接上代码(有个别中文无法识别): import java.io.UnsupportedEncodingException; /*** * @author yuki_ho**/ public ...
- JS获取中文拼音首字母,并通过拼音首字母高速查找页面内的中文内容
实现效果: 图一: 图二: 此例中输入的中文字符串"万万保重",有三个字是多音字.所以alert对话框中显示的是多种读音的组合. 怎样实现? 怎样实现通过拼音首字母高速查找页面内的 ...
- JS获取中文拼音首字母,并通过拼音首字母快速查找页面内的中文内容
实现效果: 图一: 图二: 此例中输入的中文字符串"万万保重",有三个字是多音字,所以alert对话框中显示的是多种读音的组合: 如何实现? 如何实现通过拼音首字母快速查找页面内的 ...
- java获取中文拼音首字母
import net.sourceforge.pinyin4j.PinyinHelper;public class PinyinHelperUtil {/*** 得到中文首字母(中国 -> ZG ...
最新文章
- 青龙羊毛——广汽三菱(搬运)
- day1--numpy
- Maxwell软件的学习总结
- 关于紫猫插件的共享网络数据NetData系列搭建环境新版教程
- mysql 查询开始时间和结束时间是一个月的数据sql
- S3接口访问华为云OBS
- 计算机等级考试四级网络工程师真题,计算机等级考试四级网络工程师试题
- +initialize方法的调用时机
- linux recuse 挂载u盘,从U盘启动kali系统出现Grub Rescue问题
- mysql如何使用多核cpu_利用多核 CPU 实现并行计算
- 爱,不留——陈光标裸捐之后
- Python量化交易|pd.expanding() VS pd.rolling() 时间窗口函数区别图解
- Android Crash:Bad notification for startForeground
- mysql 中间件 atlas_MySQL中间件-Atlas
- 工作流引擎添新丁:Flowable6.0发布
- Tita OKR实施攻略
- Java正则表达式校验邮箱和手机号
- 使用Docker搭建大数据Hadoop环境
- 杭电2534-Score
- ping 命令:指定源IP