【问题标题】:UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 32: ordinal not in range(128)UnicodeEncodeError:'ascii' 编解码器无法在位置 32 编码字符 u'\u2019':序数不在范围内(128)
【发布时间】:2017-09-08 15:51:19
【问题描述】:

我有一个下面给出的字符串列表:

[u'Any subscription charges to avail this facility',
 u'credited into the beneficiary\u2019s account',
 u'funds have been credited in the beneficiary\u2019s account',
 u'Can I reuse VPA']

字符串 (\u2019) 中有一些 unicode char 表示 (') 标点符号。请让我知道如何将其删除为创建错误。我已使用以下代码删除但不起作用:

for x in mylist:
  x.encode('ascii','ignore')
  new_list.append(x)

但它返回与 unicode 字符相同的列表。请帮忙

【问题讨论】:

  • 你能用简单的' 替换\u2019 吗?或者 b) 使用 utf-8 而不是 ascii 编码?
  • @MadPhysicist,你能告诉我怎么做吗?在代码中
  • x.encode() 返回编码后的结果,不会修改 x: 你想要的:new_list.append(x.encode('ascii','ignore'))
  • 啊,你打败了我
  • @TemporalWolf,谢谢!!你的方法有效

标签: python python-2.7 unicode encoding


【解决方案1】:

您没有将编码值附加到 new_list。

for x in mylist:
   new_list.append(x.encode('ascii','ignore'))


['Any subscription charges to avail this facility', 
'credited into the beneficiarys account', 
'funds have been credited in the beneficiarys account', 
'Can I reuse VPA']

【讨论】:

  • 仅代码的答案很少有帮助,尤其是考虑到它对未来的读者(可能有类似问题)有用的重点。如果您想回答问题,请查看How to Answer 并提供更多信息。
猜你喜欢
  • 2017-03-29
  • 2014-12-25
  • 2019-11-10
  • 2016-08-26
  • 2011-07-05
  • 2018-07-10
  • 2012-04-14
  • 2016-09-05
  • 2013-02-11
相关资源
最近更新 更多