V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
commoccoom
V2EX  ›  Python

用正则表达式怎么处理中文?

  •  
  •   commoccoom · 2015-05-07 13:36:17 +08:00 · 4692 次点击
    这是一个创建于 3269 天前的主题,其中的信息可能已经有所发展或是发生改变。
    用Python 爬了一个网页下来,怎么处理网页里的中文字符啊。

    中文都是\xe7\xa9\xbf\xe5\xb1\xb1\xe7\x94\xb2\xe5\x88\xb0\xe5\xba\x95\xe8\xaf\xb4\xe4\xba\x86\xe4\xbb\x80\xe4\xb9\x88\xef\xbc\x9f


    看不懂!

    或者有什么相关的资料书籍。谢谢!
    18 条回复    2015-05-10 12:06:15 +08:00
    Cee
        1
    Cee  
       2015-05-07 13:44:39 +08:00
    三個 \x** 一組 轉換成漢字
    fzinfz
        2
    fzinfz  
       2015-05-07 13:59:05 +08:00
    In [30]: s = b'\xe7\xa9\xbf\xe5\xb1\xb1\xe7\x94\xb2\xe5\x88\xb0'

    In [31]: sd = s.decode('utf-8')

    In [32]: sd
    Out[32]: '穿山甲到'

    In [33]: rs = re.search('山',sd)

    In [34]: rs
    Out[34]: <_sre.SRE_Match object; span=(1, 2), match='山'>
    huanglk
        3
    huanglk  
       2015-05-07 14:06:25 +08:00
    re.compile(r'^([\x80-\xff]{3})+$')
    liuhaotian
        4
    liuhaotian  
       2015-05-07 14:07:17 +08:00
    穿山甲到底说了什么?
    这个是汉字的 utf-8 编码
    我只会 php 就用 php,用 php 做了一个快速转换的 sample : http://cloud.vexio.net/prog/za/utf8_decode.php
    核心部分就是:urldecode(str_replace('\x','%',$input));
    likuku
        5
    likuku  
       2015-05-07 15:14:55 +08:00
    试试:

    str.encode('utf-8')

    正则没用过,但 count 和 find 都可。
    py 头部加
    # encoding: utf-8

    str.encode('utf-8').count(u'中文')
    str.encode('utf-8').find(u'中文')

    python 2.7 下。
    likuku
        6
    likuku  
       2015-05-07 15:17:00 +08:00
    python 2.x 使用引号字串前加 u 来明确使用 utf-8 编码,否则中文不行。
    buginux
        7
    buginux  
       2015-05-07 18:41:12 +08:00
    书籍的话,推荐楼主去看看《正则指引》, 余晟老师写的,是国内的正则书,里面有关于中文的。
    imn1
        8
    imn1  
       2015-05-07 19:46:40 +08:00
    先判断类型
    如果是string,就按latin-1转为bytes,decode utf-8
    如果本身就是bytes,直接decode就行了
    imn1
        9
    imn1  
       2015-05-07 20:03:25 +08:00
    >>> s='\xe7\xa9\xbf\xe5\xb1\xb1\xe7\x94\xb2\xe5\x88\xb0\xe5\xba\x95\xe8\xaf\xb4\xe4\xba\x86\xe4\xbb\x80\xe4\xb9\x88\xef\xbc\x9f'
    >>> s.encode('latin-1').decode('utf-8')
    '穿山甲到底说了什么?'
    >>>
    >>> s=b'\xe7\xa9\xbf\xe5\xb1\xb1\xe7\x94\xb2\xe5\x88\xb0\xe5\xba\x95\xe8\xaf\xb4\xe4\xba\x86\xe4\xbb\x80\xe4\xb9\x88\xef\xbc\x9f'
    >>> s.decode('utf-8')
    '穿山甲到底说了什么?'

    第一个s是string,第二个s是bytes


    错误的例子:
    >>> s='\xe7\xa9\xbf\xe5\xb1\xb1\xe7\x94\xb2\xe5\x88\xb0\xe5\xba\x95\xe8\xaf\xb4\xe4\xba\x86\xe4\xbb\x80\xe4\xb9\x88\xef\xbc\x9f'
    >>> s.encode('utf-8').decode('utf-8')
    'ç©¿å±±ç\x94²å\x88°åº\x95说äº\x86ä»\x80ä¹\x88ï¼\x9f'

    用 utf-8 或者 ascii 来 encode 都是不对的
    picasso250
        10
    picasso250  
       2015-05-08 13:55:55 +08:00
    做爬虫,请用PHP或者python3。

    人生苦短。
    picasso250
        11
    picasso250  
       2015-05-08 13:56:25 +08:00
    比如 preg_match('/我就是要处理中文/u', $html_code);
    ultimate010
        12
    ultimate010  
       2015-05-08 14:06:54 +08:00 via iPhone
    2和9正解,用好decode encode python 2正则很好弄,建议用lxml库。
    commoccoom
        13
    commoccoom  
    OP
       2015-05-08 15:22:59 +08:00
    @picasso250
    python 2.7 有什么坑吗?
    caomaocao
        14
    caomaocao  
       2015-05-08 16:48:44 +08:00
    可以用这个%\w{2}){3,} 但要主要gbk编码的...
    commoccoom
        15
    commoccoom  
    OP
       2015-05-09 21:46:38 +08:00
    @ultimate010 再问一个问题:为什么 "^[0\\\-9]$" 和 "^[0\\\\-9]$" 是等价的?
    ultimate010
        16
    ultimate010  
       2015-05-10 09:12:55 +08:00 via iPhone
    commoccoom
        17
    commoccoom  
    OP
       2015-05-10 09:22:15 +08:00
    @ultimate010

    我用 r"^[0\\-9]$" == "^[0\\\-9]$" 返回True 用 r"^[0\\-9]$" == "^[0\\\\-9]$" 返回True
    "^[0\\\-9]$" == "^[0\\\\-9]$" 同样返回 True 这样我就看不懂了。
    ultimate010
        18
    ultimate010  
       2015-05-10 12:06:15 +08:00
    In [1]: a = "^[0\\\-9]$"

    In [2]: a
    Out[2]: '^[0\\\\-9]$'

    In [3]: b = "^[0\-9]"

    In [4]: b
    Out[4]: '^[0\\-9]'

    In [5]: c = "^[0\\\\-9]"

    In [6]: c
    Out[6]: '^[0\\\\-9]'

    我理解 "^[0\\\-9]$" 这种情况,前两个'\\'表示转义'\'就是原始'\',后面单独的'\'后是'-',不是转义情况,所以理解为'\',存储是表示成转义'\',所以输出中有'\\\\'四个'\'。
    In [1]: r"^[0\\-9]$"
    Out[1]: '^[0\\\\-9]$'
    加了r后,就自动帮你加了转义需要的'\'。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3873 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 04:14 · PVG 12:14 · LAX 21:14 · JFK 00:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.