UnicodeEncodeError:'ascii'编解码器不能将位置7中的字符u'\xe9编码:序号不在范围(128) [英] UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 7: ordinal not in range(128)

查看:148
本文介绍了UnicodeEncodeError:'ascii'编解码器不能将位置7中的字符u'\xe9编码:序号不在范围(128)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

    printinfo = title + "\t" + old_vendor_id + "\t" + apple_id + '\n'
    # Write file
    f.write (printinfo + '\n')

但是运行它时会收到以下错误:

But I get this error when running it:

    f.write(printinfo + '\n')
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 7: ordinal not in range(128)

这是有可能写出来的:

Identité secrète (Abduction) [VF]

任何想法,不知道如何解决。

Any ideas please, not sure how to fix.

欢呼。

更新:
这是我的代码的大部分,所以你可以看到我在做什么:

UPDATE: This is the bulk of my code, so you can see what I am doing:

def runLookupEdit(self, event):
    newpath1 = pathindir + "/"
    errorFileOut = newpath1 + "REPORT.csv"
    f = open(errorFileOut, 'w')

global old_vendor_id

for old_vendor_id in vendorIdsIn.splitlines():
    writeErrorFile = 0
    from lxml import etree
    parser = etree.XMLParser(remove_blank_text=True) # makes pretty print work

    path1 = os.path.join(pathindir, old_vendor_id)
    path2 = path1 + ".itmsp"
    path3 = os.path.join(path2, 'metadata.xml')

    # Open and parse the xml file
    cantFindError = 0
    try:
        with open(path3): pass
    except IOError:
        cantFindError = 1
        errorMessage = old_vendor_id
        self.Error(errorMessage)
        break
    tree = etree.parse(path3, parser)
    root = tree.getroot()

    for element in tree.xpath('//video/title'):
        title = element.text
        while '\n' in title:
            title= title.replace('\n', ' ')
        while '\t' in title:
            title = title.replace('\t', ' ')
        while '  ' in title:
            title = title.replace('  ', ' ')
        title = title.strip()
        element.text = title
    print title

#########################################
######## REMOVE UNWANTED TAGS ########
#########################################

    # Remove the comment tags
    comments = tree.xpath('//comment()')
    q = 1
    for c in comments:
        p = c.getparent()
        if q == 3:
            apple_id = c.text
        p.remove(c)
        q = q+1

    apple_id = apple_id.split(':',1)[1]
    apple_id = apple_id.strip()
    printinfo = title + "\t" + old_vendor_id + "\t" + apple_id

    # Write file
    # f.write (printinfo + '\n')
    f.write(printinfo.encode('utf8') + '\n')
f.close()


推荐答案

您需要在编写文件之前明确编码Unicode,否则Python会使用默认的ASCII编解码器。

You need to encode Unicode explicitly before writing to a file, otherwise Python does it for you with the default ASCII codec.

选择一个编码并坚持下来:

Pick an encoding and stick with it:

f.write(printinfo.encode('utf8') + '\n')

或使用 io.open() 创建一个文件对象,为您写入您编写的文件对象文件:

or use io.open() to create a file object that'll encode for you as you write to the file:

import io

f = io.open(filename, 'w', encoding='utf8')

您可能想阅读:

务实Unicode 由Ned Batchelder

Pragmatic Unicode by Ned Batchelder

绝对最小的每个软件开发人员绝对必须了解Unicode和字符集(No Excuses!) by Joel Spolsky

The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) by Joel Spolsky

继续。

这篇关于UnicodeEncodeError:'ascii'编解码器不能将位置7中的字符u'\xe9编码:序号不在范围(128)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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