将 bash 脚本的输出传递给 python 时出现 Unicode 解码错误
Unicode decode error when passing a bash script's output to python
我正在尝试将 shell 脚本的输出传递到 python,当我在应该 returned 的字符串中没有 unicode 字符时它起作用。
bash获取当前播放音乐的脚本:
player_status=$(playerctl status 2> /dev/null)
if [[ $? -eq 0 ]]; then
metadata="$(playerctl metadata title 2> /dev/null)"
else
metadata="No music playing"
fi
# Foreground color formatting tags are optional
if [[ $player_status = "Playing" ]]; then
echo "$metadata"|cut -c -65 # when playing
elif [[ $player_status = "Paused" ]]; then
echo "$metadata"|cut -c -65 # when paused
else
echo "$metadata"|cut -c -65 # when stopped
fi
使用上面脚本return值的python脚本
def getMusicName():
music_name = os.popen('bash /home/nep/Self\ Script/now-playing.sh').read()
return music_name
错误
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe2 in position 64: invalid continuation byte
最后是它试图解码的音乐名称:
IZANAGI 【 イザナギ 】 ☯ Japanese Trap & Bass Type Beat ☯ Trapanese Hip Hop Mix
作为旁注,这是在 Linux 系统上进行的,但我认为这不会有什么不同,如果我单独 运行 shell 脚本 return是这样的名字:
IZANAGI 【 イザナギ 】 ☯ Japanese Trap & Bass Type Beat �
⏎
为确保元数据使用正确的 UTF-8 编码,您可以使用 iconv -ct UTF-8//TRANSLIT
:
过滤 playerctl
的输出
这是您改进后的脚本:
#!/usr/bin/env bash
if player_status="$(playerctl status 2>/dev/null)"; then
metadata="$(playerctl metadata title 2>/dev/null | iconv -ct UTF-8//TRANSLIT)"
else
metadata="No music playing"
fi
# Foreground color formatting tags are optional
case "$player_status" in
Playing)
printf '%.65s\n' "$metadata" # when playing
;;
Paused)
printf '%.65s\n' "$metadata" # when paused
;;
*)
printf '%.65s\n' "$metadata" # when stopped
;;
esac
我正在尝试将 shell 脚本的输出传递到 python,当我在应该 returned 的字符串中没有 unicode 字符时它起作用。 bash获取当前播放音乐的脚本:
player_status=$(playerctl status 2> /dev/null)
if [[ $? -eq 0 ]]; then
metadata="$(playerctl metadata title 2> /dev/null)"
else
metadata="No music playing"
fi
# Foreground color formatting tags are optional
if [[ $player_status = "Playing" ]]; then
echo "$metadata"|cut -c -65 # when playing
elif [[ $player_status = "Paused" ]]; then
echo "$metadata"|cut -c -65 # when paused
else
echo "$metadata"|cut -c -65 # when stopped
fi
使用上面脚本return值的python脚本
def getMusicName():
music_name = os.popen('bash /home/nep/Self\ Script/now-playing.sh').read()
return music_name
错误
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe2 in position 64: invalid continuation byte
最后是它试图解码的音乐名称:
IZANAGI 【 イザナギ 】 ☯ Japanese Trap & Bass Type Beat ☯ Trapanese Hip Hop Mix
作为旁注,这是在 Linux 系统上进行的,但我认为这不会有什么不同,如果我单独 运行 shell 脚本 return是这样的名字:
IZANAGI 【 イザナギ 】 ☯ Japanese Trap & Bass Type Beat �
⏎
为确保元数据使用正确的 UTF-8 编码,您可以使用 iconv -ct UTF-8//TRANSLIT
:
playerctl
的输出
这是您改进后的脚本:
#!/usr/bin/env bash
if player_status="$(playerctl status 2>/dev/null)"; then
metadata="$(playerctl metadata title 2>/dev/null | iconv -ct UTF-8//TRANSLIT)"
else
metadata="No music playing"
fi
# Foreground color formatting tags are optional
case "$player_status" in
Playing)
printf '%.65s\n' "$metadata" # when playing
;;
Paused)
printf '%.65s\n' "$metadata" # when paused
;;
*)
printf '%.65s\n' "$metadata" # when stopped
;;
esac