UnicodeEncodeError: 'ascii' codec can't encode character u'xe9' in position 7: ordinal not in range(128)
本问题已经有最佳答案,请猛点这里访问。
我有这个代码:
1 2 3 4 5 | printinfo = title +"\t" + old_vendor_id +"\t" + apple_id + ' ' # Write file f.write (printinfo + ' ') |
但是我在运行它时得到这个错误:
1 2 3 | f.write(printinfo + ' ') UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 7: ordinal not in range(128) |
这是Touble写的:
1 | Identité secrète (Abduction) [VF] |
有什么想法吗?不知道怎么解决。
干杯。
更新:这是我的大部分代码,因此您可以看到我在做什么:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | 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 ' ' in title: title= title.replace(' ', ' ') 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 + ' ') f.write(printinfo.encode('utf8') + ' ') f.close() |
在写入文件之前,您需要显式编码Unicode,否则Python会使用默认的ASCII编解码器为您编码。
选择一个编码并坚持下去:
1 2 | f.write(printinfo.encode('utf8') + ' ') |
或者使用
1 2 3 | import io f = io.open(filename, 'w', encoding='utf8') |
您可能需要阅读:
python unicode howto
Ned Batchelder的实用Unicode
绝对最小值每个软件开发人员绝对,肯定必须知道Unicode和字符集(没有借口!)Joel Spolsky
继续之前。