简体   繁体   English

将亚洲字符写入文件时出现 UnicodeEncodeError

[英]UnicodeEncodeError when writing asian characters to a file

I've been running into this error rather consistently when trying to write Asian-characters to a file - I can print the characters just fine in IDLE (though not in the command line) but when I try to write them to a file, I get this error:在尝试将亚洲字符写入文件时,我一直遇到此错误 - 我可以在 IDLE 中很好地打印字符(尽管不在命令行中)但是当我尝试将它们写入文件时,我得到这个错误:

Traceback (most recent call last):
  File "C:\Users\Tai\Desktop\Development\playlistcreator\main.py", line 21, in <module>
    playlistcreator.addtoplaylist(list_of_paths,"C:\\Users\\Tai\\Desktop\\New Music\\testplaylist.m3u8")
  File "C:\Users\Tai\Desktop\Development\playlistcreator\playlistcreator.py", line 74, in addtoplaylist
    playlistFile.write(track + '\n')
  File "C:\Users\Tai\AppData\Local\Programs\Python\Python35-32\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 56-57: character maps to <undefined>

I think understand what the error is: basically it can't render Asian characters so it can't write them to a file - but if I can print them in IDLE, why wouldn't it be able to write them to a file?认为理解错误是什么:基本上它无法呈现亚洲字符,因此无法将它们写入文件 - 但如果我可以在 IDLE 中打印它们,为什么不能将它们写入文件? Is there any way to get around this?有没有办法解决这个问题?

(I'm not sure if the code is necessary to understand the error, but I'll post it anyway) (我不确定代码是否需要理解错误,但无论如何我都会发布)

def addtoplaylist(paths, playlist_path):
    # Open file (or create if it does not exist)
    #change to given directory
    try:
        os.chdir(os.path.dirname(paths))
    except Exception as err:
        print(err)
    #open file - if it does not exist, create it
    with open(os.path.basename(playlist_path), 'a+') as playlistFile:
        for track in paths:
            playlistFile.write(track + '\n')
    playlistFile.close()

You need to specify the encoding for your file when opening it, for example:打开文件时需要指定文件的编码,例如:

with open(os.path.basename(playlist_path), 'a+', encoding='UTF-8') as playlistFile:

Otherwise Python defaults to using your console encoding (ANSI) for files, in this case CP 1252 which is the Windows name for extended Latin-1.否则,Python 默认使用您的控制台编码 (ANSI) 来处理文件,在这种情况下, CP 1252是扩展 Latin-1 的 Windows 名称。

You need to use an encoding suitable for the characters.您需要使用适合字符的编码。

playlistFile.write(track.encode(encoding=some_encoding) + '\n')

I guess some_encode = 'utf-8' will do but not really sure.我猜some_encode = 'utf-8'会做但不太确定。

Maybe IDLE is using a different encoding by default and that's why it works there.也许 IDLE 默认使用不同的编码,这就是它在那里工作的原因。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM