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

Created on 29 Jan 2018  路  3Comments  路  Source: yaml/pyyaml

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>

Most helpful comment

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

All 3 comments

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

rt mode are not needed explicitly as they are the default options.
https://docs.python.org/3/library/functions.html#open

@Felix-neko if your question is not answered by @TormodLandet then please reopen.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

prometheanfire picture prometheanfire  路  6Comments

itamarst picture itamarst  路  3Comments

Jwomers picture Jwomers  路  10Comments

sigmavirus24 picture sigmavirus24  路  8Comments

phihag picture phihag  路  6Comments