2005年02月17日 星期四 17:43
简单的利用minidom解析xml文件的测试,在xml文件中含有中文时,minidom的parse方法抛出异常了.
如果不含有中文,则解析是正确的. 请问,如何解析含有中文的xml文件?
我的 test.py 的代码如下:
# -*- coding: gb2312 -*-
import xml.dom.minidom
dom = xml.dom.minidom.parse('brz_sys.xml')
remote_ip_list = dom.getElementsByTagName('breeze_sys_config')[0].getElementsByTagName('remote_ip')
for remote_ip_node in remote_ip_list:
print remote_ip_node.getElementsByTagName('ip')[0].firstChild.toxml()
xml 文件内容如下:
10.71.105.27
root
中文
#
抛出的异常如下:
Traceback (most recent call last):
File "D:\py\test.py", line 6, in ?
dom = xml.dom.minidom.parse('brz_sys.xml')
File "c:\python24\lib\xml\dom\minidom.py", line 1915, in parse
return expatbuilder.parse(file)
File "C:\Python24\lib\xml\dom\expatbuilder.py", line 924, in parse
result = builder.parseFile(fp)
File "C:\Python24\lib\xml\dom\expatbuilder.py", line 207, in parseFile
parser.Parse(buffer, 0)
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 9, column 11
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20050217/49cf7a92/attachment.html
2005年02月17日 星期四 18:05
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, 尝试设置linux的编码为GB2312看看? __ Best Regards, Wang Kebo >-----Original Message----- >From: python-chinese-bounces at lists.python.cn >[mailto:python-chinese-bounces at lists.python.cn] On Behalf Of cry >Sent: Thursday, February 17, 2005 5:35 PM >To: Zoom Quiet, python-chinese at lists.python.cn >Subject: Re: Re: [python-chinese] LINUX下中文的?? > >Zoom Quiet,您好! > >我在LINUX下实验了,MBCS确实不行,PYTHON24也是如此。我打开mbcs.py,第? >恍行吹乃凳荈OR WINDOWS。:( >UTF-8和MBCS是什么关系?比如我在LINUX下用UTF-8写文字到文件中,这些文件 >在WINDOWS下正确显示吗?怎么解决这个问题呢? > >头疼呀! > >在 2005-02-17 09:55:00 您写道: >>没有 MBCS 这东西的,是MS 自说自话的东西, >>其实就是 GB2312, >> >>你应该全部向 utf-8 进行处理是也乎是也乎... >> >> >>On Wed, 16 Feb 2005 18:22:56 +0800, cry <zyqmail at 163.net> wrote: >>> python,您好! >>> >>> 各位好,春节去了外地,没有在这里和大家欢度。在这里向各位同好拜个晚 >>> 年。 >>> >>> 另外有个问题,我需要在LINUX下处理MBCS,但是说没有MBCS的ENCODING,? >>> 飧鲇惺裁春玫慕饩霭旆穑? >>> >>> 谢谢。 >>> >>> 致 >>> 礼! >>> >>> cry >>> zyqmail at 163.net >>> >>> _______________________________________________ >>> python-chinese list >>> python-chinese at lists.python.cn >>> http://python.cn/mailman/listinfo/python-chinese >>> >> >> >>-- >>[Time is unimportant, only life important!] >>_______________________________________________ >>python-chinese list >>python-chinese at lists.python.cn >>http://python.cn/mailman/listinfo/python-chinese > > 致 >礼! > > cry > zyqmail at 163.net > > >_______________________________________________ >python-chinese list >python-chinese at lists.python.cn >http://python.cn/mailman/listinfo/python-chinese -----BEGIN PGP SIGNATURE----- Version: PGP 8.0.3 iQA/AwUBQhRsbme19GJNaRKTEQIWHACfdRDelmHQhvbsYolPF6IcJJe25CsAoObC C12lAVK/uYHrle51eAix7SmV =zpad -----END PGP SIGNATURE-----
2005年02月17日 星期四 18:07
改成utf-8编码,encoding="utf-8",同时文件也要按utf-8编码保存。记得zoomq说过可以使用gbk,你可以试一试。 On Thu, 17 Feb 2005 17:43:42 +0800, hw_cg <hw_cg at huawei.com> wrote: > > 简单的利用minidom解析xml文件的测试,在xml文件中含有中文时,minidom的parse方法抛出异常了. > 如果不含有中文,则解析是正确的. 请问,如何解析含有中文的xml文件? > > 我的 test.py 的代码如下: > > # -*- coding: gb2312 -*- > import xml.dom.minidom > dom = xml.dom.minidom.parse('brz_sys.xml') > remote_ip_list = > dom.getElementsByTagName('breeze_sys_config')[0].getElementsByTagName('remote_ip') > > for remote_ip_node in remote_ip_list: > print remote_ip_node.getElementsByTagName('ip')[0].firstChild.toxml() > > > > xml 文件内容如下: >> > 10.71.105.27 >root >中文 ># > > > > > > 抛出的异常如下: > Traceback (most recent call last): > File "D:\py\test.py", line 6, in ? > dom = xml.dom.minidom.parse('brz_sys.xml') > File "c:\python24\lib\xml\dom\minidom.py", line 1915, in parse > return expatbuilder.parse(file) > File "C:\Python24\lib\xml\dom\expatbuilder.py", line 924, in parse > result = builder.parseFile(fp) > File "C:\Python24\lib\xml\dom\expatbuilder.py", line 207, in parseFile > parser.Parse(buffer, 0) > xml.parsers.expat.ExpatError: not well-formed (invalid token): line 9, > column 11 > > > > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese > > > -- I like python! My Blog: http://www.donews.net/limodou New Maillist: http://groups-beta.google.com/group/python-cn
2005年02月17日 星期四 18:28
我这边也发现了这个解决方法, xml用utf-8保存, encoding 也采用utf-8.
但是接着来了新问题.
我把从xml文件中读出来的值保存在类成员中后,调用类的__str__方法出了一点问题.
__str__() 方法这样写:
def __str__( self ):
s = "ip=%s,name=%s,passwd=%s,cmd_pro=%s" % \
( self.ip,self.user_name,self.password, self.cmd_prompt )
return s
执行 print node 时会出错,其中node是一个类实例:
Traceback (most recent call last):
File "D:\py\xml_test.py", line 41, in ?
print node
UnicodeEncodeError: 'ascii' codec can't encode characters in position 33-34: ordinal not in range(128)
如果是超过128的原因, 那为什么我如下写却又没有问题:
def __str__( self ):
s = "ip=%s,name=%s,passwd=%s,cmd_pro=%s" % \
( "中文", "中文", "中文", "中文" )
return s
而且我这样写也没有错:
def __str__( self ):
s = "ip=%s,name=%s,passwd=%s,cmd_pro=%s" % \
( self.ip,self.user_name,self.password, self.cmd_prompt )
print s
return ""
看样子好像是 return 时出了问题.
----- Original Message -----
From: "limodou" <limodou at gmail.com>
To: <python-chinese at lists.python.cn>
Sent: Thursday, February 17, 2005 6:07 PM
Subject: Re: [python-chinese] xml.dom.minidom在解析带有中文的xml文件 时抛出异常,请问如何解决这个问题?
改成utf-8编码,encoding="utf-8",同时文件也要按utf-8编码保存。记得zoomq说过可以使用gbk,你可以试一试。
On Thu, 17 Feb 2005 17:43:42 +0800, hw_cg <hw_cg at huawei.com> wrote:
>
> 简单的利用minidom解析xml文件的测试,在xml文件中含有中文时,minidom的parse方法抛出异常了.
> 如果不含有中文,则解析是正确的. 请问,如何解析含有中文的xml文件?
>
> 我的 test.py 的代码如下:
>
> # -*- coding: gb2312 -*-
> import xml.dom.minidom
> dom = xml.dom.minidom.parse('brz_sys.xml')
> remote_ip_list =
> dom.getElementsByTagName('breeze_sys_config')[0].getElementsByTagName('remote_ip')
>
> for remote_ip_node in remote_ip_list:
> print remote_ip_node.getElementsByTagName('ip')[0].firstChild.toxml()
>
>
>
> xml 文件内容如下:
>
>
> 10.71.105.27
> root
> 中文
> #
>
>
>
>
>
> 抛出的异常如下:
> Traceback (most recent call last):
> File "D:\py\test.py", line 6, in ?
> dom = xml.dom.minidom.parse('brz_sys.xml')
> File "c:\python24\lib\xml\dom\minidom.py", line 1915, in parse
> return expatbuilder.parse(file)
> File "C:\Python24\lib\xml\dom\expatbuilder.py", line 924, in parse
> result = builder.parseFile(fp)
> File "C:\Python24\lib\xml\dom\expatbuilder.py", line 207, in parseFile
> parser.Parse(buffer, 0)
> xml.parsers.expat.ExpatError: not well-formed (invalid token): line 9,
> column 11
>
>
>
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
>
>
>
--
I like python!
My Blog: http://www.donews.net/limodou
New Maillist: http://groups-beta.google.com/group/python-cn
_______________________________________________
python-chinese list
python-chinese at lists.python.cn
http://python.cn/mailman/listinfo/python-chinese
2005年02月17日 星期四 18:36
解析出来的是utf-8的,你需要转为本地的编码 On Thu, 17 Feb 2005 18:28:01 +0800, hw_cg <hw_cg at huawei.com> wrote: > 我这边也发现了这个解决方法, xml用utf-8保存, encoding 也采用utf-8. > 但是接着来了新问题. > 我把从xml文件中读出来的值保存在类成员中后,调用类的__str__方法出了一点问题. > > __str__() 方法这样写: > > def __str__( self ): > s = "ip=%s,name=%s,passwd=%s,cmd_pro=%s" % \ > ( self.ip,self.user_name,self.password, self.cmd_prompt ) > return s > > 执行 print node 时会出错,其中node是一个类实例: > Traceback (most recent call last): > File "D:\py\xml_test.py", line 41, in ? > print node > UnicodeEncodeError: 'ascii' codec can't encode characters in position 33-34: ordinal not in range(128) > > 如果是超过128的原因, 那为什么我如下写却又没有问题: > def __str__( self ): > s = "ip=%s,name=%s,passwd=%s,cmd_pro=%s" % \ > ( "中文", "中文", "中文", "中文" ) > return s > > 而且我这样写也没有错: > def __str__( self ): > s = "ip=%s,name=%s,passwd=%s,cmd_pro=%s" % \ > ( self.ip,self.user_name,self.password, self.cmd_prompt ) > print s > return "" > > 看样子好像是 return 时出了问题. > > > ----- Original Message ----- > From: "limodou" <limodou at gmail.com> > To: <python-chinese at lists.python.cn> > Sent: Thursday, February 17, 2005 6:07 PM > Subject: Re: [python-chinese] xml.dom.minidom在解析带有中文的xml文件 时抛出异常,请问如何解决这个问题? > > 改成utf-8编码,encoding="utf-8",同时文件也要按utf-8编码保存。记得zoomq说过可以使用gbk,你可以试一试。 > > On Thu, 17 Feb 2005 17:43:42 +0800, hw_cg <hw_cg at huawei.com> wrote: > > > > 简单的利用minidom解析xml文件的测试,在xml文件中含有中文时,minidom的parse方法抛出异常了. > > 如果不含有中文,则解析是正确的. 请问,如何解析含有中文的xml文件? > > > > 我的 test.py 的代码如下: > > > > # -*- coding: gb2312 -*- > > import xml.dom.minidom > > dom = xml.dom.minidom.parse('brz_sys.xml') > > remote_ip_list = > > dom.getElementsByTagName('breeze_sys_config')[0].getElementsByTagName('remote_ip') > > > > for remote_ip_node in remote_ip_list: > > print remote_ip_node.getElementsByTagName('ip')[0].firstChild.toxml() > > > > > > > > xml 文件内容如下: > >> > > > 10.71.105.27 > >root > >中文 > ># > > > > > > > > > > > > 抛出的异常如下: > > Traceback (most recent call last): > > File "D:\py\test.py", line 6, in ? > > dom = xml.dom.minidom.parse('brz_sys.xml') > > File "c:\python24\lib\xml\dom\minidom.py", line 1915, in parse > > return expatbuilder.parse(file) > > File "C:\Python24\lib\xml\dom\expatbuilder.py", line 924, in parse > > result = builder.parseFile(fp) > > File "C:\Python24\lib\xml\dom\expatbuilder.py", line 207, in parseFile > > parser.Parse(buffer, 0) > > xml.parsers.expat.ExpatError: not well-formed (invalid token): line 9, > > column 11 > > > > > > > > _______________________________________________ > > python-chinese list > > python-chinese at lists.python.cn > > http://python.cn/mailman/listinfo/python-chinese > > > > > > > > -- > I like python! > My Blog: http://www.donews.net/limodou > New Maillist: http://groups-beta.google.com/group/python-cn > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese > > > -- My Blog >> http://leejd.cndev.org My QQ >> 9847243
2005年02月17日 星期四 18:50
本地的编码应该是mbcs,我用sys.getfilesystemencoding()看过.
但是象下面这样写不行:
def __str__( self ):
s = "ip=%s,name=%s,passwd=%s,cmd_pro=%s" % \
( self.ip,self.user_name,self.password, self.cmd_prompt )
s = unicode( s, 'mbcs' )
return s
错误提示如下:
Traceback (most recent call last):
File "D:\py\xml_test.py", line 50, in ?
print node
File "D:\py\xml_test.py", line 25, in __str__
s = unicode( s, 'mbcs' )
TypeError: decoding Unicode is not supported
此外,mbcs替换为其它的如 ansii, utf-8, gb2312等都不行.提示信息都是不支持.
----- Original Message -----
From: "Gerald Lee" <leejd80 at gmail.com>
To: <python-chinese at lists.python.cn>
Sent: Thursday, February 17, 2005 6:36 PM
Subject: Re: [python-chinese] xml.dom.min idom在解析带有中文的xml文件 时 抛出异常,请问如何解决这个问题?
解析出来的是utf-8的,你需要转为本地的编码
On Thu, 17 Feb 2005 18:28:01 +0800, hw_cg <hw_cg at huawei.com> wrote:
> 我这边也发现了这个解决方法, xml用utf-8保存, encoding 也采用utf-8.
> 但是接着来了新问题.
> 我把从xml文件中读出来的值保存在类成员中后,调用类的__str__方法出了一点问题.
>
> __str__() 方法这样写:
>
> def __str__( self ):
> s = "ip=%s,name=%s,passwd=%s,cmd_pro=%s" % \
> ( self.ip,self.user_name,self.password, self.cmd_prompt )
> return s
>
> 执行 print node 时会出错,其中node是一个类实例:
> Traceback (most recent call last):
> File "D:\py\xml_test.py", line 41, in ?
> print node
> UnicodeEncodeError: 'ascii' codec can't encode characters in position 33-34: ordinal not in range(128)
>
> 如果是超过128的原因, 那为什么我如下写却又没有问题:
> def __str__( self ):
> s = "ip=%s,name=%s,passwd=%s,cmd_pro=%s" % \
> ( "中文", "中文", "中文", "中文" )
> return s
>
> 而且我这样写也没有错:
> def __str__( self ):
> s = "ip=%s,name=%s,passwd=%s,cmd_pro=%s" % \
> ( self.ip,self.user_name,self.password, self.cmd_prompt )
> print s
> return ""
>
> 看样子好像是 return 时出了问题.
>
>
> ----- Original Message -----
> From: "limodou" <limodou at gmail.com>
> To: <python-chinese at lists.python.cn>
> Sent: Thursday, February 17, 2005 6:07 PM
> Subject: Re: [python-chinese] xml.dom.minidom在解析带有中文的xml文件 时抛出异常,请问如何解决这个问题?
>
> 改成utf-8编码,encoding="utf-8",同时文件也要按utf-8编码保存。记得zoomq说过可以使用gbk,你可以试一试。
>
> On Thu, 17 Feb 2005 17:43:42 +0800, hw_cg <hw_cg at huawei.com> wrote:
> >
> > 简单的利用minidom解析xml文件的测试,在xml文件中含有中文时,minidom的parse方法抛出异常了.
> > 如果不含有中文,则解析是正确的. 请问,如何解析含有中文的xml文件?
> >
> > 我的 test.py 的代码如下:
> >
> > # -*- coding: gb2312 -*-
> > import xml.dom.minidom
> > dom = xml.dom.minidom.parse('brz_sys.xml')
> > remote_ip_list =
> > dom.getElementsByTagName('breeze_sys_config')[0].getElementsByTagName('remote_ip')
> >
> > for remote_ip_node in remote_ip_list:
> > print remote_ip_node.getElementsByTagName('ip')[0].firstChild.toxml()
> >
> >
> >
> > xml 文件内容如下:
> >
> >
> > 10.71.105.27
> > root
> > 中文
> > #
> >
> >
> >
> >
> >
> > 抛出的异常如下:
> > Traceback (most recent call last):
> > File "D:\py\test.py", line 6, in ?
> > dom = xml.dom.minidom.parse('brz_sys.xml')
> > File "c:\python24\lib\xml\dom\minidom.py", line 1915, in parse
> > return expatbuilder.parse(file)
> > File "C:\Python24\lib\xml\dom\expatbuilder.py", line 924, in parse
> > result = builder.parseFile(fp)
> > File "C:\Python24\lib\xml\dom\expatbuilder.py", line 207, in parseFile
> > parser.Parse(buffer, 0)
> > xml.parsers.expat.ExpatError: not well-formed (invalid token): line 9,
> > column 11
> >
> >
> >
> > _______________________________________________
> > python-chinese list
> > python-chinese at lists.python.cn
> > http://python.cn/mailman/listinfo/python-chinese
> >
> >
> >
>
> --
> I like python!
> My Blog: http://www.donews.net/limodou
> New Maillist: http://groups-beta.google.com/group/python-cn
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese
>
>
>
--
My Blog >> http://leejd.cndev.org
My QQ >> 9847243
_______________________________________________
python-chinese list
python-chinese at lists.python.cn
http://python.cn/mailman/listinfo/python-chinese
2005年02月17日 星期四 19:42
你看一下s是什么编码,我感觉就已经是unicode了。再转就会出错了。 On Thu, 17 Feb 2005 18:50:49 +0800, hw_cg <hw_cg at huawei.com> wrote: > 本地的编码应该是mbcs,我用sys.getfilesystemencoding()看过. > 但是象下面这样写不行: > > def __str__( self ): > s = "ip=%s,name=%s,passwd=%s,cmd_pro=%s" % \ > ( self.ip,self.user_name,self.password, self.cmd_prompt ) > s = unicode( s, 'mbcs' ) > return s > > 错误提示如下: > Traceback (most recent call last): > File "D:\py\xml_test.py", line 50, in ? > print node > File "D:\py\xml_test.py", line 25, in __str__ > s = unicode( s, 'mbcs' ) > TypeError: decoding Unicode is not supported > > 此外,mbcs替换为其它的如 ansii, utf-8, gb2312等都不行.提示信息都是不支持. > > > ----- Original Message ----- > From: "Gerald Lee" <leejd80 at gmail.com> > To: <python-chinese at lists.python.cn> > Sent: Thursday, February 17, 2005 6:36 PM > Subject: Re: [python-chinese] xml.dom.min idom在解析带有中文的xml文件 时 抛出异常,请问如何解决这个问题? > > 解析出来的是utf-8的,你需要转为本地的编码 > > > On Thu, 17 Feb 2005 18:28:01 +0800, hw_cg <hw_cg at huawei.com> wrote: > > 我这边也发现了这个解决方法, xml用utf-8保存, encoding 也采用utf-8. > > 但是接着来了新问题. > > 我把从xml文件中读出来的值保存在类成员中后,调用类的__str__方法出了一点问题. > > > > __str__() 方法这样写: > > > > def __str__( self ): > > s = "ip=%s,name=%s,passwd=%s,cmd_pro=%s" % \ > > ( self.ip,self.user_name,self.password, self.cmd_prompt ) > > return s > > > > 执行 print node 时会出错,其中node是一个类实例: > > Traceback (most recent call last): > > File "D:\py\xml_test.py", line 41, in ? > > print node > > UnicodeEncodeError: 'ascii' codec can't encode characters in position 33-34: ordinal not in range(128) > > > > 如果是超过128的原因, 那为什么我如下写却又没有问题: > > def __str__( self ): > > s = "ip=%s,name=%s,passwd=%s,cmd_pro=%s" % \ > > ( "中文", "中文", "中文", "中文" ) > > return s > > > > 而且我这样写也没有错: > > def __str__( self ): > > s = "ip=%s,name=%s,passwd=%s,cmd_pro=%s" % \ > > ( self.ip,self.user_name,self.password, self.cmd_prompt ) > > print s > > return "" > > > > 看样子好像是 return 时出了问题. > > > > > > ----- Original Message ----- > > From: "limodou" <limodou at gmail.com> > > To: <python-chinese at lists.python.cn> > > Sent: Thursday, February 17, 2005 6:07 PM > > Subject: Re: [python-chinese] xml.dom.minidom在解析带有中文的xml文件 时抛出异常,请问如何解决这个问题? > > > > 改成utf-8编码,encoding="utf-8",同时文件也要按utf-8编码保存。记得zoomq说过可以使用gbk,你可以试一试。 > > > > On Thu, 17 Feb 2005 17:43:42 +0800, hw_cg <hw_cg at huawei.com> wrote: > > > > > > 简单的利用minidom解析xml文件的测试,在xml文件中含有中文时,minidom的parse方法抛出异常了. > > > 如果不含有中文,则解析是正确的. 请问,如何解析含有中文的xml文件? > > > > > > 我的 test.py 的代码如下: > > > > > > # -*- coding: gb2312 -*- > > > import xml.dom.minidom > > > dom = xml.dom.minidom.parse('brz_sys.xml') > > > remote_ip_list = > > > dom.getElementsByTagName('breeze_sys_config')[0].getElementsByTagName('remote_ip') > > > > > > for remote_ip_node in remote_ip_list: > > > print remote_ip_node.getElementsByTagName('ip')[0].firstChild.toxml() > > > > > > > > > > > > xml 文件内容如下: > > >> > > > > > 10.71.105.27 > > >root > > >中文 > > ># > > > > > > > > > > > > > > > > > > 抛出的异常如下: > > > Traceback (most recent call last): > > > File "D:\py\test.py", line 6, in ? > > > dom = xml.dom.minidom.parse('brz_sys.xml') > > > File "c:\python24\lib\xml\dom\minidom.py", line 1915, in parse > > > return expatbuilder.parse(file) > > > File "C:\Python24\lib\xml\dom\expatbuilder.py", line 924, in parse > > > result = builder.parseFile(fp) > > > File "C:\Python24\lib\xml\dom\expatbuilder.py", line 207, in parseFile > > > parser.Parse(buffer, 0) > > > xml.parsers.expat.ExpatError: not well-formed (invalid token): line 9, > > > column 11 > > > > > > > > > > > > _______________________________________________ > > > python-chinese list > > > python-chinese at lists.python.cn > > > http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > > > > -- > > I like python! > > My Blog: http://www.donews.net/limodou > > New Maillist: http://groups-beta.google.com/group/python-cn > > _______________________________________________ > > python-chinese list > > python-chinese at lists.python.cn > > http://python.cn/mailman/listinfo/python-chinese > > _______________________________________________ > > python-chinese list > > python-chinese at lists.python.cn > > http://python.cn/mailman/listinfo/python-chinese > > > > > > > > -- > My Blog >> http://leejd.cndev.org > My QQ >> 9847243 > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese > > > -- I like python! My Blog: http://www.donews.net/limodou New Maillist: http://groups-beta.google.com/group/python-cn
2005年02月18日 星期五 09:22
我前面的邮件中已经说了这个问题了--- 如果s不转化会出现下面的错误.
__str__() 方法这样写:
def __str__( self ):
s = "ip=%s,name=%s,passwd=%s,cmd_pro=%s" % \
( self.ip,self.user_name,self.password, self.cmd_prompt )
return s
执行 print node 时会出错,其中node是一个类实例, print node则会调用node类的__str__方法,即上面定义的__str__方法:
Traceback (most recent call last):
File "D:\py\xml_test.py", line 41, in ?
print node
UnicodeEncodeError: 'ascii' codec can't encode characters in position 33-34: ordinal not in range(128)
我现在也不知道是怎么回事了.因为象下面这样写是没有问题的:
def __str__( self ):
#s = "ip=%s,name=%s,passwd=%s,cmd_pro=%s" % \
# ( self.ip,self.user_name,self.password, self.cmd_prompt )
s = "这是中文"
return s
代码和xml文件我都附在下面了, 代码不长,大家帮忙看看是怎么回事吧.
brz_sys.xml文件如下, 注意要另保为utf-8类型的文件:
10.71.105.27
root
口令
#
10.70.116.55
tools
root
%
xml_test.py文件如下:
# -*- coding: utf-8 -*-
# 验证xml.dom.minidom读取xml文件的测试例子
import xml.dom.minidom
class remote_ip:
def __init__( self,
ip="", user_name="",
password="", cmd_prompt="" ):
self.ip = ip
self.user_name = user_name
self.password = password
self.cmd_prompt = cmd_prompt
def __str__( self ):
s = "ip=%s,name=%s,passwd=%s,cmd_pro=%s" % \
( self.ip,self.user_name,
self.password, self.cmd_prompt )
print s
return s
def parse_node_tag_value( node , name ):
""" parse the child item value according the item name
"""
return node.getElementsByTagName( name )[0].firstChild.toxml()
def parse_ip_value( node ):
return parse_node_tag_value( node, 'ip' )
def parse_user_name_value( node ):
return parse_node_tag_value( node, 'user_name' )
def parse_password_value( node ):
return parse_node_tag_value( node, 'pasword' )
def parse_command_prompt_value( node ):
return parse_node_tag_value( node, 'command_prompt' )
def parse_one_rip( rip_node , node):
""" parse the ip,user_name,password,cmd_prompt
value of a node
"""
node.ip = parse_ip_value( rip_node )
node.user_name = parse_user_name_value( rip_node )
node.password = parse_password_value( rip_node )
node.cmd_prompt = parse_command_prompt_value( rip_node )
######################################################
#
# start
# xml配置文件的节点列表,节点为 remote_ip 类型
cfg_node_list=[]
dom = xml.dom.minidom.parse('brz_sys.xml')
# get rip_list
root = dom.getElementsByTagName('breeze_sys_config')[0]
remote_ip_list = root.getElementsByTagName('remote_ip')
# travel the rip_list,then get the ip value ,
# user name value ,password value and command prompte value
for remote_ip_node in remote_ip_list:
node = remote_ip()
parse_one_rip( remote_ip_node, node )
cfg_node_list.append( node )
# print the each node value
for node in cfg_node_list:
print node
----- Original Message -----
From: "limodou" <limodou at gmail.com>
To: <python-chinese at lists.python.cn>
Sent: Thursday, February 17, 2005 7:42 PM
Subject: Re: [python-chinese] xml.dom.min idom在解析带有中文的xml文件 时 抛出异常,请问如何解决这个问题?
你看一下s是什么编码,我感觉就已经是unicode了。再转就会出错了。
On Thu, 17 Feb 2005 18:50:49 +0800, hw_cg <hw_cg at huawei.com> wrote:
> 本地的编码应该是mbcs,我用sys.getfilesystemencoding()看过.
> 但是象下面这样写不行:
>
> def __str__( self ):
> s = "ip=%s,name=%s,passwd=%s,cmd_pro=%s" % \
> ( self.ip,self.user_name,self.password, self.cmd_prompt )
> s = unicode( s, 'mbcs' )
> return s
>
> 错误提示如下:
> Traceback (most recent call last):
> File "D:\py\xml_test.py", line 50, in ?
> print node
> File "D:\py\xml_test.py", line 25, in __str__
> s = unicode( s, 'mbcs' )
> TypeError: decoding Unicode is not supported
>
> 此外,mbcs替换为其它的如 ansii, utf-8, gb2312等都不行.提示信息都是不支持.
2005年02月18日 星期五 09:59
经过反复验证.总算解决了,在此也感谢limodou的无私帮助.
__str__方法应该如下写就没有问题了:
def __str__( self ):
s = "ip=%s,name=%s,passwd=%s,cmd_pro=%s" % \
( self.ip,self.user_name,
self.password, self.cmd_prompt )
s= s.encode(sys.getfilesystemencoding())
return s
我在return s前加了一行转换的代码.
由于从 xml文件中读取的是unicode编码, 但是__str__方法需要返回一个本地编码的串以供打印,
所以需要将unicode编码转换为本地编码.
如何实现这一步上我绕了弯路, 我使用了下面的方法
s = unicode( s, 'mbcs' )
这样做是不行的. 如果变量s是unicode编码的串,采用下面的方法就可以了:
s = s.encode( 'mbcs' ) 就可以转成本地编码了.
只是我很奇怪, 为什么采用下面的语句不行呢?
s = unicode( s, 'mbcs')
是不是已经是unicode的字符串不能再调用 unicode() 函数了呢?
那如何判断一个字符串是不是unicode呢?
----- Original Message -----
From: "hw_cg" <hw_cg at huawei.com>
To: <python-chinese at lists.python.cn>
Sent: Friday, February 18, 2005 9:22 AM
Subject: Re: [python-chinese] xml.dom.min idom在解析带有中文的xml文件 时 抛出异常,请问如何解决这个问题?
> 我前面的邮件中已经说了这个问题了--- 如果s不转化会出现下面的错误.
>
> __str__() 方法这样写:
>
> def __str__( self ):
> s = "ip=%s,name=%s,passwd=%s,cmd_pro=%s" % \
> ( self.ip,self.user_name,self.password, self.cmd_prompt )
> return s
>
> 执行 print node 时会出错,其中node是一个类实例, print node则会调用node类的__str__方法,即上面定义的__str__方法:
> Traceback (most recent call last):
> File "D:\py\xml_test.py", line 41, in ?
> print node
> UnicodeEncodeError: 'ascii' codec can't encode characters in position 33-34: ordinal not in range(128)
>
> 我现在也不知道是怎么回事了.因为象下面这样写是没有问题的:
> def __str__( self ):
> #s = "ip=%s,name=%s,passwd=%s,cmd_pro=%s" % \
> # ( self.ip,self.user_name,self.password, self.cmd_prompt )
> s = "这是中文"
> return s
>
2005年02月18日 星期五 10:22
>如何实现这一步上我绕了弯路, 我使用了下面的方法 >s = unicode( s, 'mbcs' ) >这样做是不行的. 如果变量s是unicode编码的串,采用下面的方法就可以了: >s = s.encode( 'mbcs' ) 就可以转成本地编码了. > >只是我很奇怪, 为什么采用下面的语句不行呢? >s = unicode( s, 'mbcs') >是不是已经是unicode的字符串不能再调用 unicode() 函数了呢? >那如何判断一个字符串是不是unicode呢? > > Python中unicode和string是两种不同类型的对象,它们都可以用来表示字符串, 所以不要把字符串和string等同起来。unicode 函数用来将string转化为 unicode,它的参数如果已经是unicode类型的就没有什么实际意义了。而 unicode.encode则将 unicode类型的对象使用相应的编码转化为普通的string。 判断对象是否是unicode和python的其他对象类型判断一样,可以使用 type(s) == type(u'') 或 isinstance(s, type(u'')) 或使用types模块中定义的常量types.UnicodeType。 -- HONG Yuan Homemaster Trading Co., Ltd. No. 601, Bldg. 41, 288 Shuangyang Rd. (N) Shanghai 200433, P.R.C. Tel: +86 21 55056553 Fax: +86 21 55067325 E-mail: hongyuan at homemaster.cn
Zeuux © 2025
京ICP备05028076号