2006年06月22日 星期四 20:49
如果需要在regex表达式里用中文,应该注意些什么问题。 比如: r = '微软' s = open(file).read() m = re.findall(r,s) 程序和读入的文件都是utf-8编码了,但是仍然匹配不上,正则表达式我在regexbuddy下调试没有问题。
2006年06月22日 星期四 21:05
关于中文的处理,程序内部统一用unicode字符串比较好,可以避免很多诡异的问题。 所有的外部输入在第一时间解码,输出时再相应编码就行了。 不过就你的例子来说,我这里即使不用unicode测试倒也没有问题。 On 6/22/06, chen arthur <agakong at gmail.com> wrote: > > 如果需要在regex表达式里用中文,应该注意些什么问题。 > > 比如: > r = '微软' > s = open(file).read() > m = re.findall(r,s) > > 程序和读入的文件都是utf-8编码了,但是仍然匹配不上,正则表达式我在regexbuddy下调试没有问题。 > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060622/c302da27/attachment.html
2006年06月22日 星期四 21:51
你是怎么测试的,我怎么测试都匹配不上。
统一用unicode怎么用。
我的原始文件:
# -*- coding: utf-8 -*-
import urllib,re
r = '微软'
f = urllib.urlopen('http://localhost/123.html')
s = f.read()
a = re.findall(r,s)
if not a:
print 'error'
其中123.html是utf-8编码,程序文件也是utf-8编码
在 06-6-22,swordsp<sparas2006 at gmail.com> 写道:
> 关于中文的处理,程序内部统一用unicode字符串比较好,可以避免很多诡异的问题。
> 所有的外部输入在第一时间解码,输出时再相应编码就行了。
>
> 不过就你的例子来说,我这里即使不用unicode测试倒也没有问题。
>
>
> On 6/22/06, chen arthur < agakong at gmail.com> wrote:
> > 如果需要在regex表达式里用中文,应该注意些什么问题。
> >
> > 比如:
> > r = '微软'
> > s = open(file).read()
> > m = re.findall(r,s)
> >
> > 程序和读入的文件都是utf-8编码了,但是仍然匹配不上,正则表达式我在regexbuddy下调试没有问题。
> >
> >
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
>
2006年06月22日 星期四 22:52
On 6/22/06, chen arthur <agakong at gmail.com> wrote: > 你是怎么测试的,我怎么测试都匹配不上。 > 统一用unicode怎么用。 > 我的原始文件: > # -*- coding: utf-8 -*- > import urllib,re > r = '微软' > f = urllib.urlopen('http://localhost/123.html') > s = f.read() > a = re.findall(r,s) > if not a: > print 'error' > > 其中123.html是utf-8编码,程序文件也是utf-8编码 > 建议你先在命令行下试试。 -- I like python! My Blog: http://www.donews.net/limodou My Django Site: http://www.djangocn.org NewEdit Maillist: http://groups.google.com/group/NewEdit
Zeuux © 2025
京ICP备05028076号