Click here to Skip to main content
15,303,572 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to embedded Chinese names (title, Artist, Year, Genre and Album) in a mp3 file. title names will be taken from a CSV file(which has Chinese data).

The code is working but the when i check the mp3 files details, every thing is shown in '?????' this is happening only when the data is in Chinese. But when the data is in English. its working fine. I have no idea whats going wrong in my code.

Please find the code below.

Python
from argparse import ArgumentParser
from mutagen.easyid3 import EasyID3
from datetime import datetime
from string import Template
import os
import sys
import datetime
import codecs
import string
import shutil
import re
import logging
import io
import eyed3
import id3
import csv


# other
UTF8Writer = codecs.getwriter('utf8')
sys.stdout = UTF8Writer(sys.stdout)

save_stdout = sys.stdout  # save for restore

# default input filenames
filename_mp3_csv = "mp3_ArtTitAlbGenYer.csv"
outfile = open ('mp3_cop.cop', 'wb')

def get_args():
    """parse command line arguments"""
    parser = ArgumentParser()
    parser.add_argument("-t", "--mp3template", help="filename of mp3 file to be used as template",default="template.mp3")
    parser.add_argument("-l", "--language", help="name of language folder e.g. enu", default="enu")
    parser.add_argument("-ic", "--mp3_scv", help="filename of csv file to be used as input", default="mp3_ArtTitAlbGenYer.csv")
    args = parser.parse_args()
    return args


########################################################################################################################
# main program

def something():
    if __name__ == '__main__':
        args = get_args()
        i=0
        while os.path.exists("%s.mp3" % i):
            i+=1
        fh = open("%s.mp3" % i, "w")
        shutil.copy2(args.mp3template, "%s.mp3" %i)
        audio = EasyID3("%s.mp3" %i)

        audio["title"] =Title
        audio["artist"] = Artist
        audio["album"] = Album
        audio["date"] = Year
        audio["genre"] = Genre
        audio["tracknumber"] = u"1"

        
        audio.save()
    return
# read mp3_csv    utf-8-sig ignores BOM \xef\xbb\xbf at begin of file
try:
    file = codecs.open(filename_mp3_csv, "r",encoding='utf-8-sig')
    for line in file:
       # print(' \n '.join(line))

        #print(len(line.rstrip().encode('utf8')))

       item = line.split(';')
       Artist = item[0].rstrip()
       Title = item[1].rstrip()
       Album = item[2].rstrip()
       Genre = item[3].rstrip()
       Year = item[4].rstrip()
       

       print(line.rstrip(';'))

       something()

    file.close()

except IOError as err:  # e.g. file not existing
    mustwritesomethinghere = 0
    print >> sys.stderr, "error: unable to read mandatory inputfile " + filename_mp3_csv


What I have tried:

I have no idea what to do, because the code is working when the CSV file is having English data and but when it has Chinese data it not working
Posted
Updated 11-Nov-16 1:32am
v2
Comments
Nelek 11-Nov-16 8:25am
   
Problems with other languages are often related to Unicode and chars sets... have you checked that?
Richard MacCutchan 11-Nov-16 8:37am
   
There is likely nothing wrong with your code, apart from the fact that you are trying to display Chinese characters in a Western font. you need to set the language or locale to the correct character set in order to display them correctly.
Shaik Izaz 11-Nov-16 9:20am
   
Richard MacCutchan, Can you explain me where do I need to set the language or locale to the correct set of character set in order to display them correctly!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900