在线做行测的网站,怎样自己制作网站,什么是域名空间,重庆互联网公司多吗安装pypinyin module 这个模块主要处理中文拼音可以用来做很多很有趣的事情
如何使用help 函数
将help 函数的输出保存到文件中去
一眼会
import sys
import pypinyinwith open(帮助文档.txt,w) as f:sys.stdout f help(pypinyin) 如何查找模块中内置的函数
类 函数 la…安装pypinyin module 这个模块主要处理中文拼音可以用来做很多很有趣的事情
如何使用help 函数
将help 函数的输出保存到文件中去
一眼会
import sys
import pypinyinwith open(帮助文档.txt,w) as f:sys.stdout f help(pypinyin) 如何查找模块中内置的函数
类 函数 lazy_pinyin(hans, styleStyle.NORMAL: 0, errorsdefault, strictTrue, v_to_uFalse, neutral_tone_with_fiveFalse, tone_sandhiFalse) 将汉字转换为拼音返回不包含多音字结果的拼音列表load_phrases_dict(phrases_dict, styledefault) 载入用户自定义的词语拼音库load_single_dict(pinyin_dict, styledefault) 载入用户自定义的单字拼音库pinyin(hans, styleStyle.TONE: 1, heteronymFalse, errorsdefault, strictTrue, v_to_uFalse, neutral_tone_with_fiveFalse) 将汉字转换为拼音返回汉字的拼音列表slug(hans, styleStyle.NORMAL: 0, heteronymFalse, separator-, errorsdefault, strictTrue) 将汉字转换为拼音然后生成 slug 字符串 pypinyin module style example
from pypinyin import pinyinfor i in range(15):print(i, i)print(.join([i[0] for i in pinyin(啊什么你说计算机, stylei, heteronymFalse)]))i 0 ashenmenishuojisuanji i 1 ashénmenǐshuōjìsuànjī i 2 ashe2nmeni3shuo1ji4sua4nji1 i 3 shmnshjsj i 4 asmnsjsj i 5 aeneiuoiuani i 6 aéneǐuōìuànī i 7 ae2nei3uo1i4ua4ni1 i 8 ashen2meni3shuo1ji4suan4ji1 i 9 aen2ei3uo1i4uan4i1 i 10 ㄚ˙ㄕㄣˊㄇㄜ˙ㄋㄧˇㄕㄨㄛㄐㄧˋㄙㄨㄢˋㄐㄧ i 11 ㄚㄕㄇㄋㄕㄐㄙㄐ i 12 ашэнь2мэни3шуо1цзи4суань4цзи1 i 13 ашмншцсц i 14 ashenmenishochisuanchi
拼音风格问题多音字问题 有趣的应用寻找同音字
完整代码
from pypinyin import pinyindef get_jokesrt(jokestr):return .join([i[0] for i in pinyin(jokestr, style1, heteronymFalse)])def find_jokes(name, style1, pathsentences.txt, heteronymFalse, COUNT10):jokestr get_jokesrt(name)count 0with open(path, r, encodingutf-8) as file:for line in file:linestr line.strip()if jokestr in get_jokesrt(linestr):print(linestr)print(##)count 1if count COUNT:break find_jokes(睿雯)问题 没有实现字与音的一一对应 改进后的代码
from pypinyin import pinyin
import pypinyin def get_jokestr(jokestr):return pypinyin.slug(jokestr, style1, separator-) -def printf(name, linestr, separator-):print(linestr)jokestr get_jokestr(name)textstr get_jokestr(linestr)index1 textstr.find(jokestr)index2 textstr[:index1].count(separator)newstr linestr[:index2]newstr * len(name)newstr linestr[len(name) index2:]print(newstr)return [linestr, newstr]def find_jokes(name, style1, pathfile.txt, heteronymFalse, COUNT3):jokestr get_jokestr(name)count 0with open(path, r, encodinggbk) as file:for line in file:linestr line.strip()if jokestr in get_jokestr(linestr):printf(name, linestr)print(##)count 1if count COUNT:breaktest_str 睿雯
find_jokes(test_str)常用到的几个内置函数
strip() 用于去除字符串两端的指定字符默认为空格的函数find()count() 统计字符串中指定子串出现的次数lower()、upper() 将字符串转换为小写和大写islower()、isupper() 判断字符串是否全为小写或大写capitalize() 将字符串的首字母转换为大写其他字母转换为小写title() 将字符串中每个单词的首字母转换为大写isalpha()、isdigit()、isalnum() 判断字符串是否全为字母、数字、字母和数字的组合startswith(substring)、endswith(substring) 判断字符串是否以指定的子串开始或结束replace(old, new) 将字符串中的指定子串替换为新的子串split(separator) 将字符串按照指定的分隔符分割成列表index(substring) 在字符串中查找指定子串返回第一次出现的索引join(iterable) 将可迭代对象中的元素连接成一个字符串。isdigit() 判断字符串是否全为数字字符。