将python unicode转换为ASCII写入文件

bkane56

我正在编写一个脚本,以检查音乐库并打印一个带有专辑名称date.txt的.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)
马丁·彼得斯(Martijn Pieters)

丢弃了相册的UTF-8编码:

album.encode('utf-8')

字符串是不可变的。您实际上丢弃了字节串返回值。存储:

album = album.encode('utf-8')

您可以改用io.open()函数打开一个文件对象,该文件对象将所有写入其中的Unicode自动编码为UTF-8:

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

album在这种情况下,您根本不需要编码

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python:将ascii转换为unicode的困难

来自分类Dev

将输入文件转换为ASCII Python

来自分类Dev

Excel函数-将Unicode转换为ASCII

来自分类Dev

将ASCII转换为Unicode编码问题

来自分类Dev

Python:将字节文件转换为可读的ascii文件

来自分类Dev

将\ u转义的Unicode字符串转换为ASCII

来自分类Dev

将\ u转义的Unicode字符串转换为ASCII

来自分类Dev

将MFC的CString转换为ASCII和UNICODE的int

来自分类Dev

将单个 Unicode 字符转换为 ASCII 字符

来自分类Dev

使用Python将Unicode写入文件

来自分类Dev

使用Python将Unicode写入文件

来自分类Dev

将ASCII文件转换为十进制文件

来自分类Dev

Python:将Unicode转换为html实体

来自分类Dev

python将unicode转换为可读字符

来自分类Dev

将Unicode文件转换为其他文件

来自分类Dev

Java程序的示例EBCDIC文件,以将EBCDIC转换为ASCII

来自分类Dev

将文件从ASCII转换为二进制

来自分类Dev

Java程序的示例EBCDIC文件,以将EBCDIC转换为ASCII

来自分类Dev

如何将JSON转换为CoffeeScript并写入文件“ .coffee”?

来自分类Dev

Python-如何将Unicode文件名转换为CP437?

来自分类Dev

将任意unicode写入文件

来自分类Dev

将 Unicode 写入 .docx 文件

来自分类Dev

将字符转换为ASCII

来自分类Dev

将 ASCII 转换为 Int

来自分类Dev

将unicode(带有BOM)字符串转换为ASCII std :: string

来自分类Dev

将数字的Unicode表示形式转换为ASCII字符串

来自分类Dev

有没有办法将unicode转换为最接近的ASCII等值?

来自分类Dev

将数字的Unicode表示形式转换为ASCII字符串

来自分类Dev

用mb_convert_encoding将ASCII编码的unicode转换为PHP中的SQL语句