问题描述
我有这个代码:
printinfo = title + " " + old_vendor_id + " " + apple_id + '
'
# Write file
f.write (printinfo + '
')
但是我在运行时遇到这个错误:
But I get this error when running it:
f.write(printinfo + '
')
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 '
' in title:
title= title.replace('
', ' ')
while ' ' in title:
title = title.replace(' ', ' ')
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 + " " + old_vendor_id + " " + apple_id
# Write file
# f.write (printinfo + '
')
f.write(printinfo.encode('utf8') + '
')
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') + '
')
或使用 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')
您可能想阅读:
Pragmatic Unicode 作者 Ned Batchelder
Pragmatic Unicode by Ned Batchelder
每个软件开发人员绝对、肯定必须了解 Unicode 和字符集的绝对最低要求(没有借口)!) 作者:乔尔·斯波尔斯基
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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!