我正在使用invoice2data 库进行发票解析。该库在 YAML 中预定义了模板,用于解析发票。但是当我运行示例时,它给我所有模板的 YAML 解析错误
运行它:
invoice2data --input-reader tesseract FlipkartInvoice.pdf
异常(exception):
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 包上。有人遇到过这个问题吗?
最佳答案
您的输入是否有效 UTF-8 无关紧要,因为 YAML 源应该只接受 Unicode 代码点的子集(独立于 UTF-8 或其他某种编码)。
特别是它只支持 Unicode 的可打印子集和旧版本 YAML 1.1 specification ,PyYAML 支持的那个,详细说明了:
The allowed character range explicitly excludes the surrogate block #xD800-#xDFFF, DEL #x7F, the C0 control block #x0-#x1F (except for #x9, #xA, and #xD), the C1 control block #x80-#x9F, #xFFFE, and #xFFFF. Any such characters must be presented using escape sequences.
因此,不可打印的“BREAK PERMITTED HERE”代码点 0x0082
显然是不允许的(并且不是 PyYAML 应该允许但不允许的事情之一)。
关于Python-yaml : yaml. reader.ReaderError: Not Acceptable 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55902201/