Python 在数据库中存储重音符号和其他字符

Python 在数据库中存储重音符号和其他字符,python,unicode,encoding,Python,Unicode,Encoding,我正在使用一个sqlite3数据库,从MP3中选择使用诱变剂获得的某些ID3信息,并将其存储起来。当歌曲包含重音或其他“外来”字符时,如果我只是尝试将它们存储为常规Python字符串,则会出现以下错误: sqlite3.ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory =

我正在使用一个
sqlite3
数据库,从MP3中选择使用
诱变剂
获得的某些ID3信息,并将其存储起来。当歌曲包含重音或其他“外来”字符时,如果我只是尝试将它们存储为常规Python字符串,则会出现以下错误:

sqlite3.ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory
that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended     
that you instead just switch your application to Unicode strings.
因此,我将要存储在数据库中的所有字符串编码为unicode:

            try: # store as unkown if no ID3 info
                songtitle = unicode(audio["TIT2"].__str__(), errors="replace")
            except KeyError:
                songtitle = "Unknown"

            try:
                artist = unicode(audio["TPE1"].__str__(), errors="replace")
            except KeyError:
                artist = "Unknown"

            try:
                album = unicode(audio["TALB"].__str__(), errors="replace")
            except KeyError:
                album = "Unknown"
这将消除所有错误,并允许成功填充数据库。但是,它仍然不显示重音和其他字符,通常用问号、空格或其他垃圾字符替换它们


我假设我需要指定某种编码,但我不确定如何在不破坏与英语编码的兼容性的情况下做到这一点。我相信你可以告诉我,我的编码经验很少

阅读以了解编码是什么。您的数据已经编码,您需要将其解码为Unicode。请参阅,了解具体使用哪种编解码器的信息。你是说当我调用诱变剂的ID3标记时,它们已经编码了,还是在我对它们执行
unicode
操作时已经编码了?他说,我也是,你应该读那篇文章。正确的。现在。joelonsoftware.com/articles/Unicode.html-只要去那里就可以了。看看诱变剂源代码,它似乎已经为您提供了Unicode数据。您不需要再次将值转换为unicode。