gpt4 book ai didi

python - lxml.etree.XMLSyntaxError : Start tag expected, '<' 未找到,第 1 行,第 1 列

转载 作者:行者123 更新时间:2023-12-05 07:52:22 24 4
gpt4 key购买 nike

这应该很简单,所以不确定为什么标题中出现错误。创建的 SVG 没问题,因为我已手动检查

code to generate svg
....

mod_svg = 'final1.svg'
tree.write(mod_svg)

import cairosvg
fileout = open('finaltree','w')
cairosvg.svg2png(bytestring=mod_svg,write_to=fout)
fileout.close()

我在 pythonanywhere 上使用 python 2.7 以防万一。

完整的回溯

Traceback (most recent call last):
File "/home/drdavepier/mysite/flask_app.py", line 184, in <module>
cairosvg.svg2png(bytestring=mod_svg,write_to=fout)
File "/usr/local/lib/python2.7/dist-packages/cairosvg/__init__.py", line 43, in <lambda>
surface_type.convert(*args, **kwargs))(_surface_type)
File "/usr/local/lib/python2.7/dist-packages/cairosvg/surface/__init__.py", line 82, in convert
tree = Tree(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/cairosvg/parser.py", line 270, in __init__
tree = ElementTree.fromstring(bytestring)
File "lxml.etree.pyx", line 3032, in lxml.etree.fromstring (src/lxml/lxml.etree.c:68121)
File "parser.pxi", line 1786, in lxml.etree._parseMemoryDocument (src/lxml/lxml.etree.c:102470)
File "parser.pxi", line 1674, in lxml.etree._parseDoc (src/lxml/lxml.etree.c:101299)
File "parser.pxi", line 1074, in lxml.etree._BaseParser._parseDoc (src/lxml/lxml.etree.c:96481)
File "parser.pxi", line 582, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:91290)
File "parser.pxi", line 683, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:92476)
File "parser.pxi", line 622, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:91772)
lxml.etree.XMLSyntaxError: Start tag expected, '<' not found, line 1, column 1

相关SVG的开始行

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Created with matplotlib (http://matplotlib.org/) --><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="432pt" version="1.1" viewBox="0 0 576 432" width="576pt">

树的初始化:

filename = "/path/to/file.svg"
with open( filename, 'r') as infile:
tree = etree.parse( infile )

最佳答案

你的文字中有特殊字符

def transference(value):
value = value.replace("<","&lt;")
value = value.replace(">", "&gt;")
value = value.replace("&", "&amp;")
value = value.replace(r'"', "&quot;")
value = value.replace(r"'", "&apos;")
value = value.replace("×", "&times;")
value = value.replace("÷", "&divide;")
return value

关于python - lxml.etree.XMLSyntaxError : Start tag expected, '<' 未找到,第 1 行,第 1 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33792558/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com