Python 使用 utf-8 字符集 systemd 控制 mpd 的脚本不起作用

Python script controlling mpd with utf-8 charset systemd doesnt work

我在 Raspberry Pi 上安装了 MPD,并编写了一个 Python 脚本来在扫描 RFID 卡时播放播放列表。

我的问题是当我在控制台中调用脚本时脚本运行正常。它播放每个播放列表。但是当它作为一项服务启动时,我只能播放没有 äöüß utf8-charset 的播放列表。

我现在的问题是如何使脚本作为服务用于 utf-8 播放列表?

这是我的脚本:

#!/usr/bin/python
# coding=utf-8
import io
import os
import serial
from mpd import MPDClient

with io.open("/var/lib/mpd/mnt/playlists/liste.txt", "r", encoding="utf8") as f:
        list = f.read()
list = list.strip()
list = list.replace("\n","")
list = list[:-1].split(";")

while True:
        ser = serial.Serial("/dev/serial0", baudrate=9600)
        rfid = ser.read(14)

        try:
                tag = { "raw" : rfid,
                        "mfr" : int(rfid[1:5], 16),
                        "id" : int(rfid[5:11], 16),
                        "chk" : int(rfid[11:13], 16)}

                print "Read Card : %s" % tag['id']

                cardnr = str(tag['id'])
                listid = list.index(cardnr)

                print(cardnr)

                plist = list[listid-1]

                print(plist)

                client = MPDClient()
                client.timeout = 10
                client.idletimeout = None
                client.connect("localhost", 6600)
                client.update()

                print "Connected..."

                client.clear()
                print "cleared"
                client.load(plist)
                print(client.playlist())
                print "Playlist geladen"
                client.play(0)
                print "Abspielen"
                client.close()
                client.disconnect()

        except:
                print "error"

系统服务:

[Unit]
Description=Listen to RFID-reader and play mopidy playlists
Requires=mpd.service networking.service
After=mpd.service networking.service

[Service]
Type=idle
Restart=always
ExecStart=/usr/bin/python /var/lib/mpd/mnt/scripts/play.py

[Install]
WantedBy=multi-user.target

语言环境的输出:

LANG=de_DE.UTF-8
LANGUAGE=de_DE.UTF-8
LC_CTYPE="de_DE.UTF-8"
LC_NUMERIC="de_DE.UTF-8"
LC_TIME="de_DE.UTF-8"
LC_COLLATE="de_DE.UTF-8"
LC_MONETARY="de_DE.UTF-8"
LC_MESSAGES="de_DE.UTF-8"
LC_PAPER="de_DE.UTF-8"
LC_NAME="de_DE.UTF-8"
LC_ADDRESS="de_DE.UTF-8"
LC_TELEPHONE="de_DE.UTF-8"
LC_MEASUREMENT="de_DE.UTF-8"
LC_IDENTIFICATION="de_DE.UTF-8"
LC_ALL=

运行 脚本 ExecStart=/usr/bin/**python3** /var/lib/mpd/mnt/scripts/play.py 有帮助。