使用codecs.open()的Unicode解码错误 [英] Unicode decode error using codecs.open()

查看:781
本文介绍了使用codecs.open()的Unicode解码错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个字符编码问题,如下所示:

  rating ='Barntillåten'
new_file = codecs.open(os.path.join(folder,metadata.xml),'w','utf-8')
new_file.write (

<?xml version =1.0encoding =UTF-8?>
< ratings>
< rating system = s>%s< / rating>
< / rating>%(values ['rating_system'],rating))

我得到的错误是:

 在write_file 
< / rating>%(values ['rating_system'],rating)中的文件./assetshare.py,第314行)
/ pre>

我知道编码错误与Barntillåten有关,因为如果我用 test ,函数工作正常。



为什么这种编码错误发生,我需要做什么来解决它?

解决方案

评级必须是Unicode字符串才能包含Unicode代码点。

  rating =u'Barntillåten'

否则,在Python 2中,非Unicode字符串'Barntillåten'包含字节(使用源代码是),而不是代码点。


I have run into a character encoding problem as follows:

rating = 'Barntillåten'
new_file = codecs.open(os.path.join(folder, "metadata.xml"), 'w', 'utf-8')
new_file.write(

"""<?xml version="1.0" encoding="UTF-8"?>
   <ratings>
        <rating system="%s">%s</rating>
   </ratings>""" % (values['rating_system'], rating))

The error I get is:

  File "./assetshare.py", line 314, in write_file
    </ratings>""" % (values['rating_system'], rating))

I know that the encoding error is related to Barntillåten, because if I replace that word with test, the function works fine.

Why is this encoding error happening and what do I need to do to fix it?

解决方案

rating must be a Unicode string in order to contain Unicode codepoints.

rating = u'Barntillåten'

Otherwise, in Python 2, the non-Unicode string 'Barntillåten' contains bytes (encoded with whatever your source encoding was), not codepoints.

这篇关于使用codecs.open()的Unicode解码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆