question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

yaml.load does not support encodings different from current system encoding, cannot you add it?

See original GitHub issue

Hi folks!

We try to use PyYaml in Windows with UTF-8 yaml files. Alas, yaml.load raises an error: it does not support encoding different from system one (in Windows it is CP-1251). Can you add such a feature to manually set the encoding in which the yaml file is?

The traceback, if needed:

Traceback (most recent call last):
  File "D:/Projects/bricks2/main.py", line 45, in <module>
    main_wnd.load_components()
  File "D:\Projects\bricks2\bricks\gui\main_wnd.py", line 286, in load_components
    self.registry.load()
  File "D:\Projects\bricks_cli\bricks_cli\registry.py", line 38, in load
    self._load_config(root_node, config)
  File "D:\Projects\bricks_cli\bricks_cli\registry.py", line 44, in _load_config
    config_obj = yaml.load(open(config, 'r'))
  File "C:\Python35\lib\site-packages\yaml\__init__.py", line 73, in load
    loader = Loader(stream)
  File "C:\Python35\lib\site-packages\yaml\loader.py", line 24, in __init__
    Reader.__init__(self, stream)
  File "C:\Python35\lib\site-packages\yaml\reader.py", line 85, in __init__
    self.determine_encoding()
  File "C:\Python35\lib\site-packages\yaml\reader.py", line 124, in determine_encoding
    self.update_raw()
  File "C:\Python35\lib\site-packages\yaml\reader.py", line 178, in update_raw
    data = self.stream.read(size)
  File "C:\Python35\lib\encodings\cp1251.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x98 in position 2574: character maps to <undefined>

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

21reactions
TormodLandetcommented, Jun 7, 2018

The yaml.load() method takes an open file object. You must set the encoding when you open the file. This does not have anything to do with PyYAML. Your code contains

config_obj = yaml.load(open(config, 'r'))

I would suggest to change this to

with open(config, 'rt', encoding='utf8') as yml:
    config_obj = yaml.load(yml)

PS: I did not test this code, but it (or something close to it) should work on Python3. If you are still on python2 you can import codecs and use codecs.open.

I suggest to close this issue

0reactions
mohamadmansourXcommented, Apr 29, 2022

I would suggest to change this to

with open(config, 'rt', encoding='utf8') as yml:
    config_obj = yaml.load(yml)

Incase of having !!python/tuple in the yaml file, I can’t apply utf-8 encoding anymore.

~\anaconda3\lib\site-packages\yaml\constructor.py in construct_undefined(self, node)
    425 
    426     def construct_undefined(self, node):
--> 427         raise ConstructorError(None, None,
    428                 "could not determine a constructor for the tag %r" % node.tag,
    429                 node.start_mark)

ConstructorError: could not determine a constructor for the tag 'tag:yaml.org,2002:python/tuple'
  in "tmp.yaml", line 4, column 5

Any suggestion!?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set the character encoding in a yaml file - Stack Overflow
A YAML processor must support the UTF-16 and UTF-8 character encodings. If a character stream does not begin with a byte order mark...
Read more >
YAML: The Missing Battery in Python
Note: The documentation for Python's logging framework mentions YAML despite the fact that the language doesn't support YAML natively.
Read more >
PyYAML Documentation
yaml.load detects the encoding by checking the BOM (byte order mark) sequence at the beginning of the string/file. If no BOM is present, ......
Read more >
Core Features - Spring
Here you can learn about the key features that you may want to use and ... If the file has an encoding other...
Read more >
Configuration - Sphinx documentation
Note that the current builder tag is not available in conf.py , as it is created after the ... You can add a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found