在进行文本分析、提取关键词时,新闻评论等文本通常是中英文及其他语言的混杂,若不加处理直接分析,结果往往差强人意。
# 内存溢出 (jb51.cc) s = "China's Legend Holdings will split its several business arms to go public on stock markets,the group's presIDent Zhu linan saID on Tuesday.该集团总裁朱利安周二表示,中国联想控股将分拆其多个业务部门在股市上市。"result = "".join(i for i in s if ord(i) < 256)print(result)# End 内存溢出 jb51.cc
out:China's Legend Holdings will split its several business arms to go public on stock markets,the group's presIDent Zhu linan saID on Tuesday.
2、unicode编码识别
# 内存溢出 (jb51.cc) import res = "China's Legend Holdings will split its several business arms to go public on stock markets,the group's presIDent Zhu linan saID on Tuesday.该集团总裁朱利安周二表示,中国联想控股将分拆其多个业务部门在股市上市。"uncn = re.compile(r'[\u0061-\u007a,\u0020]')en = "".join(uncn.findall(s.lower()))print(en)# End 内存溢出 jb51.cc
out:chinas legend holdings will split its several business arms to go public on stock markets,the groups presIDent zhu linan saID on tuesday
中文的编码范围是:\u4e00-\u9fa5,相应的[^\u4e00-\u9fa5]可匹配非中文。
匹配英文时,需要将空格[\u0020]加入,不然单词之间没空格了。
总结
以上是内存溢出为你收集整理的Python 将字符串的中英文分离的完整代码全部内容,希望文章能够帮你解决Python 将字符串的中英文分离的完整代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)