UnicodeEncodeError:' ascii'编解码器不能对字符u' \ u200b'进行编码。和正则表达式修复尝试

时间:2018-01-03 11:49:29

标签: python regex

assert response == str_list_answers[elements_counter],\
                "{} != {}".format(
                str_list_answers[elements_counter],
                response)//error message on this line

我做了什么来看错误:

print response.encode('raw_unicode_escape')

我看到了什么:

Would you like to schedule the \u200bbusiness meeting 2018-01-04 at 14:00 in california and invite clara?

我做了什么来解决它:

response = re.sub('\u200b', '', response)

但没有奏效 当我正常打印print response我得到的是什么:

Would you like to schedule the ​business meeting 2018-01-04 at 14:00 in california and invite clara?

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

您可以尝试:

response = re.sub(ur'\\u200b','', response)

我查了一下:

response = "Would you like to schedule the \u200bbusiness meeting 2018-01-04 at 14:00 in california and invite clara?"
response = re.sub(ur'\\u200b','', response)
print response.encode('raw_unicode_escape')
Would you like to schedule the business meeting 2018-01-04 at 14:00 in california and invite clara?
response
'Would you like to schedule the business meeting 2018-01-04 at 14:00 in california and invite clara?'

答案 1 :(得分:0)

您需要将正则表达式模式标记为unicode字符串,前缀为u

>>> s = u"Would you like to schedule the \u200bbusiness meeting 2018-01-04 at 14:00 in california and invite clara?"
>>> '{}'.format(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u200b' in position 31: ordinal not in range(128)

>>> s1 = re.sub(u'\u200b', '', s)
>>> '{}'.format(s1)
'Would you like to schedule the business meeting 2018-01-04 at 14:00 in california and invite clara?'