0
votes

I have this ini file

[temp]
units = \u00b0 C

If I retrieve the value with ConfigParser, I get the following result

pi@pi-vm1:~ $ python3
Python 3.5.3 (default, Jan 19 2017, 14:11:04)
[GCC 6.3.0 20170118] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from configparser import ConfigParser
>>> parser = ConfigParser()
>>> parser.read('/home/pi/myapp/units.ini', encoding='utf-8')
['/home/pi/myapp/units.ini']
>>> items = parser.items('temp')
>>> print(items[0])
('units', '\\u00b0 C')

This is passed as an API response, and the end result is that the browser renders \u00b0 on the page.

If I put into my code directly:

units = "\u00b0C"

Then this works fine, and is rendered as a degree symbol

So, basically these two lines render a different output.

units = unitsConfig['units']
units = "\u00b0C"

How can I get the config file method to return the same string as if I specified it directly in the code?

1

1 Answers

0
votes

The ConfigParser syntax is not the same as the Python syntax. The .ini file format does not have any escaping mechanism like Python has for strings.

If you want to have a degree symbol in the value, just insert a literal ° into the config file:

units = °C

Then make sure the .ini file is saved with UTF-8, since you read it with this codec later. That should do the trick.