N.Balauro
N.Balauro

Reputation: 151

Unicode Dict Keys Issue

Using python 3.5. From my understand all strings should be unicode by default. Why are these unicode key names being encoded using ascii?

row_map = {
            'α-Pinene': 7,
            'β-Pinene': 8,
            'Terpinolene': 9,
            'Geraniol': 10,
            'α-Terpinene': 11,
            'γ-Terpinene': 12,
            'Camphene': 13,
            'Linalool': 14,
            'd-Limonene': 15,
            'Citral': 16,
            'Myrcene': 17,
            'α-Terpineol': 18,
            'Citronellol': 19,
            'dl-Menthol': 20,
            '1-Borneol': 21,
            '2-Piperidone': 22,
            'β-Caryophyllene': 23,
            'α-Humulene': 24,
            'Caryophyllene Oxide': 5,
        }
        with open("log.txt", "w", encoding="utf-8") as f:
            print(row_map, file=f)
        print(open("log.txt", "rb").read())

Here are the results writing these keys to utf-8 text. log.txt

dict_keys([
    'Terpinolene', 
    'Camphene', 
    'Myrcene', 
    'α-Terpineol', 
    'd-Limonene', 
    '2-Piperidone', 
    'γ-Terpinene', 
    'Geraniol', 
    'Linalool', 
    'α-Humulene', 
    'α-Pinene', 
    'β-Caryophyllene', 
    'β-Pinene', 
    'Caryophyllene Oxide', 
    'Citronellol', 
    '1-Borneol', 
    'Citral', 
    'α-Terpinene', 
    'dl-Menthol'])

EDIT: Here is the actual txt file. So one can verify it is not my viewer.

EDIT #2: Please take a look at this funkiness.

Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:27:37) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> row_map = {
...                 'α-Pinene': 7,
...                 'β-Pinene': 8,
...                 'Terpinolene': 9,
...                 'Geraniol': 10,
...                 'α-Terpinene': 11,
...                 'γ-Terpinene': 12,
...                 'Camphene': 13,
...                 'Linalool': 14,
...                 'd-Limonene': 15,
...                 'Citral': 16,
...                 'Myrcene': 17,
...                 'α-Terpineol': 18,
...                 'Citronellol': 19,
...                 'dl-Menthol': 20,
...                 '1-Borneol': 21,
...                 '2-Piperidone': 22,
...                 'β-Caryophyllene': 23,
...                 'α-Humulene': 24,
...                 'Caryophyllene Oxide': 25,
...             }
>>> row_map
{'Citral': 16, 'd-Limonene': 15, 'Myrcene': 17, 'Camphene': 13, 'ß-Caryophyllene': 23, 'α-Terpinene': 11, 'Linalool': 14, 'α-Humulene': 24, '1-Borneol': 21, 'Citronellol': 19, 'Caryophyllene Oxide': 25, 'α-Terpineol': 18, 'α-Pinene': 7, '2-Piperidone': 22, 'dl-Menthol': 20, 'Terpinolene': 9, 'ß-Pinene': 8, 'Geraniol': 10, '?-Terpinene': 12}
>>> from strains.models import Terpene
>>> Terpene.row_map
{'Citral': 16, 'd-Limonene': 15, 'Myrcene': 17, 'Camphene': 13, 'α-Terpinene': 11, 'Linalool': 14, 'α-Humulene': 24, '1-Borneol': 21, 'Citronellol': 19, 'Caryophyllene Oxide': 25, 'α-Terpineol': 18, 'α-Pinene': 7, '\u03b2-Caryophyllene': 23, '2-Piperidone': 22, 'dl-Menthol': 20, 'Terpinolene': 9, '\u03b3-Terpinene': 12, 'Geraniol': 10, '\u03b2-Pinene': 8}
>>>

I copied this from the problem code and pasted this into the shell. Note how the pasted dict has automatically replaced anything it couldnt encode with a ?.

Notice how the same exact dict that is an attribute of the Terpene obj escaped the unicode!

Here is the Terpene Object's row_map

class Terpene(models.Model):

    name = models.CharField(max_length=50, unique=True)
    short_desc = models.CharField(max_length=250, blank=True, null=True)
    long_desc = models.TextField(blank=True, null=True)
    aroma = models.CharField(max_length=250, blank=True, null=True)
    flavor = models.CharField(max_length=250, blank=True, null=True)
    effects = models.CharField(max_length=250, blank=True, null=True)

row_map = {
    'α-Pinene': 7,
    'β-Pinene': 8,
    'Terpinolene': 9,
    'Geraniol': 10,
    'α-Terpinene': 11,
    'γ-Terpinene': 12,
    'Camphene': 13,
    'Linalool': 14,
    'd-Limonene': 15,
    'Citral': 16,
    'Myrcene': 17,
    'α-Terpineol': 18,
    'Citronellol': 19,
    'dl-Menthol': 20,
    '1-Borneol': 21,
    '2-Piperidone': 22,
    'β-Caryophyllene': 23,
    'α-Humulene': 24,
    'Caryophyllene Oxide': 25,
}

EDIT 3:

Here is the binary read from the problem code:

b"{'\xc3\x8e\xc2\xb1-Terpinene': 11, 'Geraniol': 10, '\xc3\x8e\xc2\xb1-Pinene': 7, 'dl-Menthol': 20, 'Myrcene': 17, 'Citral': 16, 'Citronellol': 19, 'Camphene': 13, '\xc3\x8e\xc2\xb3-Terpinene': 12, '\xc3\x8e\xc2\xb1-Terpineol': 18, '1-Borneol': 21, '\xc3\x8e\xc2\xb1-Humulene': 24, '\xc3\x8e\xc2\xb2-Caryophyllene': 23, '\xc3\x8e\xc2\xb2-Pinene': 8, '2-Piperidone': 22, 'Caryophyllene Oxide': 25, 'Linalool': 14, 'Terpinolene': 9, 'd-Limonene': 15}\r\n"

Here is the binary read from the Terpene Object row_map:

b"{'Geraniol': 10, '\xce\xb2-Caryophyllene': 23, '\xce\xb1-Pinene': 7, 'Citral': 16, '\xce\xb3-Terpinene': 12, 'Myrcene': 17, 'Camphene': 13, '\xce\xb1-Terpinene': 11, 'dl-Menthol': 20, '1-Borneol': 21, '\xce\xb1-Humulene': 24, '\xce\xb2-Pinene': 8, 'd-Limonene': 15, 'Citronellol': 19, '2-Piperidone': 22, 'Caryophyllene Oxide': 25, '\xce\xb1-Terpineol': 18, 'Linalool': 14, 'Terpinolene': 9}\r\n"

Upvotes: 1

Views: 246

Answers (1)

Stefan Pochmann
Stefan Pochmann

Reputation: 28636

"Why are these unicode key names being encoded to ascii?"

They aren't. "Encoded to ASCII" doesn't even make sense, and encoding them using ASCII doesn't even work:

>>> 'α-Terpineol'.encode('ascii')
Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    'α-Terpineol'.encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode character '\u03b1' in position 0: ordinal not in range(128)

Looks like after the correct encoding using UTF-8, your file viewer decoded using ISO-8859-1 or so:

>>> 'α-Terpineol'.encode('utf-8').decode('ISO-8859-1')
'α-Terpineol'

Upvotes: 1

Related Questions