Python3 迁移 xml 写入问题
Python3 migration xml write issue
目前对向 Python3 (3.6.8) 的代码迁移感到沮丧
out_fname 是.cproject 文件(xml 格式)
self.cproject_xml = ET.parse(self.CPROJ_NAME))
with open(out_fname, 'a') as cxml:
cxml.write('<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n')
cxml.write('<?fileVersion 4.0.0?>')
self.cproject_xml.write(cxml,encoding='utf-8')
导致:
File "/home/build/workspace/bismuth_build_nightly_py3@2/venv/lib/python3.6/site-packages/tinlane/cprojecttools.py", line 209, in export_cproject
self.cproject_xml.write(fxml)
snips..
File "/usr/lib64/python3.6/xml/etree/ElementTree.py", line 946, in _serialize_xml
write(_escape_cdata(elem.tail))
TypeError: write() argument must be str, not bytes
我已经尝试了所有不同的方法(小心,打开我的文件时我需要“a”)以使其工作(发布原始 python2 代码,而不是替代代码)。通常我只是在 r,a,w 中放置一个“b”就可以解决问题。不,它不起作用:
(cxml.write('<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n')
TypeError: a bytes-like object is required, not 'str')
即使我转换为字节(我认为是错误的)
要重现的最小示例:
使用以下内容创建 2 个相同的文件(file1、file2):
<note>
<to>minimal</to>
<from>xml</from>
<heading>file</heading>
<body>content</body>
</note>
和运行这个代码块:
import xml.etree.ElementTree as ET
cproject_xml = ET.parse('file1')
fname = 'file2'
with open(fname, 'a') as cxml:
cxml.write('<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n')
cxml.write('<?fileVersion 4.0.0?>')
cproject_xml.write(cxml,encoding='utf-8')
当运行与python2时,file2变为:
<note>
<to>minimal</to>
<from>xml</from>
<heading>file</heading>
<body>content</body>
</note>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?fileVersion 4.0.0?><note>
<to>minimal</to>
<from>xml</from>
<heading>file</heading>
<body>content</body>
</note>
有什么想法吗?
谢谢
我确定我遗漏了一些东西,但是尝试将树 (cproject_xml
) 写入打开的文件句柄 (cxml
) 是没有意义的。
我认为序列化树并直接写入打开的文件会更有意义。
尝试更改:
cproject_xml.write(cxml, encoding='utf-8')
至:
cxml.write(ET.tostring(cproject_xml.getroot()).decode())
目前对向 Python3 (3.6.8) 的代码迁移感到沮丧
out_fname 是.cproject 文件(xml 格式)
self.cproject_xml = ET.parse(self.CPROJ_NAME))
with open(out_fname, 'a') as cxml:
cxml.write('<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n')
cxml.write('<?fileVersion 4.0.0?>')
self.cproject_xml.write(cxml,encoding='utf-8')
导致:
File "/home/build/workspace/bismuth_build_nightly_py3@2/venv/lib/python3.6/site-packages/tinlane/cprojecttools.py", line 209, in export_cproject
self.cproject_xml.write(fxml)
snips..
File "/usr/lib64/python3.6/xml/etree/ElementTree.py", line 946, in _serialize_xml
write(_escape_cdata(elem.tail))
TypeError: write() argument must be str, not bytes
我已经尝试了所有不同的方法(小心,打开我的文件时我需要“a”)以使其工作(发布原始 python2 代码,而不是替代代码)。通常我只是在 r,a,w 中放置一个“b”就可以解决问题。不,它不起作用:
(cxml.write('<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n')
TypeError: a bytes-like object is required, not 'str')
即使我转换为字节(我认为是错误的)
要重现的最小示例: 使用以下内容创建 2 个相同的文件(file1、file2):
<note>
<to>minimal</to>
<from>xml</from>
<heading>file</heading>
<body>content</body>
</note>
和运行这个代码块:
import xml.etree.ElementTree as ET
cproject_xml = ET.parse('file1')
fname = 'file2'
with open(fname, 'a') as cxml:
cxml.write('<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n')
cxml.write('<?fileVersion 4.0.0?>')
cproject_xml.write(cxml,encoding='utf-8')
当运行与python2时,file2变为:
<note>
<to>minimal</to>
<from>xml</from>
<heading>file</heading>
<body>content</body>
</note>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?fileVersion 4.0.0?><note>
<to>minimal</to>
<from>xml</from>
<heading>file</heading>
<body>content</body>
</note>
有什么想法吗? 谢谢
我确定我遗漏了一些东西,但是尝试将树 (cproject_xml
) 写入打开的文件句柄 (cxml
) 是没有意义的。
我认为序列化树并直接写入打开的文件会更有意义。
尝试更改:
cproject_xml.write(cxml, encoding='utf-8')
至:
cxml.write(ET.tostring(cproject_xml.getroot()).decode())