<bdo id='5Jz1s'></bdo><ul id='5Jz1s'></ul>
    1. <small id='5Jz1s'></small><noframes id='5Jz1s'>

      1. <legend id='5Jz1s'><style id='5Jz1s'><dir id='5Jz1s'><q id='5Jz1s'></q></dir></style></legend>
      2. <i id='5Jz1s'><tr id='5Jz1s'><dt id='5Jz1s'><q id='5Jz1s'><span id='5Jz1s'><b id='5Jz1s'><form id='5Jz1s'><ins id='5Jz1s'></ins><ul id='5Jz1s'></ul><sub id='5Jz1s'></sub></form><legend id='5Jz1s'></legend><bdo id='5Jz1s'><pre id='5Jz1s'><center id='5Jz1s'></center></pre></bdo></b><th id='5Jz1s'></th></span></q></dt></tr></i><div id='5Jz1s'><tfoot id='5Jz1s'></tfoot><dl id='5Jz1s'><fieldset id='5Jz1s'></fieldset></dl></div>
        <tfoot id='5Jz1s'></tfoot>

        builtins.TypeError:必须是 str,而不是 bytes

        时间:2024-04-20
        <i id='RVxym'><tr id='RVxym'><dt id='RVxym'><q id='RVxym'><span id='RVxym'><b id='RVxym'><form id='RVxym'><ins id='RVxym'></ins><ul id='RVxym'></ul><sub id='RVxym'></sub></form><legend id='RVxym'></legend><bdo id='RVxym'><pre id='RVxym'><center id='RVxym'></center></pre></bdo></b><th id='RVxym'></th></span></q></dt></tr></i><div id='RVxym'><tfoot id='RVxym'></tfoot><dl id='RVxym'><fieldset id='RVxym'></fieldset></dl></div>
        <legend id='RVxym'><style id='RVxym'><dir id='RVxym'><q id='RVxym'></q></dir></style></legend>

          1. <small id='RVxym'></small><noframes id='RVxym'>

          2. <tfoot id='RVxym'></tfoot>
              • <bdo id='RVxym'></bdo><ul id='RVxym'></ul>
                    <tbody id='RVxym'></tbody>
                  本文介绍了builtins.TypeError:必须是 str,而不是 bytes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我已将我的脚本从 Python 2.7 转换为 3.2,但我遇到了一个错误.

                  I've converted my scripts from Python 2.7 to 3.2, and I have a bug.

                  # -*- coding: utf-8 -*-
                  import time
                  from datetime import date
                  from lxml import etree
                  from collections import OrderedDict
                  
                  # Create the root element
                  page = etree.Element('results')
                  
                  # Make a new document tree
                  doc = etree.ElementTree(page)
                  
                  # Add the subelements
                  pageElement = etree.SubElement(page, 'Country',Tim = 'Now', 
                                                        name='Germany', AnotherParameter = 'Bye',
                                                        Code='DE',
                                                        Storage='Basic')
                  pageElement = etree.SubElement(page, 'City', 
                                                        name='Germany',
                                                        Code='PZ',
                                                        Storage='Basic',AnotherParameter = 'Hello')
                  # For multiple multiple attributes, use as shown above
                  
                  # Save to XML file
                  outFile = open('output.xml', 'w')
                  doc.write(outFile) 
                  

                  在最后一行,我得到了这个错误:

                  On the last line, I got this error:

                  builtins.TypeError: must be str, not bytes
                  File "C:PythonExamplesXmlReportGeneratorExample.py", line 29, in <module>
                    doc.write(outFile)
                  File "c:Python32Libsite-packageslxmletree.pyd", line 1853, in lxml.etree._ElementTree.write (src/lxml/lxml.etree.c:44355)
                  File "c:Python32Libsite-packageslxmletree.pyd", line 478, in lxml.etree._tofilelike (src/lxml/lxml.etree.c:90649)
                  File "c:Python32Libsite-packageslxmletree.pyd", line 282, in lxml.etree._ExceptionContext._raise_if_stored (src/lxml/lxml.etree.c:7972)
                  File "c:Python32Libsite-packageslxmletree.pyd", line 378, in lxml.etree._FilelikeWriter.write (src/lxml/lxml.etree.c:89527)
                  

                  我已经安装了 Python 3.2,并且我已经安装了 lxml-2.3.win32-py3.2.exe.

                  I've installed Python 3.2, and I've installed lxml-2.3.win32-py3.2.exe.

                  在 Python 2.7 上它可以工作.

                  On Python 2.7 it works.

                  推荐答案

                  输出文件应该是二进制模式.

                  The outfile should be in binary mode.

                  outFile = open('output.xml', 'wb')
                  

                  这篇关于builtins.TypeError:必须是 str,而不是 bytes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:NameError:Python 3 中未定义全局名称“xrange" 下一篇:'else if' 的正确语法是什么?

                  相关文章

                  <legend id='UYJYV'><style id='UYJYV'><dir id='UYJYV'><q id='UYJYV'></q></dir></style></legend>
                1. <tfoot id='UYJYV'></tfoot>
                    <bdo id='UYJYV'></bdo><ul id='UYJYV'></ul>

                  <small id='UYJYV'></small><noframes id='UYJYV'>

                    1. <i id='UYJYV'><tr id='UYJYV'><dt id='UYJYV'><q id='UYJYV'><span id='UYJYV'><b id='UYJYV'><form id='UYJYV'><ins id='UYJYV'></ins><ul id='UYJYV'></ul><sub id='UYJYV'></sub></form><legend id='UYJYV'></legend><bdo id='UYJYV'><pre id='UYJYV'><center id='UYJYV'></center></pre></bdo></b><th id='UYJYV'></th></span></q></dt></tr></i><div id='UYJYV'><tfoot id='UYJYV'></tfoot><dl id='UYJYV'><fieldset id='UYJYV'></fieldset></dl></div>