python gi.repository Notify和新行\";
我无法在“gi.repository通知”中显示新行。当我在程序中使用字符串常量时,它工作,但当我使用ConfigParser类从配置文件中读取字符串时失败 test.inipython gi.repository Notify和新行\";,python,notifications,configparser,libnotify,Python,Notifications,Configparser,Libnotify,我无法在“gi.repository通知”中显示新行。当我在程序中使用字符串常量时,它工作,但当我使用ConfigParser类从配置文件中读取字符串时失败 test.ini [NOTIFICATIONS] test1 = Hello,\n{username}! test.py: import ConfigParser from gi.repository import Notify # notifyText = "Hello, {username}" - will work data =
[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()
当前程序的输出将为:“你好\n请执行!”但是,如果我在程序中硬编码此字符串(注释行),则它将按应有的方式显示。
\n
当配置解析器读取配置文件时,不会对其进行特殊处理,而是将其解释为LitreReal\n
如果需要换行符,只需继续下一行的选项字符串:
[通知]
test1=你好,
{username}!
以空格开头的每一行都被视为前一行的延续,空格将被删除,但换行符将保留:
>>> print(cfg.get('NOTIFICATIONS', 'test1'))
Hello,
{username}!
>>>
哦,哇。这是如此简单明了。非常感谢你。