2017-02-20 70 views
1

我在列表中有几个字是​​。我想用空字符串替换'\u'。我环顾四周,但迄今为止没有任何工作可以帮助我。我试图使用"%r"%word转换为原始字符串,但没有奏效。我也尝试使用word.encode('unicode-escape'),但没有得到任何地方。有任何想法吗?从字符串中移除 u?

编辑

添加代码

word = '\u2019' 
word.encode('unicode-escape') 
print(word) # error 

word = '\u2019' 
word = "%r"%word 
print(word) # error 
+4

请包含一些代码,显示您已经尝试过的内容。 – Petar

+0

''\ uword'.replace(r'\ u','')' - >''word'' – martineau

+1

用''替换'\\ u' – MohaMad

回答

3

我是在假设字符串的.encode方法修改字符串就地类似于列表的.sort()方法犯错误。但是,根据文档

The opposite method of bytes.decode() is str.encode(), which returns a bytes representation of the Unicode string, encoded in the requested encoding.

def remove_u(word): 
    word_u = (word.encode('unicode-escape')).decode("utf-8", "strict") 
    if r'\u' in word_u: 
     # print(True) 
     return word_u.split('\\u')[1] 
    return word 

vocabulary_ = [remove_u(each_word) for each_word in vocabulary_] 
-1

因为你正面临着编码和Unicode问题,这将有助于了解你所使用的Python版本。 我不知道如果我得到你的权利,但是这应该做的伎俩:

string = r'\uword' 
string.replace(r'\u','') 
+0

我没有原始字符串。我有一个''\ u2019''形式的字符串字面值。当'string ='\ u2019''时,上述方法不起作用 –

0

如果我理解正确的,你没有使用正则表达式。刚刚尝试:

>>> # string = '\u2019' 
>>> char = string.decode('unicode-escape') 
>>> print format(ord(char), 'x') 
2019 
0

假设你正在处理的唯一字符串。 我们可以简单地使用字符串函数将其转换为字符串。

>>> string = u"your string" 
>>> string 
u'your string' 
>>> str(string) 
'your string' 

猜猜这会做!