【发布时间】:2021-09-06 19:46:09
【问题描述】:
我有一些这样的数组:
['دوره', 'ندارد', '\uf0d6', 'دارد']
我想从中删除特定的 Unicode 字符,例如 \uf0d6。我试过这个:
for item in tagArray[0]:
if item.startswith('\u'):
tagArray[0] = tagArray[0].remove(item)
但是当我运行它时,我收到了这个错误:
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \uXXXX escape
如何解决这个问题?
编辑:ASCII 字符的编码/解码或
【问题讨论】:
-
这是因为 python 期望在
\u之后有 4 位数字。相反,请尝试通过 ASCII 范围 (> 128) 之外的范围查找 unicode 字符。 -
@sahasrara62 这个方法不起作用,因为波斯字符不是 ASCII,所以它会全部删除
-
@Frontear 这个方法不起作用,因为波斯字符不是 ASCII,所以它会全部删除
-
'\uXXXX' 不是由许多字符组成的字符串:它没有
` noru. It is just an escape sequence meaning: *this really mean the character/code point at XXXX (in Unicode Database)*. When Python execute the code, it just see one character. Note: it is much like other escape sequences, like\n`(通常用于无法打印和/或难以键入,或者仅仅因为特定的需要代码点)。