Python-yaml:yaml.reader.ReaderError:不可接受的字符

问题描述 投票:0回答:1

我正在使用invoice2data库处理发票解析。此库在YAML中具有预定义模板,用于解析发票。但是当我运行示例时,它给了我所有模板的YAML解析错误

运行方式为:

invoice2data --input-reader tesseract FlipkartInvoice.pdf

例外:

Traceback (most recent call last):
File "/home/webwerks/.local/bin/invoice2data", line 10, in <module>
sys.exit(main())
File "/home/webwerks/.local/lib/python3.5/site-packages/invoice2data/main.py", line 191, in main
templates += read_templates()
File "/home/webwerks/.local/lib/python3.5/site-packages/invoice2data/extract/loader.py", line 88, in read_templates
tpl = ordered_load(template_file.read())
File "/home/webwerks/.local/lib/python3.5/site-packages/invoice2data/extract/loader.py", line 36, in ordered_load
return yaml.load(stream, OrderedLoader)
File "/usr/local/lib/python3.5/dist-packages/yaml/__init__.py", line 112, in load
loader = Loader(stream)
File "/usr/local/lib/python3.5/dist-packages/yaml/loader.py", line 44, in __init__
Reader.__init__(self, stream)
File "/usr/local/lib/python3.5/dist-packages/yaml/reader.py", line 74, in __init__
self.check_printable(stream)
File "/usr/local/lib/python3.5/dist-packages/yaml/reader.py", line 144, in check_printable
'unicode', "special characters are not allowed")
yaml.reader.ReaderError: unacceptable character #x0082: special characters are not allowed
in "<unicode string>", position 312

最后一行说:

File "/usr/local/lib/python3.5/dist-packages/yaml/reader.py", line 144, in check_printable
'unicode', "special characters are not allowed")
yaml.reader.ReaderError: unacceptable character #x0082: special characters are not allowed
in "<unicode string>", position 312

我检查了模板。所有都以UTF-8格式有效。 问题似乎与python-yaml包有关。有人遇到过这个问题吗?

python yaml invoice app.yaml
1个回答
3
投票

您的输入是有效的UTF-8是无关紧要的,因为YAML源应该只接受Unicode代码点的子集(独立于UTF-8或其他一些编码)。

特别是它只支持Unicode的可打印子集和旧的YAML 1.1 specification(PyYAML支持的那个),用以下内容详细说明:

允许的字符范围明确排除代理块#xD800-#xDFFF,DEL#x7F,C0控制块#x0-#x1F(#x9,#xA和#xD除外),C1控制块#x80-# x9F,#xFFFE和#xFFFF。必须使用转义序列呈现任何此类字符。

因此,不可打印的“BREAK PERMITTED HERE”代码点,0x0082显然是不被允许的(并且不是PyYAML应该允许的事情之一,但不是这样)。

© www.soinside.com 2019 - 2024. All rights reserved.