简体   繁体   English

Python eyed3 UnicodeEncodeError:'ascii'编解码器无法在位置17编码字符u'\\ xe9':序数不在范围内(128)

[英]Python eyed3 UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 17: ordinal not in range(128)

Trying to rename all files in a directory using eye3d 0.7.8-final 尝试使用eye3d 0.7.8-final重命名目录中的所有文件

#! /usr/bin/env python
import os, sys, unicodedata, eyed3

def parse(sourcefile):
    audiofile = eyed3.load(sourcefile)
    if audiofile.tag.artist != audiofile.tag.artist:
        if audiofile.tag.title != audiofile.tag.title:
            temp = u"{0} - {1}.mp3".format(audiofile.tag.artist, audiofile.tag.title)
            os.rename(sourcefile, temp)

def main():
    for filelist in os.listdir('.'):
        if filelist.endswith('.mp3'):
           print u"Processing: {0}".format(filelist)
               parse(filelist)

if __name__ == "__main__":
    os.system('clear')
    main()

I though that adding the u"" to temp and print would resolves these but I am still getting 我虽然将u“”添加到temp和print可以解决这些问题,但我仍然得到

Traceback (most recent call last):
File "./test.py", line 19, in <module>
main()
File "./test.py", line 14, in main
print u"Processing: {0}".format(filelist)

UnicodeDecodeError: 'ascii' codec can't decode byte 0xcc in position 14: ordinal not in range(128)

I have also attempted this 我也尝试过

reload(sys)
sys.setdefaultencoding('utf8')

Which resulted in the following 这导致以下

Traceback (most recent call last):
File "./test.py", line 21, in <module>
main()
File "./test.py", line 14, in main
print u"Processing: {0}".format(filelist)

UnicodeEncodeError: 'ascii' codec can't encode character u'\u0308' in position 26: ordinal not in range(128)

Per @DevShark I set the following but the same error is displayed 每@DevShark我设置以下内容,但显示相同的错误

temp = temp.encode('ascii','ignore')

Traceback (most recent call last):
File "./test.py", line 22, in <module>
main()
File "./test.py", line 15, in main
print u"Processing: {0}".format(filelist)

UnicodeEncodeError: 'ascii' codec can't encode character u'\u0308' in position 26: ordinal not in range(128)

So it seems the files renamed correctly but there is still a problem with the 因此,似乎文件已正确重命名,但仍然存在问题

filelist = filelist.encode('ascii','ignore')
print u"Processing: {0}".format(filelist)

I would suggest to not use unicode to name files, use ascii instead. 我建议不要使用unicode来命名文件,而应使用ascii。 Depending on your operating system, it can cause bad behaviour. 根据您的操作系统,它可能导致不良行为。

You can just add the following line before you rename your file: 您可以在重命名文件之前添加以下行:

temp = temp.encode('ascii', 'ignore')

It will just skip the unicode characters in your file names, and will fix your code. 它只会跳过文件名中的unicode字符,并会修复您的代码。

If you want to keep unicode names and not ascii, I would suggest you make sure you understand well the concepts before. 如果您要保留unicode名称而不是ASCII,建议您一定要对概念有所了解。 It's one of these things that can lead to a lot of pain down the road if done without a clear understanding. 如果没有明确的理解,这就是其中之一,可能会导致很多痛苦。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UnicodeEncodeError: &#39;ascii&#39; codec can&#39;t encode character u&#39;\\xe9&#39; in position 54: ordinal not in range(128) - UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 54: ordinal not in range(128) Python - &#39;ascii&#39; 编解码器无法对位置 5 中的字符 u&#39;\\xe9&#39; 进行编码:序号不在范围内(128) - Python - 'ascii' codec can't encode character u'\xe9' in position 5: ordinal not in range(128) Cassandra:&#39;ascii&#39;编解码器无法在位置218处编码字符u&#39;\\ xe9&#39;:序数不在范围内(128) - Cassandra : 'ascii' codec can't encode character u'\xe9' in position 218: ordinal not in range(128) UnicodeEncodeError:'ascii'编解码器无法编码位置17710中的字符u'\ xe7':序数不在范围内(128) - UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in position 17710: ordinal not in range(128) UnicodeEncodeError:&#39;ascii&#39;编解码器无法在位置31编码字符&#39;\\ xe9&#39;:安装金字塔期间序数不在range(128)中 - UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 31: ordinal not in range(128) during installing pyramid Python:UnicodeEncodeError:&#39;ascii&#39;编解码器无法编码位置78中的字符u&#39;\\ xf1&#39;:序数不在范围内(128) - Python: UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1' in position 78: ordinal not in range(128) Python:UnicodeEncodeError:&#39;ascii&#39; 编解码器无法在位置 0 中对字符 &#39;\Ο&#39; 进行编码:序号不在范围内 (128) - Python: UnicodeEncodeError: 'ascii' codec can't encode character '\u039f' in position 0: ordinal not in range(128) Python:UnicodeEncodeError:&#39;ascii&#39;编解码器无法在位置0编码字符u&#39;\\ xfc&#39;:序数不在范围内(128)-&gt; Excel - Python: UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 0: ordinal not in range(128) -> Excel UnicodeEncodeError:&#39;ascii&#39;编解码器无法编码字符u&#39;\\ xe9&#39; - UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' UnicodeEncodeError:&#39;ascii&#39;编解码器不能编码字符u&#39;\\ xe9&#39; - UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM