0

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?

wcndave
  • 306
  • 2
  • 12

1 Answers1

0

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.

lenz
  • 5,658
  • 5
  • 24
  • 44
  • Thanks, however when I paste that into the file I get units = \260C The file is utf-8 – wcndave Sep 04 '18 at 16:51
  • The config file also won't accept the % sign. I would like to keep all cosmetic config out of code, and in ini files, however it appears to be a struggle. If I put the HTML code in there, eg %deg; the json returned messes that up, however is that an easier problem to address? – wcndave Sep 04 '18 at 16:53
  • What do you mean you get "units = \260C"? Does the editor insert this when pasting? Or is this the output of your Python program? When you open the ini file in Python in binary mode, what does the line look like? – lenz Sep 04 '18 at 18:34
  • As for the percent sign, it might be interfering with the interpolation (resolving references to other values in the config, see the [docs](https://docs.python.org/3.4/library/configparser.html#interpolation-of-values)). Try setting `interpolation=None` in the `ConfigParser()` constructor. – lenz Sep 04 '18 at 18:38
  • I don't understand the HTML/JSON thing, sounds complicated. Maybe you should consider replacing the ini config with settings written in JSON or YAML. These allow for specifying arbitrary symbols using ASCII characters. – lenz Sep 04 '18 at 18:41
  • This is all passed back via an Ajax JSON call to JavaScript which renders it to screen. When I put ° in the JSON, it ended up being rendered as that on screen. When I put \u values in JSON, they ended up as the special char on screen. However I then abstracted all labels and furniture out from python code into config.ini files that are easy for anyone to edit. However this then renders the data on screen as \ufoo. The \260 is what I see in putty when pasting into utf-8 file. When I save it then warns: utf-8-unix cannot encode these: \260 – wcndave Sep 05 '18 at 06:44
  • Sounds like a putty problem then. Maybe you should edit the ini file locally and then upload it through putty. – lenz Sep 05 '18 at 06:55