2007年10月19日 星期五 09:47
ÎÒÓÐÒ»¸ö³ÌÐòÏëҪдxmlÎļþ£¬µ±ÒÔutf-8À´Ð´Ê±ÊǺõģ¬µ«ÒÔutf-16д֮ºó£¬´ò¿ª¾Í´æÔÚÂÒÂë¡£ ´úÂëÈçÏÂ: import sys import xml.dom.minidom impl = xml.dom.minidom.getDOMImplementation() dom = impl.createDocument(None, 'catalog', None) root = dom.documentElement text=dom.createTextNode('test') item = dom.createElement('caption') item.appendChild(text) item.setAttribute('id', 'id value') root.appendChild(item) print root.toxml() file = open(r'd:\1.xml', 'w') import codecs writer = codecs.lookup('utf-8')[3](file) #ÕâÀïºÍÏÂÃæ»»³É'utf-16'¾Í»á²úÉúÂÒÂëÎļþ¡£ dom.writexml(writer=writer, addindent='\t', newl='\n', encoding='utf-8') writer.close() µ«ÎÒ²é×ÊÁÏ˵£¬XML´¦ÀíʱÄÚ²¿Ê¹ÓÃUnicode±àÂ룬°´Ëµutf-16Ó¦¸ÃûÓÐÎÊÌâµÄѽ£¬ÎªÊ²Ã´£¿£¿ -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20071019/9d20c0f1/attachment.html
2007年10月19日 星期五 10:01
xml文件中第一行应该是: 如果是utf-16,改一下试试看,同时你的整个文件要保存为utf-16吧.不过我没有看到过有人使用utf-16来处理xml On 10/19/07, ??? ?? <clfff.peter在gmail.com> wrote: > 我有一个程序想要写xml文件,当以utf-8来写时是好的,但以utf-16写之后,打开就存在乱码。 > 代码如下: > import sys > > > import xml.dom.minidom > > impl = xml.dom.minidom.getDOMImplementation() > dom = impl.createDocument(None, 'catalog', None) > root = dom.documentElement > text=dom.createTextNode('test') > item = dom.createElement('caption') > item.appendChild(text) > item.setAttribute('id', 'id value') > root.appendChild(item) > print root.toxml() > file = open(r'd:\1.xml', 'w') > import codecs > writer = codecs.lookup ('utf-8')[3](file) > #这里和下面换成'utf-16'就会产生乱码文件。 > dom.writexml(writer=writer, addindent='\t', newl='\n', encoding='utf-8') > writer.close() > > 但我查资料说,XML处理时内部使用Unicode编码,按说utf-16应该没有问题的呀,为什么?? -- I like python! UliPad <>: http://code.google.com/p/ulipad/ meide < >: http://code.google.com/p/meide/ My Blog: http://www.donews.net/limodou
2007年10月19日 星期五 11:22
用winhex打开生成的xml文件,看一下十六进制就清楚了。 是dom.writexml(writer=writer, addindent='\t', newl='\n', encoding='utf-8')中'\t'和'\n'的问题。 在 07-10-19,limodou<limodou at gmail.com> 写道: > xml文件中第一行应该是: > > > > 如果是utf-16,改一下试试看,同时你的整个文件要保存为utf-16吧.不过我没有看到过有人使用utf-16来处理xml > > On 10/19/07, ??? ?? <clfff.peter at gmail.com> wrote: > > 我有一个程序想要写xml文件,当以utf-8来写时是好的,但以utf-16写之后,打开就存在乱码。 > > 代码如下: > > import sys > > > > > > import xml.dom.minidom > > > > impl = xml.dom.minidom.getDOMImplementation() > > dom = impl.createDocument(None, 'catalog', None) > > root = dom.documentElement > > text=dom.createTextNode('test') > > item = dom.createElement('caption') > > item.appendChild(text) > > item.setAttribute('id', 'id value') > > root.appendChild(item) > > print root.toxml() > > file = open(r'd:\1.xml', 'w') > > import codecs > > writer = codecs.lookup ('utf-8')[3](file) > > #这里和下面换成'utf-16'就会产生乱码文件。 > > dom.writexml(writer=writer, addindent='\t', newl='\n', encoding='utf-8') > > writer.close() > > > > 但我查资料说,XML处理时内部使用Unicode编码,按说utf-16应该没有问题的呀,为什么?? > > > -- > I like python! > UliPad <>: http://code.google.com/p/ulipad/ > meide <>: http://code.google.com/p/meide/ > My Blog: http://www.donews.net/limodou > _______________________________________________ > 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 -- wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: http://python.cn/pipermail/python-chinese/attachments/20071019/a2c785db/attachment.html
2007年10月19日 星期五 13:58
8´í£¬¾ÍÊÇaddindent='\t', newl='\n'µÄÎÊÌ⣬¿É²»ÖªµÀÓÐûÓÐʲô°ì·¨½â¾öµÄ£¿ ʵÔÚ²»ÐоÍÖ»ÄÜÓÃ'utf-8'ÁË¡£ ÔÚ07-10-19£¬Wayne <moonbingbing在gmail.com> дµÀ£º > > ÓÃwinhex´ò¿ªÉú³ÉµÄxmlÎļþ£¬¿´Ò»ÏÂÊ®Áù½øÖƾÍÇå³þÁË¡£ > ÊÇdom.writexml(writer=writer, addindent='\t', newl='\n', > encoding='utf-8')ÖÐ'\t'ºÍ'\n'µÄÎÊÌâ¡£ > > ÔÚ 07-10-19£¬limodou<limodou在gmail.com> дµÀ£º > > xmlÎļþÖеÚÒ»ÐÐÓ¦¸ÃÊÇ: > > > > > > > > Èç¹ûÊÇutf-16,¸ÄÒ»ÏÂÊÔÊÔ¿´, ͬʱÄãµÄÕû¸öÎļþÒª±£´æΪutf-16°É.²»¹ýÎÒûÓп´µ½¹ýÓÐÈËʹÓÃutf-16À´´¦Àíxml > > > > On 10/19/07, ??? ?? <clfff.peter在gmail.com> wrote: > > > ÎÒÓÐÒ»¸ö³ÌÐòÏëҪдxmlÎļþ£¬µ±ÒÔutf-8À´Ð´Ê±ÊǺõģ¬µ«ÒÔutf-16д֮ºó£¬´ò¿ª¾Í´æÔÚÂÒÂë¡£ > > > ´úÂëÈçÏÂ: > > > import sys > > > > > > > > > import xml.dom.minidom > > > > > > impl = xml.dom.minidom.getDOMImplementation() > > > dom = impl.createDocument(None, 'catalog', None) > > > root = dom.documentElement > > > text=dom.createTextNode('test') > > > item = dom.createElement('caption') > > > item.appendChild(text) > > > item.setAttribute('id', 'id value') > > > root.appendChild(item) > > > print root.toxml() > > > file = open(r'd:\1.xml', 'w') > > > import codecs > > > writer = codecs.lookup ('utf-8')[3](file) > > > #ÕâÀïºÍÏÂÃæ»»³É'utf-16'¾Í»á²úÉúÂÒÂëÎļþ¡£ > > > dom.writexml(writer=writer, addindent='\t', newl='\n', > encoding='utf-8') > > > writer.close() > > > > > > µ«ÎÒ²é×ÊÁÏ˵£¬XML´¦ÀíʱÄÚ²¿Ê¹ÓÃUnicode±àÂ룬°´Ëµutf-16Ó¦¸ÃûÓÐÎÊÌâµÄѽ£¬ÎªÊ²Ã´£¿£¿ > > > > > > -- > > I like python! > > UliPad <>: http://code.google.com/p/ulipad/ > > meide <>: http://code.google.com/p/meide/ > > My Blog: http://www.donews.net/limodou > > _______________________________________________ > > python-chinese > > Post: send python-chinese在lists.python.cn > > Subscribe: send subscribe to python-chinese-request在lists.python.cn > > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > -- > wayne > > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20071019/f261983e/attachment.html
2007年10月19日 星期五 14:04
ÄÇÊDz»Êǵà '\t'.decode('utf-8').encode('utf-16') Èç¹ûÄãµÄpyÎļþÊÇÓÃutf-8±£´æµÄ On 10/19/07, ??? ?? <clfff.peter在gmail.com> wrote: > > 8´í£¬¾ÍÊÇaddindent='\t', newl='\n'µÄÎÊÌ⣬¿É²»ÖªµÀÓÐûÓÐʲô°ì·¨½â¾öµÄ£¿ > ʵÔÚ²»ÐоÍÖ»ÄÜÓÃ'utf-8'ÁË¡£ > > > ÔÚ07-10-19£¬Wayne <moonbingbing在gmail.com> дµÀ£º > > > > ÓÃwinhex´ò¿ªÉú³ÉµÄxmlÎļþ£¬¿´Ò»ÏÂÊ®Áù½øÖƾÍÇå³þÁË¡£ > > ÊÇdom.writexml(writer=writer, addindent='\t', newl='\n', > > encoding='utf-8')ÖÐ'\t'ºÍ'\n'µÄÎÊÌâ¡£ > > > > ÔÚ 07-10-19£¬limodou< limodou在gmail.com> дµÀ£º > > > xmlÎļþÖеÚÒ»ÐÐÓ¦¸ÃÊÇ: > > > > > > > > > > > > Èç¹ûÊÇutf-16,¸ÄÒ»ÏÂÊÔÊÔ¿´, ͬʱÄãµÄÕû¸öÎļþÒª±£´æΪutf-16°É.²»¹ýÎÒûÓп´µ½¹ýÓÐÈËʹÓÃutf-16À´´¦Àíxml > > > > > > On 10/19/07, ??? ?? <clfff.peter在gmail.com> wrote: > > > > ÎÒÓÐÒ»¸ö³ÌÐòÏëҪдxmlÎļþ£¬µ±ÒÔutf-8À´Ð´Ê±ÊǺõģ¬µ«ÒÔutf-16д֮ºó£¬´ò¿ª¾Í´æÔÚÂÒÂë¡£ > > > > ´úÂëÈçÏÂ: > > > > import sys > > > > > > > > > > > > import xml.dom.minidom > > > > > > > > impl = xml.dom.minidom.getDOMImplementation() > > > > dom = impl.createDocument(None, 'catalog', None) > > > > root = dom.documentElement > > > > text=dom.createTextNode('test') > > > > item = dom.createElement('caption') > > > > item.appendChild(text) > > > > item.setAttribute('id', 'id value') > > > > root.appendChild(item) > > > > print root.toxml() > > > > file = open(r'd:\1.xml', 'w') > > > > import codecs > > > > writer = codecs.lookup ('utf-8')[3](file) > > > > #ÕâÀïºÍÏÂÃæ»»³É'utf-16'¾Í»á²úÉúÂÒÂëÎļþ¡£ > > > > dom.writexml(writer=writer, addindent='\t', newl='\n', > > encoding='utf-8') > > > > writer.close() > > > > > > > > µ«ÎÒ²é×ÊÁÏ˵£¬XML´¦ÀíʱÄÚ²¿Ê¹ÓÃUnicode±àÂ룬°´Ëµutf-16Ó¦¸ÃûÓÐÎÊÌâµÄѽ£¬ÎªÊ²Ã´£¿£¿ > > > > > > > > > -- > > > I like python! > > > UliPad <>: http://code.google.com/p/ulipad/ > > > meide <>: http://code.google.com/p/meide/ > > > My Blog: http://www.donews.net/limodou > > > _______________________________________________ > > > python-chinese > > > Post: send python-chinese在lists.python.cn > > > Subscribe: send subscribe to python-chinese-request在lists.python.cn > > > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > > > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > -- > > wayne > > > > _______________________________________________ > > python-chinese > > Post: send python-chinese在lists.python.cn > > Subscribe: send subscribe to python-chinese-request在lists.python.cn > > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20071019/c4dd9fca/attachment.html
2007年10月19日 星期五 14:17
把addindent='\t', newl='\n'去掉也没有什么影响吧?只是用文本编辑器打开没有树形结构而已 在07-10-19,@@ <askfor at gmail.com> 写道: > > 那是不是得 '\t'.decode('utf-8').encode('utf-16') > 如果你的py文件是用utf-8保存的 > > > On 10/19/07, ??? ?? <clfff.peter at gmail.com> wrote: > > > > 8错,就是addindent='\t', newl='\n'的问题,可不知道有没有什么办法解决的? > > 实在不行就只能用'utf-8'了。 > > > > > > 在07-10-19,Wayne <moonbingbing at gmail.com > 写道: > > > > > > 用winhex打开生成的xml文件,看一下十六进制就清楚了。 > > > 是dom.writexml(writer=writer, addindent='\t', newl='\n', > > > encoding='utf-8')中'\t'和'\n'的问题。 > > > > > > 在 07-10-19,limodou< limodou at gmail.com> 写道: > > > > xml文件中第一行应该是: > > > > > > > > > > > > > > > > 如果是utf-16,改一下试试看, 同时你的整个文件要保存为utf-16吧.不过我没有看到过有人使用utf-16来处理xml > > > > > > > > On 10/19/07, ??? ?? < clfff.peter at gmail.com> wrote: > > > > > 我有一个程序想要写xml文件,当以utf-8来写时是好的,但以utf-16写之后,打开就存在乱码。 > > > > > 代码如下: > > > > > import sys > > > > > > > > > > > > > > > import xml.dom.minidom > > > > > > > > > > impl = xml.dom.minidom.getDOMImplementation() > > > > > dom = impl.createDocument(None, 'catalog', None) > > > > > root = dom.documentElement > > > > > text=dom.createTextNode('test') > > > > > item = dom.createElement('caption') > > > > > item.appendChild(text) > > > > > item.setAttribute('id', 'id value') > > > > > root.appendChild(item) > > > > > print root.toxml() > > > > > file = open(r'd:\1.xml', 'w') > > > > > import codecs > > > > > writer = codecs.lookup ('utf-8')[3](file) > > > > > #这里和下面换成'utf-16'就会产生乱码文件。 > > > > > dom.writexml(writer=writer, addindent='\t', newl='\n', > > > encoding='utf-8') > > > > > writer.close() > > > > > > > > > > 但我查资料说,XML处理时内部使用Unicode编码,按说utf-16应该没有问题的呀,为什么?? > > > > > > > > > > > > -- > > > > I like python! > > > > UliPad <>: http://code.google.com/p/ulipad/ > > > > meide <>: http://code.google.com/p/meide/ > > > > My Blog: http://www.donews.net/limodou > > > > _______________________________________________ > > > > 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 > > > > > > > > > -- > > > wayne > > > > > > _______________________________________________ > > > 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 > > > > > > > > > _______________________________________________ > > 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 > > > > > _______________________________________________ > 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 > -- wayne http://blog.csdn.net/wayne92 Kingsoft(Zhuhai) -------------- next part -------------- An HTML attachment was scrubbed... URL: http://python.cn/pipermail/python-chinese/attachments/20071019/033475d6/attachment.htm
2007年10月19日 星期五 14:22
同意楼上的说法, On 10/19/07, Wayne <moonbingbing at gmail.com> wrote: > > 把addindent='\t', newl='\n'去掉也没有什么影响吧?只是用文本编辑器打开没有树形结构而已 > > 在07-10-19,@@ < askfor at gmail.com> 写道: > > > > 那是不是得 '\t'.decode('utf-8').encode('utf-16') > > 如果你的py文件是用utf-8保存的 > > > > > > On 10/19/07, ??? ?? <clfff.peter at gmail.com > wrote: > > > > > > 8错,就是addindent='\t', newl='\n'的问题,可不知道有没有什么办法解决的? > > > 实在不行就只能用'utf-8'了。 > > > > > > > > > 在07-10-19,Wayne <moonbingbing at gmail.com > 写道: > > > > > > > > 用winhex打开生成的xml文件,看一下十六进制就清楚了。 > > > > 是dom.writexml(writer=writer, addindent='\t', newl='\n', > > > > encoding='utf-8')中'\t'和'\n'的问题。 > > > > > > > > 在 07-10-19,limodou< limodou at gmail.com> 写道: > > > > > xml文件中第一行应该是: > > > > > > > > > > > > > > > > > > > > 如果是utf-16,改一下试试看, 同时你的整个文件要保存为utf-16吧.不过我没有看到过有人使用utf-16来处理xml > > > > > > > > > > On 10/19/07, ??? ?? < clfff.peter at gmail.com> wrote: > > > > > > 我有一个程序想要写xml文件,当以utf-8来写时是好的,但以utf-16写之后,打开就存在乱码。 > > > > > > 代码如下: > > > > > > import sys > > > > > > > > > > > > > > > > > > import xml.dom.minidom > > > > > > > > > > > > impl = xml.dom.minidom.getDOMImplementation() > > > > > > dom = impl.createDocument(None, 'catalog', None) > > > > > > root = dom.documentElement > > > > > > text=dom.createTextNode('test') > > > > > > item = dom.createElement('caption') > > > > > > item.appendChild(text) > > > > > > item.setAttribute('id', 'id value') > > > > > > root.appendChild(item) > > > > > > print root.toxml() > > > > > > file = open(r'd:\1.xml', 'w') > > > > > > import codecs > > > > > > writer = codecs.lookup ('utf-8')[3](file) > > > > > > #这里和下面换成'utf-16'就会产生乱码文件。 > > > > > > dom.writexml(writer=writer, addindent='\t', newl='\n', > > > > encoding='utf-8') > > > > > > writer.close() > > > > > > > > > > > > 但我查资料说,XML处理时内部使用Unicode编码,按说utf-16应该没有问题的呀,为什么?? > > > > > > > > > > > > > > > -- > > > > > I like python! > > > > > UliPad <>: http://code.google.com/p/ulipad/ > > > > > meide <>: http://code.google.com/p/meide/ > > > > > My Blog: http://www.donews.net/limodou > > > > > _______________________________________________ > > > > > 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 > > > > > > > > > > > > -- > > > > wayne > > > > > > > > _______________________________________________ > > > > 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 > > > > > > > > > > > > > _______________________________________________ > > > 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 > > > > > > > > > _______________________________________________ > > 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 > > > > > > -- > wayne > http://blog.csdn.net/wayne92 > Kingsoft(Zhuhai) > _______________________________________________ > 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 > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://python.cn/pipermail/python-chinese/attachments/20071019/406b02f5/attachment.html
Zeuux © 2025
京ICP备05028076号