2017-07-15 54 views
0

在以下代码中,rawwant都是1,但它们在计算机中不同。我想将raw转换为want。我有很多这样的strs,我怎样才能在Python中转换它们?'1' - >'1',如何将左str转换为右str

raw = '1' 
want = '1' 
print(ord(raw)) # 65297 
print(ord(want)) # 49 
+0

'ord(raw.strip())'? –

+3

[将unicode表示的数字转换为ascii字符串]可能重复(https://stackoverflow.com/questions/25313773/convert-unicode-representation-of-number-to-ascii-string) –

+0

以下url解决了我的问题。谢谢。 http://www.cnblogs.com/kaituorensheng/p/3554571.html –

回答

0

raw看起来像一个unicode '1'(也许U+FF11?),而不是一个ASCII '1'。您可以使用以unicode字符作为键和ascii字符作为值的字典。这可能看起来像

lookup = {'1': '1', ...} 

然后,当你想要的字符转换,你可以不喜欢

want = lookup[raw] 

编辑:This可能是一个更强大的答案。