Python 由于外来字符,查找文件时出现问题

Python 由于外来字符,查找文件时出现问题,python,xml,unicode,itunes,python-3.5,Python,Xml,Unicode,Itunes,Python 3.5,这是我第一次在这里写作,所以我希望我做的一切都很好。 我在Win10上使用Python3.5,我正在尝试将音乐从Itunes同步到我的Android设备。基本上,我正在阅读Itunes库XML文件,并获取所有文件的位置,以便我可以将它们复制/粘贴到手机中,但我对包含外来字符的歌曲有问题 import getpass import re import os from urllib.parse import unquote user = getpass.getuser() ITUNES_LIB_P

这是我第一次在这里写作,所以我希望我做的一切都很好。 我在Win10上使用Python3.5,我正在尝试将音乐从Itunes同步到我的Android设备。基本上,我正在阅读Itunes库XML文件,并获取所有文件的位置,以便我可以将它们复制/粘贴到手机中,但我对包含外来字符的歌曲有问题

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()

我应该如何处理这个问题&;在文件名?

中,代码中存在两个相关问题,例如,未转义的xml字符引用、使用导致的硬编码字符编码。要解决这些问题,请使用xml解析器,如或使用更具体的语法分析器。

也许您可以再次使用replace使all&;s进入&or像答案一样使用html库谢谢@RNar,它解决了这个问题!我想知道是否有办法避免utf8编码避免读写…&38;amp;是一个符号,逃过两次。这与任何字符编码无关。我必须通过将utf8设置为编码来克服UnicodeError,这会更改字符串并导致文件路径出错,我想知道是否有任何方法可以在不编码为utf8的情况下使用这些路径@罗兰
Issue locating file C:\Users\Dymy\Desktop\Media\Norin &#38;amp; Rad - Bird Is The Word.mp3