Pregunta

I'm not able to display a new line in 'gi.repository Notification'. It works when i use string constant within a program, but fails when i read string from configuration file using ConfigParser class.

test.ini

[NOTIFICATIONS]
test1 = Hello,\n{username}!

test.py:

import ConfigParser
from gi.repository import Notify

# notifyText = "Hello, {username}" - will work
data = {'username': 'sudo', 'test': 'test'}


if __name__ == '__main__':
    cfg = ConfigParser.ConfigParser()                              
    cfg.read('test.ini')
    notifyText = cfg.get('NOTIFICATIONS', 'test1').format(**data)

    Notify.init('Test')
    notification = Notify.Notification('Test', notifyText)
    notification.show()

The output of current program will be: 'Hello\nsudo!' However, if i hardcode this string (commented line) in my program then it's displayed as it should.

¿Fue útil?

Solución

\n isn't treated specially when a config file is read by a ConfigParser, it's interpreted as a litreral \n.

If you want a newline, just continue the option string on the next line:

[NOTIFICATIONS]
test1 = Hello,
        {username}!

Each line starting with whitespace is treated as continuation of the previous line, the whitespace will be removed, but the newline stays:

>>> print(cfg.get('NOTIFICATIONS', 'test1'))
Hello,
{username}!
>>> 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top