Search code examples
pythonmutagen

How to handle Japanese characters?


I have an input in Japanese language from other source which is out of my control.

But I get this error:

UnicodeEncodeError: 'charmap' codec can't encode characters in position 15-41: character maps to undefined

Code:

import mutagen

def addTag(fpath, title, albumName):
    audio = mutagen.File(fpath, easy=True)
    audio.add_tags()
    audio['title'] = title
    audio['album'] = albumName
    audio.save(fpath)

# The Code below this comment is out of my control but this is how it is implemented
file = "1.mp3"
title = "We Must Go TV"
album = "アニメ「風が強く吹いている」オリジナルサウンドトラック"
addTag(file, title, album)

Solution

  • Read the documentation: https://docs.python.org/3/howto/unicode.html

    It says how to process and include non-ASCII text in your Python code. Essentially, you use unicode literals to represent a single character. This will print one character:

    ru = u'\u30EB'
    

    You could also try to force the string to be a unicode object in python 2:

    album = u"\uアニメ「風が強く吹いている」オリジナルサウンドトラック"
    

    By default, all strings are already unicode.

    Also check out this informative video: https://www.youtube.com/watch?v=oEbNWXhS_mk