San*_*ich 3 python regex unicode
我编写了在网页中查找表达式的脚本:
import sre, urllib2, sys, BaseHTTPServer
# -*- coding: utf-8 -*-
address = sys.argv[1]
web_handle = urllib2.urlopen(address)
website_text = website_handle.read()
matches = sre.findall(u"?????", website_text)
for item in matches:
print iten
Run Code Online (Sandbox Code Playgroud)
如果我使用“正则”正则表达式(不带希伯来字符),则此脚本有效,如果使用它们,则不匹配任何内容。我究竟做错了什么?
编辑 示例:url = https://en.wikipedia.org/wiki/Category:Countries
您需要确保输入字符串也采用UTF8格式。
使用unicode
function utf-8
作为第二个参数:
website_text = unicode(website_text, "utf-8")
Run Code Online (Sandbox Code Playgroud)
一切都应采用一致的编码,以便Unicode在Python 2中正常工作。