python gi.repository通知和新行“\ n”

时间:2013-10-01 15:08:58

标签: python notifications configparser libnotify

我无法在'gi.repository Notification'中显示新行。它在程序中使用字符串常量时有效,但是当我使用ConfigParser类从配置文件中读取字符串时失败。

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()

当前程序的输出将是:'Hello \ nsudo!'但是,如果我在我的程序中对此字符串(注释行)进行硬编码,那么它将按原样显示。

1 个答案:

答案 0 :(得分:0)

\n读取配置文件时,

ConfigParser未被特别处理,它被解释为一个\n

如果您想要换行,只需在下一行继续选项字符串:

[NOTIFICATIONS]
test1 = Hello,
        {username}!

以空格开头的每一行都被视为前一行的延续,空格将被删除,但换行仍然存在:

>>> print(cfg.get('NOTIFICATIONS', 'test1'))
Hello,
{username}!
>>>