Download special characters with PyYaml

I am working on loading a list of emoji characters in a simple python 3.6 script. The YAML structure is essentially as follows:

- 🙂   
- 😁
- 😬

My python script looks like this:

import yaml
f = open('emojis.yml')
EMOJIS = yaml.load(f)
f.close()

I get the following exception:

yaml.reader.ReaderError: unacceptable character #x001d: special characters are not allowed in "emojis.yml", position 2

I saw a parameter allow_unicode=True, but it seems to be available only for yaml.dump. It seems like people had problems with similar problems in Python2, but since all lines must be unicode, it's hard for me to understand why this is not working.

emojis "tag: yaml.org, 2002: str". , -, - , yaml lib emoji . , emoji .

yaml, emojis PyYAML?

+12
2

ruamel.yaml ( : ), PyYAML:

import sys
from ruamel.yaml import YAML

yaml = YAML()

with open('emojis.yml') as fp:
    idx = 0
    for c in fp.read():
        print('{:08x}'.format(ord(c)), end=' ')
        idx += 1
        if idx % 4 == 0:
            print()

with open('emojis.yml') as fp:
    data = yaml.load(fp)
yaml.dump(data, sys.stdout)

:

0000002d 00000020 0001f642 0000000a 
0000002d 00000020 0001f601 0000000a 
0000002d 00000020 0001f62c 0000000a 
['🙂', '😁', '😬']

PyYAML, :

import yaml.reader
import re

yaml.reader.Reader.NON_PRINTABLE = re.compile(
    u'[^\x09\x0A\x0D\x20-\x7E\x85\xA0-\uD7FF\uE000-\uFFFD\U00010000-\U0010FFFF]')

.


0.15.16, ruamel.yaml Unicode , \Uxxxxxxxx ( API .unicode_supplementary allow_unicode).

+6

pyyaml , pyyaml>=5


, pyyaml, - escape-:

$ cat test.yaml
- "\U0001f642"
- "\U0001f601"
- "\U0001f62c"

$ python
...
>>> yaml.load(open('test.yaml'))
['🙂', '😁', '😬']
+4

All Articles