写入文件时Python unicode到ASCII的转换

写入文件时Python unicode到ASCII的转换,python,unicode,converter,non-ascii-characters,Python,Unicode,Converter,Non Ascii Characters,我正在写一个脚本,通过一个音乐库打印一个带有专辑名date…的.txt文件,然后对曲目进行编号。在导入的标记(如unicode)达到(-)之前,它一直工作正常。然后我得到一个: File "C:/Users/Brian/Python files/CDinfoRF2.py", line 51, in music_album_info mfile.write(header) UnicodeEncodeError: 'ascii' codec can't encode character

我正在写一个脚本,通过一个音乐库打印一个带有专辑名date…的.txt文件,然后对曲目进行编号。在导入的标记(如unicode)达到(-)之前,它一直工作正常。然后我得到一个:

  File "C:/Users/Brian/Python files/CDinfoRF2.py", line 51, in music_album_info
    mfile.write(header)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 18: ordinal not in range(128).
守则:

#!usr/bin/env python
__author__ = 'Brian Kane'

"""This scripts takes a path argument to the root directory of the music files (mp3 here) and
   writes various information about the disc to a text file which is named by the artist"""

import io
import os
from os.path import *
import string
from mutagen.mp3 import MP3
from mutagen.easyid3 import EasyID3
import unicodedata

def music_album_info(path):

    count = 0

    for root, dirs, files in os.walk(path):                             # walks through the data tree to get files
        for name in files:
            path_name = root +'\\' + name
            extension = os.path.splitext(name)[1][1:].strip().lower()   # gets the file extension
            if extension == 'mp3':
                artist = EasyID3(path_name)['artist'][0]                # gets unicode artist name
                # print artist
                track_num = EasyID3(path_name)['tracknumber']           # gets unicode tracknumer
                album = EasyID3(path_name)['album'][0]
                # print album
                # print type(album)
                album.encode('utf-8')
                # length = EasyID3(path_name)['length'][0]
                print album
                date = EasyID3(path_name)['date'][0]
                # print date
                track_name = EasyID3(path_name)['title']
                # print track_name
                header = '\n' + 'Title:  ' + album + '        Released ' + date + '\n\n'
                # if EasyID3(path_name)['discnumber'] != []:
                #     disc = EasyID3(path_name)['discnumber']
                #     header = '\n' + 'Title:  ' + album + 'Disc: ' + disc + '        Released ' + date + '\n\n'
                file_name = artist + '.txt'                             # used to name file to be written to = artist
                mp3info = EasyID3(path_name)


                # print mp3info.items()
                # print 'header in',header
                if count < 1:                                           # used to not write header over each track
                    # print 'header = ', header
                    mfile = open(file_name, 'a')
                    mfile.write(header)
                    count += 1

                if extension == 'mp3':                                  # avoid album art and errors which occur

                    track_info = '\t' +  track_num[0] + '. ' + track_name[0] + '\n'
                    # if EasyID3(path_name)'length' in mp3info.items():
                    #     length = EasyID3(path_name)['length'][0]
                    #     print length
                    #     track_info = '\t' +  track_num[0] + '. ' + track_name[0] + '   ' + length[0] + '\n'
                    # print 'track_info = ',track_info
                    mfile.write(track_info)
        count = 0                                                       # reset for next artist - new .txt file

    mfile.close()

path = 'C:\\test'                       # this is a test path
# path = raw_input('Enter the path to the music file:\n>')

music_album_info(path)
#!usr/bin/env python
__作者:布莱恩·凯恩
“”“此脚本将path参数带到音乐文件(此处为mp3)的根目录,并
将有关光盘的各种信息写入由艺术家命名的文本文件“”
输入io
导入操作系统
从os.path导入*
导入字符串
从诱变剂.mp3导入mp3
来自诱变剂easyid3进口easyid3
导入Unicode数据
def music_唱片集_信息(路径):
计数=0
对于os.walk(path)中的root、dirs和文件:#遍历数据树以获取文件
对于文件中的名称:
路径\名称=根+'\\'+名称
extension=os.path.splitext(name)[1][1:].strip().lower()#获取文件扩展名
如果扩展名==“mp3”:
artist=EasyID3(路径名称)['artist'][0]#获取unicode艺术家名称
#印刷艺术家
track_num=EasyID3(路径名称)['tracknumber']#获取unicode tracknumer
相册=EasyID3(路径名称)[“相册”][0]
#印刷相册
#打印类型(相册)
唱片集编码('utf-8')
#length=EasyID3(路径名称)['length'][0]
印刷相册
date=EasyID3(路径名称)['date'][0]
#打印日期
曲目名称=EasyID3(路径名称)['title']
#打印曲目名称
标题='\n'+'标题:'+album+'发行'+date+'\n\n'
#如果EasyID3(路径名称)['discnumber']!=[]:
#disc=EasyID3(路径名称)['discnumber']
#标题='\n'+'标题:'+album+'光盘:'+Disc+'发行'+date+'\n\n'
file_name=artist+'.txt'#用于命名要写入的文件=artist
mp3info=EasyID3(路径名称)
#打印mp3info.items()
#打印“页眉”,页眉
如果计数<1:#用于不在每个磁道上写入标题
#打印“页眉=”,页眉
mfile=open(文件名'a')
mfile.write(头文件)
计数+=1
如果扩展名==“mp3”:#避免出现相册艺术和错误
曲目信息='\t'+曲目编号[0]+'.+曲目名称[0]+'\n'
#如果mp3info.items()中的EasyID3(路径名称)“长度”:
#length=EasyID3(路径名称)['length'][0]
#打印长度
#曲目信息='\t'+曲目编号[0]+'.+音轨名称[0]+''+长度[0]+'\n'
#打印“跟踪信息=”,跟踪信息
mfile.write(轨道信息)
计数=0#为下一位艺术家重置-新建.txt文件
mfile.close()
路径='C:\\test'#这是一个测试路径
#path=raw\u输入('输入音乐文件的路径:\n>')
音乐专辑信息(路径)
您放弃了相册的UTF-8编码:

album.encode('utf-8')
字符串是不可变的;实际上,您放弃了bytestring返回值。存储它:

album = album.encode('utf-8')
您可以改为使用打开一个文件对象,该对象自动将写入其中的所有Unicode编码为UTF-8:

with io.open(file_name, 'a', encoding='utf-8') as mfile:
    mfile.write(header)
在这种情况下,您根本不需要对相册进行编码。

您放弃了相册的UTF-8编码:

album.encode('utf-8')
字符串是不可变的;实际上,您放弃了bytestring返回值。存储它:

album = album.encode('utf-8')
您可以改为使用打开一个文件对象,该对象自动将写入其中的所有Unicode编码为UTF-8:

with io.open(file_name, 'a', encoding='utf-8') as mfile:
    mfile.write(header)

在这种情况下,您根本不需要对
相册进行编码。

@JoranBeasley:或使用
io.open()
获取本机处理Unicode的文件对象。@JoranBeasley:或使用
io.open()
获取本机处理Unicode的文件对象。