Rajesh Gosemath
Rajesh Gosemath

Reputation: 1872

Python-yaml : yaml.reader.ReaderError: unacceptable character

I am working on invoice parsing using invoice2data library. This library has predefined templates in YAML for parsing invoices. But when I am running the samples it is giving me YAML parsing error for all the templates

Runnung it as :

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

The last line says:

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

I have checked the templates. All are valid in the UTF-8 format.
The issue seems to be with the python-yaml package.Anyone has encountered this issue?

Upvotes: 2

Views: 13307

Answers (2)

He David
He David

Reputation: 13

It could be that your LANG environment variable is incorrectly set. The default LANG environment variable is en_US.ASCII for some OS, so a valid character 0x82 in UTF-8 is not recognized as a valid character in ASCII, which would cause this issue.

The solution is simply setting LANG to en_US.UTF-8

Upvotes: 0

Anthon
Anthon

Reputation: 76882

That your input is valid UTF-8 is irrelevant, as YAML source should only accept a subset of Unicode code points (independent of UTF-8 or some other encoding).

In particular it does only support the printable subset of Unicode and the old YAML 1.1 specification, the one that PyYAML supports, elaborates that with:

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.

So the non-printable "BREAK PERMITTED HERE" codepoint, 0x0082 is clearly disallowed (and is not one of those things PyYAML should allow, but doesn't).

Upvotes: 5

Related Questions