UnicodeEncodeError:“ascii”编解码器无法对字符 u'\u2019' 进行编码在位置 126:序数不在范围内(128)
好吧,我已经阅读了许多类似的问题,并且我相信我正确地遵循了建议,但不知何故我的代码仍然无法正常工作。
我已经解析了一个xml文件。我在这里读到输出现在是unicode。我正在使用 csv writer 将输出写入文件。
因此,在我的代码中,我在使用 writerow 之前尝试使用 utf-8 进行编码。为什么我仍然收到 writerow 错误?我的警告,“unicode!!!”在发生此错误之前不会被抛出(我在多个文件上运行此错误,并且它适用于大多数文件)。但事实上,我不明白为什么 writerow 试图使用 ascii,它不应该期待 utf-8 吗?我在编码函数中将 utf-8 替换为 ascii 只是为了好玩。结果相同。请帮忙!!!
try:
mystring=elem.find('./'+r2+'Description').text
if isinstance(mystring, unicode):
print("unicode!!!")
mystring.encode('utf-8','ignore')
datalist.append(mystring)
else:
datalist.append(mystring)
except AttributeError:
datalist.append('No text')
c.writerow(datalist)
Okay, I have read through many similar questions, and I believe I am following the advice correctly, but somehow my code is still not working.
I have parsed an xml file. I have read on here that the output is now unicode. I am using the csv writer to write output to a file.
So, in my code I have tried to encode in utf-8 before using writerow. Why do I still get the error on writerow? My warning, "unicode!!!" does not get thrown until this error happens (I am running this on multiple files, and it works for most). Actually, though, I don't understand why the writerow is trying to use ascii, shouldn't it be expecting utf-8? I have replaced utf-8 with ascii in the encode function just for kicks. Same results. Please help!!!
try:
mystring=elem.find('./'+r2+'Description').text
if isinstance(mystring, unicode):
print("unicode!!!")
mystring.encode('utf-8','ignore')
datalist.append(mystring)
else:
datalist.append(mystring)
except AttributeError:
datalist.append('No text')
c.writerow(datalist)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您调用 mystring.encode(... 时,它不会就地更改字符串;它会返回一个新字符串。
When you call
mystring.encode(...
, it's not changing the string in-place; it returns a new string.