因工作需要,要查找中文汉字分词,因为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正则的中文处理的更多相关文章

  1. python正则的中文处理(转)

    匹配中文时,正则表达式规则和目标字串的编码格式必须相同 print sys.getdefaultencoding() text =u"#who#helloworld#a中文x#" ...

  2. python正则匹配——中文字符的匹配

    # -*- coding:utf-8 -*- import re '''python 3.5版本 正则匹配中文,固定形式:\u4E00-\u9FA5 ''' words = 'study in 山海大 ...

  3. python 正则匹配中文(unicode)(转)

    由于 需求原因,需要匹配 提取中文,大量google下,并没有我需要的.花了一个小时大概测试,此utf8中文通过,特留文.    参考: http://hi.baidu.com/nivrrex/blo ...

  4. 2019-02-18 扩展Python控制台实现中文反馈信息之二-正则替换

    "中文编程"知乎专栏原文地址 续前文扩展Python控制台实现中文反馈信息, 实现了如下效果: >>> 学 Traceback (most recent call ...

  5. Python正则式的基本用法

    Python正则式的基本用法 1.1基本规则 1.2重复 1.2.1最小匹配与精确匹配 1.3前向界定与后向界定 1.4组的基本知识 2.re模块的基本函数 2.1使用compile加速 2.2 ma ...

  6. python 正则,常用正则表达式大全

    Nginx访问日志匹配 re.compile #re.compile 规则解释,改规则必须从前面开始匹配一个一个写到后面,前面一个修改后面全部错误.特殊标准结束为符号为空或者双引号:  改符号开始 从 ...

  7. Python2.7 转义和正则匹配中文

    今天爬虫(新浪微博 个人信息页面)的时候遇到了转义和正则匹配中文出乱码的问题. 先给出要匹配的部分网页源代码如下: <span class=\"pt_title S_txt2\&quo ...

  8. python正则中如何匹配汉字以及encode(‘utf-8’)和decode(‘utf-8’)的互转

    正则表达式: [\u2E80-\u9FFF]+$ 匹配所有东亚区的语言  [\u4E00-\u9FFF]+$ 匹配简体和繁体  [\u4E00-\u9FA5]+$ 匹配简体  <input ty ...

  9. Python只读取文本中文字符

    #coding=utf-8 import re with open('aaa.txt','r',encoding="utf-8") as f: #data = f.read().d ...

随机推荐

  1. zend studio快捷模板 开发工具之zend studio一些配置

    以下是以Zend Studio 10.0.0版本为基础的: 模板的配置(template): [菜单]->[Window]->[preferences]->[PHP]->[Ed ...

  2. Memcached source code analysis (threading model)--reference

    Look under the start memcahced threading process memcached multi-threaded mainly by instantiating mu ...

  3. 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 ...

  4. docker no permmition problem

    resolved by: sudo docker run --privileged ....

  5. Lodash Filter

    var persons = [{name:'1',age:'20'}, {name:'2', age:'25'}];_.filter(persons, {'age': '25'}); //return ...

  6. NetBeans自定义代码折叠块,类似vs中的#region

    //<editor-fold defaultstate="collapsed" desc="测试代码折叠"> echo '<script ty ...

  7. 使用Javascript获得网页中通过GET方法提交的参数

    下面我将写出一个函数,用来获取GET方法提交的参数 function getParameter(parameterName) { var string = window.location.search ...

  8. redhat6.5 配置使用centos的yum源

    新安装了redhat6.5安装后,登录系统,使用yum update 更新系统.提示: This system is not registered to Red Hat Subscription Ma ...

  9. oracle 表空间常用语句

    –查询表空间使用情况 SELECT UPPER(F.TABLESPACE_NAME) "表空间名", D.TOT_GROOTTE_MB "表空间大小(M)", ...

  10. ios--socket

    一.打开服务器 a.在终端打开,到服务器文件路径输入命令 python chatserver.py b.当显示 Iphone Chat server started 表示成功 二.建立连接 a.设置对 ...