我在 Python 3.x 中写入文件时遇到问题,FOR 中的写入函数是以 utf-8 编码编写捷克符号。我是 Python 新手,但我为“utf-8”编码设置了 IDE 和 .py、.xml 文件,但我不知道为什么输出文件看起来像这样。我的代码:
-*- coding: utf-8 -*-
from lxml import etree
from io import BytesIO
import sys
import codecs
f = open('uzivatelska_prirucka.xml','rb')
fo = open('try.xml','wb',1)
header = '?xml version="1.0" encoding="utf-8"?>\n<root\n'
fo.write(bytes(header,'UTF-8'))
some_file_like_object = f
tree = etree.parse(some_file_like_object)
root = tree.getroot()
node = tree.xpath('/prirucka/body/p');
for a in node:
for b in a.getiterator():
if not (b.find('r') is None):
text = etree.tostring(b.find('r'))
fo.write(bytes(str(text),'UTF-8'))
感谢您的帮助和建议