由于外来字符问题定位文件

Issue locating files due to foreign characters

这是我第一次在这里写作,所以我希望我做的一切都很好。 我在 Win10 上使用 python 3.5,我正在尝试 "sync" 音乐从 Itunes 到我的 Android 设备。基本上,我正在读取 Itunes Library XML 文件并获取所有文件位置(这样我就可以 copy/paste 将它们放入我的 phone 中)但是我对包含外来字符的歌曲有疑问。

import getpass
import re
import os
from urllib.parse import unquote

user = getpass.getuser()
ITUNES_LIB_PATH = "C:\Users\%s\Music\Itunes\iTunes Music Library.xml" % user
ITUNES_SONGS_FILE = "ya.txt"


def write(file, what, newline=True):
    with open(file, 'a', encoding="utf8") as f:
        if  not os.path.isfile(what):
            print("Issue locating file %s\n" % what)
        if newline:
            what+"\n"
        f.write(what)


def get_songs(file=ITUNES_LIB_PATH):
    with open(file, 'r', encoding="utf8") as f:
        f = f.read()
        songs_location = re.findall("<key>Location</key><string>file://localhost/(.*?)</string>", f)
        for song in songs_location:
            song = unquote(song.replace("/", '\'))
            write(ITUNES_SONGS_FILE, song)


get_songs()

输出:

Issue locating file C:\Users\Dymy\Desktop\Media\Norin &#38;amp; Rad - Bird Is The Word.mp3

我应该如何处理“&”在文件名中?

您的代码中存在一些相关问题,例如未转义的 xml 字符引用、使用 regular expressions to parse xml. To fix them, use xml parser such as xml.etree.ElementTree or use a more specific pyitunes library (I haven't tried it).

导致的硬编码字符编码