gpt4 book ai didi

python - 在 python 中使用 notify-send 显示多行通知

转载 作者:太空狗 更新时间:2023-10-29 11:18:00 27 4
gpt4 key购买 nike

我有来自 api 的天气 (json) 数据。我只想显示我放入字典的一部分:

vals = {
'temperature': 34.41,
'summary': 'Clear',
'ozone': 249.95,
'humidity': 0.32,
'precipType': 'rain',
'pressure': 1010.05,
'dewPoint': 15.31,
'time': 1456393033,
'windSpeed': 2.5,
'apparentTemperature': 34.23,
'icon': 'clear-day',
'windBearing': 96,
'precipProbability': 0.01,
'cloudCover': 0.17,
'precipIntensity': 0.0203
}

我使用此代码以“键:值”的格式显示通知。代码如下:

for i in sorted(vals.keys()):

if i == 'time':
vals[i] = datetime.datetime.fromtimestamp(int(vals[i])) #unix timestamp to readble format

if i == 'summary':
continue #Put it in the notif's title not body
msg = msg + str(i).strip() + ':\ ' +str(vals[i]).strip() + '\n'

msg = 'notify-send -u critical ' + vals['summary'] + ' ' + msg
os.system(msg)

输出显示带有标题的通知(在本例中为摘要 -“清除”)和第一个键值对,即表观温度:34.23,然后终端显示以下错误:

sh: 2: cloudCover: 0.17: not found
sh: 3: dewPoint: 15.31: not found
sh: 4: humidity: 0.32: not found
sh: 5: icon: clear-day: not found
sh: 6: ozone: 249.95: not found
sh: 7: precipIntensity: 0.0203: not found
sh: 8: precipProbability: 0.01: not found
sh: 9: precipType: rain: not found
sh: 10: pressure: 1010.05: not found
sh: 11: temperature: 34.41: not found
sh: 12: time: 2016-02-25: not found
sh: 13: windBearing: 96: not found
sh: 14: windSpeed: 2.5: not found

错误是什么,我该如何纠正?

最佳答案

错误在 msg = 'notify-send -u critical ' + vals['summary'] + ' ' + msg 中。您的 sh 认为参数 vals['summary']msg 是由 sh 执行的命令。这是因为您的 msg 输出中有空格(我不是在告诉您与您的 msg 变量的相同名称相关的混淆)。

您可以在输出数据中使用引号 (\") 来避免它。因此 msg 可以看起来像

msg = 'notify-send -u critical \"%s %s\"' %% (vals['summary'], msg)

msg = 'notify-send -u critical ' + '\"' + vals['summary'] + ' ' + msg + '\"'

UPD我之前不明白主要问题,所以:

在脚本中使用多行 notify-send 有一些问题。我认为最简单的方法是在脚本中使用 echo -e,例如:

notify-send "Title" "$(echo -e "This is the first line.\nAnd this is the second.")"

你可以尝试在你的脚本中使用这个想法,但是你必须对引号和控制字符做一些技巧。

关于python - 在 python 中使用 notify-send 显示多行通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35628702/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com