python正则的中文处理
因工作需要,要查找中文汉字分词,因为python正则表达式\W+表示的是所有的中文字就连标点符号都包括。所以要想办法过滤掉。
参考博客:http://log.medcl.net/item/2011/03/the-chinese-deal-is-the-python/
1.匹配中文时,正则表达式规则和目标字串的编码格式必须相同
print sys.getdefaultencoding()
text =u"#who#helloworld#a中文x#"
print isinstance(text,unicode)
print text
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 18: ordinal not in range(128)
print text报错
解释:控制台信息输出窗口是按照ascii编码输出的(英文系统的默认编码是ascii),而上面代码中的字符串是Unicode编码的,所以输出时产生了错误。
改成 print(word.encode('utf8'))即可
2.//确定系统默认编码
import sys
print sys.getdefaultencoding()
3.//判断字符类型是否unicode
print isinstance(text,unicode)
4.unicode\python字符互转
# -*- coding: utf-8 -*-
unistr= u'a';
pystr=unistr.encode('utf8')
unistr2=unicode(pystr,'utf8')
#需要unicode的环境
if not isinstance(input,unicode):
temp=unicode(input,'utf8')
else:
temp=input #需要pythonstr的环境
if isinstance(input,unicode):
temp2=input.encode('utf8')
else:
temp2=input 经实验如果脚本的# -*- coding: utf-8 -*- 设置为GBK用unicode转换的时候会报错。
python正则的中文处理的更多相关文章
- python正则的中文处理(转)
匹配中文时,正则表达式规则和目标字串的编码格式必须相同 print sys.getdefaultencoding() text =u"#who#helloworld#a中文x#" ...
- python正则匹配——中文字符的匹配
# -*- coding:utf-8 -*- import re '''python 3.5版本 正则匹配中文,固定形式:\u4E00-\u9FA5 ''' words = 'study in 山海大 ...
- python 正则匹配中文(unicode)(转)
由于 需求原因,需要匹配 提取中文,大量google下,并没有我需要的.花了一个小时大概测试,此utf8中文通过,特留文. 参考: http://hi.baidu.com/nivrrex/blo ...
- 2019-02-18 扩展Python控制台实现中文反馈信息之二-正则替换
"中文编程"知乎专栏原文地址 续前文扩展Python控制台实现中文反馈信息, 实现了如下效果: >>> 学 Traceback (most recent call ...
- Python正则式的基本用法
Python正则式的基本用法 1.1基本规则 1.2重复 1.2.1最小匹配与精确匹配 1.3前向界定与后向界定 1.4组的基本知识 2.re模块的基本函数 2.1使用compile加速 2.2 ma ...
- python 正则,常用正则表达式大全
Nginx访问日志匹配 re.compile #re.compile 规则解释,改规则必须从前面开始匹配一个一个写到后面,前面一个修改后面全部错误.特殊标准结束为符号为空或者双引号: 改符号开始 从 ...
- Python2.7 转义和正则匹配中文
今天爬虫(新浪微博 个人信息页面)的时候遇到了转义和正则匹配中文出乱码的问题. 先给出要匹配的部分网页源代码如下: <span class=\"pt_title S_txt2\&quo ...
- python正则中如何匹配汉字以及encode(‘utf-8’)和decode(‘utf-8’)的互转
正则表达式: [\u2E80-\u9FFF]+$ 匹配所有东亚区的语言 [\u4E00-\u9FFF]+$ 匹配简体和繁体 [\u4E00-\u9FA5]+$ 匹配简体 <input ty ...
- Python只读取文本中文字符
#coding=utf-8 import re with open('aaa.txt','r',encoding="utf-8") as f: #data = f.read().d ...
随机推荐
- [Effective C++ --021]必须返回对象时,别妄想返回其reference
引言 在条目20中,我们知道了值传递和引用传递的效率问题,因此在设计程序时,我们可能就尽可能来返回引用而不是值. 可是,可能会犯下面的一些错误:传递一些引用指向其实并不存在的对象. 第一节:返回临时变 ...
- Dashboards (Android)
his page provides information about the relative number of devices that share a certain characterist ...
- Memcached source code analysis -- Analysis of change of state--reference
This article mainly introduces the process of Memcached, libevent structure of the main thread and w ...
- Events
Events The idea behind Events is the ability to send data, as parameters, to interested Listeners an ...
- 可扩展的listview--Expandablelistview
layout.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x ...
- 通过代码设置button中文字的对齐方式
// button.titleLabel.textAlignment = NSTextAlignmentLeft; 这句无效 button.contentHorizontalAlignment = U ...
- GitHub帮助文档翻译2——contribution
工欲善其事必先利其器 ,都不知道 GitHub到底是什么,还怎么玩?因为总是会读了第一句就忘了下一句,形成不了感觉,所以希望把读GitHub的帮助文档都翻译出来,总是看大段大段的东西,谁都会懵圈的.希 ...
- StringBuilder 用法和div获取
StringBuilder strHtml = new StringBuilder(); strHtml.Append("<tr>"); strHtml.Append( ...
- [转]win7 64位下android开发环境的搭建
本文转自:http://www.cfanz.cn/index.php?c=article&a=read&id=65289 最近换了新电脑,装了win7 64位系统,安装了各种开发环境, ...
- mongodb用mongoose取到的对象不能增加属性
先定义了一个article的schema var mongoose = require('mongoose'); var Schema = mongoose.Schema; exports.schem ...